From ea673e6bdda6d55128087a47720a368d1346866c Mon Sep 17 00:00:00 2001 From: ayoayco Date: Sat, 3 Aug 2024 01:01:58 +0200 Subject: [PATCH] feat: implement assetCachePrefix & set sw config defaults --- astro.config.mjs | 3 +-- src/plugins/astro-sw.ts | 23 +++++++++++++++++++---- src/utils/sw.js | 4 ++-- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/astro.config.mjs b/astro.config.mjs index ce23e35..30bc0dd 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,6 +1,5 @@ import { defineConfig } from "astro/config"; import node from "@astrojs/node"; -import { randomUUID } from "node:crypto"; import serviceWorker from "./src/plugins/astro-sw.ts"; // https://astro.build/config @@ -11,7 +10,7 @@ export default defineConfig({ }), integrations: [ serviceWorker({ - assetCacheVersionID: randomUUID(), + assetCachePrefix: 'cozy-reader', serviceWorkerPath: './src/utils/sw.js' }) ] diff --git a/src/plugins/astro-sw.ts b/src/plugins/astro-sw.ts index bbf8b4f..ae47e5e 100644 --- a/src/plugins/astro-sw.ts +++ b/src/plugins/astro-sw.ts @@ -2,16 +2,26 @@ 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 = { - assetCacheVersionID: string, + assetCachePrefix?: string, + assetCacheVersionID?: string, serviceWorkerPath: string, } -export default (config?: ServiceWorkerConfig): AstroIntegration => { - const { assetCacheVersionID, serviceWorkerPath } = config ?? {}; +const plugin_dir = path.resolve(path.dirname('.')); + +export default (config: ServiceWorkerConfig): AstroIntegration => { + let { + assetCachePrefix, + assetCacheVersionID = randomUUID(), + serviceWorkerPath + } = config; + + console.log('[astro-sw] dir', plugin_dir) return { 'name': 'astro-sw', @@ -32,7 +42,12 @@ export default (config?: ServiceWorkerConfig): AstroIntegration => { } const assetsDeclaration = `const assets = ${JSON.stringify(assets)};\n`; const versionDeclaration = `const version = ${JSON.stringify(assetCacheVersionID)};\n`; - await writeFile(outFile, assetsDeclaration + versionDeclaration + originalScript); + const prefixDeclaration = `const prefix = ${JSON.stringify(assetCachePrefix)};\n`; + + await writeFile( + outFile, + assetsDeclaration + versionDeclaration + prefixDeclaration + originalScript + ); } } } diff --git a/src/utils/sw.js b/src/utils/sw.js index ea52fc2..e686e1b 100644 --- a/src/utils/sw.js +++ b/src/utils/sw.js @@ -1,10 +1,10 @@ /** - * TODO: create virtual imports for `version` & `assets` + * TODO: create virtual imports for `prefix`, `version` & `assets` * - currently injected by ../plugins/astro-sw.ts */ -const cacheName = 'cozy-reader-app-v' + (version ?? '000') +const cacheName = `${prefix ?? 'app'}-v${version ?? '000'}` const addResourcesToCache = async (resources) => { const cache = await caches.open(cacheName); console.log('adding resources to cache...', resources)