From 38db5a26e9851e3678a028739324c007c1f7df95 Mon Sep 17 00:00:00 2001 From: ayoayco Date: Sat, 3 Aug 2024 08:58:57 +0200 Subject: [PATCH] feat: use published @ayco/astro-sw --- astro.config.mjs | 2 +- package.json | 1 + pnpm-lock.yaml | 9 +++++++ src/plugins/astro-sw.ts | 54 ----------------------------------------- src/utils/sw.js | 8 +++--- 5 files changed, 14 insertions(+), 60 deletions(-) delete mode 100644 src/plugins/astro-sw.ts diff --git a/astro.config.mjs b/astro.config.mjs index ddb46c6..1fcd1bb 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,6 +1,6 @@ import { defineConfig } from "astro/config"; import node from "@astrojs/node"; -import serviceWorker from "./src/plugins/astro-sw.ts"; +import serviceWorker from "@ayco/astro-sw"; // https://astro.build/config export default defineConfig({ diff --git a/package.json b/package.json index 15a2a86..9af78c0 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "deploy": "astro build && scp -r dist ayo@ayco.io:~/cozy/" }, "devDependencies": { + "@ayco/astro-sw": "^0.0.1", "astro": "^4.12.2" }, "dependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e7ee935..5a60470 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -36,6 +36,9 @@ importers: specifier: ^1.5.3 version: 1.5.3 devDependencies: + '@ayco/astro-sw': + specifier: ^0.0.1 + version: 0.0.1 astro: specifier: ^4.12.2 version: 4.12.2(@types/node@20.14.12)(sass@1.77.8) @@ -79,6 +82,10 @@ packages: peerDependencies: 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': resolution: {integrity: sha512-BcYH1CVJBO9tvyIZ2jVeXgSIMvGZ2FDRvDdOIVQyuklNKSsx+eppDEBq/g47Ayw+RqNFE+URvOShmf+f/qwAlA==} engines: {node: '>=6.9.0'} @@ -2443,6 +2450,8 @@ snapshots: dependencies: 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': dependencies: '@babel/highlight': 7.24.7 diff --git a/src/plugins/astro-sw.ts b/src/plugins/astro-sw.ts deleted file mode 100644 index 60b9de6..0000000 --- a/src/plugins/astro-sw.ts +++ /dev/null @@ -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 - ); - } - } - } -}; \ No newline at end of file diff --git a/src/utils/sw.js b/src/utils/sw.js index e686e1b..275cb26 100644 --- a/src/utils/sw.js +++ b/src/utils/sw.js @@ -1,10 +1,8 @@ /** - * TODO: create virtual imports for `prefix`, `version` & `assets` - * - currently injected by ../plugins/astro-sw.ts + * Note: astro-sw integration injects variables `__prefix`, `__version`, & `__assets` */ - -const cacheName = `${prefix ?? '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) @@ -77,7 +75,7 @@ self.addEventListener('install', (event) => { event.waitUntil( addResourcesToCache([ './', - ...(assets ?? []) + ...(__assets ?? []) ]) ); });