From bd49663ceed764d8c6be865ca793d29091604856 Mon Sep 17 00:00:00 2001 From: ayoayco Date: Sun, 18 Aug 2024 11:42:01 +0200 Subject: [PATCH] feat: expose registration hooks --- astro.config.mjs | 13 +++++++++++++ index.js | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/astro.config.mjs b/astro.config.mjs index 5b6638d..45907ac 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,3 +1,5 @@ +// @ts-check + import { defineConfig } from "astro/config"; import node from "@astrojs/node"; import serviceWorker from "./index.js"; @@ -20,6 +22,17 @@ export default defineConfig({ logAssets: true, esbuild: { minify: true + }, + registrationHooks: { + installing: () => console.log('>>> installing...'), + waiting: () => console.log('>>> waiting...'), + active: () => console.log('>>> active...'), + error: (error) => console.error('>>> error', error), + unsupported: () => console.log('>>> service worker unsupported'), + afterRegistration: async () => { + const sw = await navigator.serviceWorker.getRegistration(); + console.log('>>> registrered', sw) + } } }) ] diff --git a/index.js b/index.js index b952f14..8c63dd1 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,15 @@ const ASTROSW = '@ayco/astro-sw'; * customRoutes?: string[], * excludeRoutes?: string[], * logAssets?: true, - * esbuild?: BuildOptions + * esbuild?: BuildOptions, + * registrationHooks?: { + * installing?: () => void, + * waiting?: () => void, + * active?: () => void, + * error?: (error) => void, + * unsupported?: () => void, + * afterRegistration?: () => void, + * } * }} options * @returns {AstroIntegration} */ @@ -37,9 +45,19 @@ export default function serviceWorker(options) { customRoutes = [], excludeRoutes = [], logAssets = false, - esbuild = {} + esbuild = {}, + registrationHooks = {} } = options || {}; + const { + installing: installingFn, + waiting: waitingFn, + active: activeFn, + error: errorFn, + unsupported: unsupportedFn, + afterRegistration: afterRegistrationFn, + } = registrationHooks; + /** * @type {Array} */ @@ -52,22 +70,19 @@ export default function serviceWorker(options) { scope: "/", }); if (registration.installing) { - // installingFn(); - console.log('[${ASTROSW}] Installing...') + (${installingFn.toString()})(); } else if (registration.waiting) { - // installedFn(); - console.log('[${ASTROSW}] Installed...') + (${waitingFn.toString()})(); } else if (registration.active) { - // activeFn(); - console.log('[${ASTROSW}] Active...') + (${activeFn.toString()})(); } + + (${afterRegistrationFn.toString()})(); } catch (error) { - // onError(error); - console.error('[${ASTROSW}] ERR', error) + (${errorFn.toString()})(error); } } else { - // onUnsupported(); - console.log('[${ASTROSW}] Browser does not support Service Worker') + (${unsupportedFn.toString()})(); } } @@ -85,7 +100,7 @@ export default function serviceWorker(options) { // REQUIRED OPTION IS MISSING logger.error('Missing required path to service worker script'); } - + // const transformedScript=await transform(registrationScript) output = _config.output; if (command === 'build') {