feat: expose registration hooks

This commit is contained in:
Ayo Ayco 2024-08-18 11:42:01 +02:00
parent 2581b073d3
commit bd49663cee
2 changed files with 41 additions and 13 deletions

View file

@ -1,3 +1,5 @@
// @ts-check
import { defineConfig } from "astro/config"; import { defineConfig } from "astro/config";
import node from "@astrojs/node"; import node from "@astrojs/node";
import serviceWorker from "./index.js"; import serviceWorker from "./index.js";
@ -20,6 +22,17 @@ export default defineConfig({
logAssets: true, logAssets: true,
esbuild: { esbuild: {
minify: true 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)
}
} }
}) })
] ]

View file

@ -25,7 +25,15 @@ const ASTROSW = '@ayco/astro-sw';
* customRoutes?: string[], * customRoutes?: string[],
* excludeRoutes?: string[], * excludeRoutes?: string[],
* logAssets?: true, * logAssets?: true,
* esbuild?: BuildOptions * esbuild?: BuildOptions,
* registrationHooks?: {
* installing?: () => void,
* waiting?: () => void,
* active?: () => void,
* error?: (error) => void,
* unsupported?: () => void,
* afterRegistration?: () => void,
* }
* }} options * }} options
* @returns {AstroIntegration} * @returns {AstroIntegration}
*/ */
@ -37,9 +45,19 @@ export default function serviceWorker(options) {
customRoutes = [], customRoutes = [],
excludeRoutes = [], excludeRoutes = [],
logAssets = false, logAssets = false,
esbuild = {} esbuild = {},
registrationHooks = {}
} = options || {}; } = options || {};
const {
installing: installingFn,
waiting: waitingFn,
active: activeFn,
error: errorFn,
unsupported: unsupportedFn,
afterRegistration: afterRegistrationFn,
} = registrationHooks;
/** /**
* @type {Array<string>} * @type {Array<string>}
*/ */
@ -52,22 +70,19 @@ export default function serviceWorker(options) {
scope: "/", scope: "/",
}); });
if (registration.installing) { if (registration.installing) {
// installingFn(); (${installingFn.toString()})();
console.log('[${ASTROSW}] Installing...')
} else if (registration.waiting) { } else if (registration.waiting) {
// installedFn(); (${waitingFn.toString()})();
console.log('[${ASTROSW}] Installed...')
} else if (registration.active) { } else if (registration.active) {
// activeFn(); (${activeFn.toString()})();
console.log('[${ASTROSW}] Active...')
} }
(${afterRegistrationFn.toString()})();
} catch (error) { } catch (error) {
// onError(error); (${errorFn.toString()})(error);
console.error('[${ASTROSW}] ERR', error)
} }
} else { } else {
// onUnsupported(); (${unsupportedFn.toString()})();
console.log('[${ASTROSW}] Browser does not support Service Worker')
} }
} }
@ -85,7 +100,7 @@ export default function serviceWorker(options) {
// REQUIRED OPTION IS MISSING // REQUIRED OPTION IS MISSING
logger.error('Missing required path to service worker script'); logger.error('Missing required path to service worker script');
} }
// const transformedScript=await transform(registrationScript)
output = _config.output; output = _config.output;
if (command === 'build') { if (command === 'build') {