diff --git a/astro.config.mjs b/astro.config.mjs index b948974..ce23e35 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,6 +1,7 @@ import { defineConfig } from "astro/config"; import node from "@astrojs/node"; -import cozyBuild from "./src/plugins/cozy-build.ts"; +import { randomUUID } from "node:crypto"; +import serviceWorker from "./src/plugins/astro-sw.ts"; // https://astro.build/config export default defineConfig({ @@ -9,6 +10,9 @@ export default defineConfig({ mode: "middleware" }), integrations: [ - cozyBuild() + serviceWorker({ + assetCacheVersionID: randomUUID(), + serviceWorkerPath: './src/utils/sw.js' + }) ] }); \ No newline at end of file diff --git a/src/plugins/astro-sw.ts b/src/plugins/astro-sw.ts new file mode 100644 index 0000000..bbf8b4f --- /dev/null +++ b/src/plugins/astro-sw.ts @@ -0,0 +1,39 @@ +import { AstroIntegration } from 'astro'; +import { readFile, writeFile } from 'node:fs/promises'; +import { fileURLToPath } from 'node:url'; +import path from 'node:path'; + +let assets: string[] = []; + +export type ServiceWorkerConfig = { + assetCacheVersionID: string, + serviceWorkerPath: string, +} + +export default (config?: ServiceWorkerConfig): AstroIntegration => { + const { assetCacheVersionID, serviceWorkerPath } = config ?? {}; + + 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`; + await writeFile(outFile, assetsDeclaration + versionDeclaration + originalScript); + } + } + } +}; \ No newline at end of file diff --git a/src/plugins/cozy-build.ts b/src/plugins/cozy-build.ts deleted file mode 100644 index d167373..0000000 --- a/src/plugins/cozy-build.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { AstroIntegration } from 'astro'; -import { readFile, writeFile } from 'node:fs/promises'; -import { fileURLToPath } from 'node:url'; -import path from 'node:path'; - -let assets: string[] = []; - -function getBuildTime() { - const today = new Date(); - const dd = String(today.getDate()).padStart(2, '0'); - const mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0! - const yyyy = today.getFullYear(); - const time = today.getTime(); - - return mm + dd + yyyy + time; -} - - -const config: AstroIntegration = { - 'name': 'astro-cozy-build', - 'hooks': { - 'astro:build:ssr': ({ manifest }) => { - assets = manifest.assets.filter(ass => !ass.includes('sw.js')) - console.log('build-cozy', assets) - }, - 'astro:build:done': async ({ dir }) => { - const outFile = fileURLToPath(new URL('./sw.js', dir)); - const __dirname = path.resolve(path.dirname('.')); - const originalScript = await readFile(__dirname + '/src/plugins/sw.js'); - const assetsDeclaration = `const assets = ${JSON.stringify(assets)};\n`; - const versionDeclaration = `const version = ${JSON.stringify(getBuildTime())};\n`; - await writeFile(outFile, assetsDeclaration + versionDeclaration + originalScript); - } - } -} - -export default (): AstroIntegration => config; \ No newline at end of file diff --git a/src/plugins/sw.js b/src/utils/sw.js similarity index 96% rename from src/plugins/sw.js rename to src/utils/sw.js index 3a7a8c5..ea52fc2 100644 --- a/src/plugins/sw.js +++ b/src/utils/sw.js @@ -1,4 +1,9 @@ +/** + * TODO: create virtual imports for `version` & `assets` + * - currently injected by ../plugins/astro-sw.ts + */ + const cacheName = 'cozy-reader-app-v' + (version ?? '000') const addResourcesToCache = async (resources) => { const cache = await caches.open(cacheName);