diff --git a/astro.config.mjs b/astro.config.mjs index 0ba1893..b70d622 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,10 +1,7 @@ import { defineConfig } from "astro/config"; +import cozyBuild from './plugins/cozy-build.ts'; import node from "@astrojs/node"; -import { readFile, writeFile } from 'node:fs/promises'; -import { fileURLToPath } from 'node:url'; - -let assets = [] // https://astro.build/config export default defineConfig({ @@ -13,19 +10,6 @@ export default defineConfig({ mode: "middleware" }), integrations: [ - { - 'name': 'astro-cozy-build', - 'hooks': { - 'astro:build:ssr': (options) => { - assets = options.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 originalScript = await readFile(outFile); - await writeFile(outFile, 'const assets = ' + JSON.stringify(assets) + ';' + originalScript); - } - } - } + cozyBuild() ] }); \ No newline at end of file diff --git a/plugins/cozy-build.ts b/plugins/cozy-build.ts new file mode 100644 index 0000000..1cb9041 --- /dev/null +++ b/plugins/cozy-build.ts @@ -0,0 +1,34 @@ +import { AstroIntegration } from 'astro'; +import { readFile, writeFile } from 'node:fs/promises'; +import { fileURLToPath } from 'node:url'; + +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 originalScript = await readFile(outFile); + 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/public/sw.js b/public/sw.js index ea45902..3a7a8c5 100644 --- a/public/sw.js +++ b/public/sw.js @@ -1,12 +1,13 @@ +const cacheName = 'cozy-reader-app-v' + (version ?? '000') const addResourcesToCache = async (resources) => { - const cache = await caches.open('cozy-reader-app-v1'); + const cache = await caches.open(cacheName); console.log('adding resources to cache...', resources) await cache.addAll(resources); }; const putInCache = async (request, response) => { - const cache = await caches.open('cozy-reader-app-v1'); + const cache = await caches.open(cacheName); console.log('adding one response to cache...', request) await cache.put(request, response); };