feat: use published @ayco/astro-sw
This commit is contained in:
parent
881c946a0a
commit
38db5a26e9
5 changed files with 14 additions and 60 deletions
|
@ -1,6 +1,6 @@
|
||||||
import { defineConfig } from "astro/config";
|
import { defineConfig } from "astro/config";
|
||||||
import node from "@astrojs/node";
|
import node from "@astrojs/node";
|
||||||
import serviceWorker from "./src/plugins/astro-sw.ts";
|
import serviceWorker from "@ayco/astro-sw";
|
||||||
|
|
||||||
// https://astro.build/config
|
// https://astro.build/config
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
"deploy": "astro build && scp -r dist ayo@ayco.io:~/cozy/"
|
"deploy": "astro build && scp -r dist ayo@ayco.io:~/cozy/"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@ayco/astro-sw": "^0.0.1",
|
||||||
"astro": "^4.12.2"
|
"astro": "^4.12.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
@ -36,6 +36,9 @@ importers:
|
||||||
specifier: ^1.5.3
|
specifier: ^1.5.3
|
||||||
version: 1.5.3
|
version: 1.5.3
|
||||||
devDependencies:
|
devDependencies:
|
||||||
|
'@ayco/astro-sw':
|
||||||
|
specifier: ^0.0.1
|
||||||
|
version: 0.0.1
|
||||||
astro:
|
astro:
|
||||||
specifier: ^4.12.2
|
specifier: ^4.12.2
|
||||||
version: 4.12.2(@types/node@20.14.12)(sass@1.77.8)
|
version: 4.12.2(@types/node@20.14.12)(sass@1.77.8)
|
||||||
|
@ -79,6 +82,10 @@ packages:
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
astro: ^4.12.2
|
astro: ^4.12.2
|
||||||
|
|
||||||
|
'@ayco/astro-sw@0.0.1':
|
||||||
|
resolution: {integrity: sha512-zzuOjXBKmnm9cvyFPckk/dLmWkwHZs1ZS5L7YVzcPXJfGnEre01awqy0fIvw2ErGc3DDULo1llPtAfW31b6EGQ==}
|
||||||
|
engines: {node: '>=18.0.0'}
|
||||||
|
|
||||||
'@babel/code-frame@7.24.7':
|
'@babel/code-frame@7.24.7':
|
||||||
resolution: {integrity: sha512-BcYH1CVJBO9tvyIZ2jVeXgSIMvGZ2FDRvDdOIVQyuklNKSsx+eppDEBq/g47Ayw+RqNFE+URvOShmf+f/qwAlA==}
|
resolution: {integrity: sha512-BcYH1CVJBO9tvyIZ2jVeXgSIMvGZ2FDRvDdOIVQyuklNKSsx+eppDEBq/g47Ayw+RqNFE+URvOShmf+f/qwAlA==}
|
||||||
engines: {node: '>=6.9.0'}
|
engines: {node: '>=6.9.0'}
|
||||||
|
@ -2443,6 +2450,8 @@ snapshots:
|
||||||
dependencies:
|
dependencies:
|
||||||
astro: 4.12.2(@types/node@20.14.12)(sass@1.77.8)
|
astro: 4.12.2(@types/node@20.14.12)(sass@1.77.8)
|
||||||
|
|
||||||
|
'@ayco/astro-sw@0.0.1': {}
|
||||||
|
|
||||||
'@babel/code-frame@7.24.7':
|
'@babel/code-frame@7.24.7':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@babel/highlight': 7.24.7
|
'@babel/highlight': 7.24.7
|
||||||
|
|
|
@ -1,54 +0,0 @@
|
||||||
import { AstroIntegration } from 'astro';
|
|
||||||
import { readFile, writeFile } from 'node:fs/promises';
|
|
||||||
import { fileURLToPath } from 'node:url';
|
|
||||||
import path from 'node:path';
|
|
||||||
import { randomUUID } from "node:crypto";
|
|
||||||
|
|
||||||
let assets: string[] = [];
|
|
||||||
|
|
||||||
export type ServiceWorkerConfig = {
|
|
||||||
assetCachePrefix?: string,
|
|
||||||
assetCacheVersionID?: string,
|
|
||||||
path: string,
|
|
||||||
}
|
|
||||||
|
|
||||||
const plugin_dir = path.resolve(path.dirname('.'));
|
|
||||||
|
|
||||||
export default (config: ServiceWorkerConfig): AstroIntegration => {
|
|
||||||
let {
|
|
||||||
assetCachePrefix,
|
|
||||||
assetCacheVersionID = randomUUID(),
|
|
||||||
path: serviceWorkerPath
|
|
||||||
} = config;
|
|
||||||
|
|
||||||
console.log('[astro-sw] dir', plugin_dir)
|
|
||||||
|
|
||||||
return {
|
|
||||||
'name': 'astro-sw',
|
|
||||||
'hooks': {
|
|
||||||
'astro:build:ssr': ({ manifest }) => {
|
|
||||||
assets = manifest.assets.filter(ass => !ass.includes('sw.js'))
|
|
||||||
},
|
|
||||||
'astro:build:done': async ({ dir }) => {
|
|
||||||
const outFile = fileURLToPath(new URL('./sw.js', dir));
|
|
||||||
let originalScript;
|
|
||||||
try {
|
|
||||||
const __dirname = path.resolve(path.dirname('.'));
|
|
||||||
const swPath = path.join(__dirname, serviceWorkerPath ?? '');
|
|
||||||
console.log('[astro-sw] Using service worker:', swPath);
|
|
||||||
originalScript = await readFile(swPath);
|
|
||||||
} catch {
|
|
||||||
throw Error('[astro-sw] ERROR: service worker script not found!')
|
|
||||||
}
|
|
||||||
const assetsDeclaration = `const assets = ${JSON.stringify(assets)};\n`;
|
|
||||||
const versionDeclaration = `const version = ${JSON.stringify(assetCacheVersionID)};\n`;
|
|
||||||
const prefixDeclaration = `const prefix = ${JSON.stringify(assetCachePrefix)};\n`;
|
|
||||||
|
|
||||||
await writeFile(
|
|
||||||
outFile,
|
|
||||||
assetsDeclaration + versionDeclaration + prefixDeclaration + originalScript
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
|
@ -1,10 +1,8 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: create virtual imports for `prefix`, `version` & `assets`
|
* Note: astro-sw integration injects variables `__prefix`, `__version`, & `__assets`
|
||||||
* - currently injected by ../plugins/astro-sw.ts
|
|
||||||
*/
|
*/
|
||||||
|
const cacheName = `${__prefix ?? 'app'}-v${__version ?? '000'}`
|
||||||
const cacheName = `${prefix ?? 'app'}-v${version ?? '000'}`
|
|
||||||
const addResourcesToCache = async (resources) => {
|
const addResourcesToCache = async (resources) => {
|
||||||
const cache = await caches.open(cacheName);
|
const cache = await caches.open(cacheName);
|
||||||
console.log('adding resources to cache...', resources)
|
console.log('adding resources to cache...', resources)
|
||||||
|
@ -77,7 +75,7 @@ self.addEventListener('install', (event) => {
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
addResourcesToCache([
|
addResourcesToCache([
|
||||||
'./',
|
'./',
|
||||||
...(assets ?? [])
|
...(__assets ?? [])
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue