From 9d9ae8e17bfd7556ee4c898324082664a5b74957 Mon Sep 17 00:00:00 2001 From: ayoayco Date: Thu, 15 Aug 2024 20:51:15 +0200 Subject: [PATCH] feat: implement customRoutes for caching --- astro.config.mjs | 7 +++---- index.js | 25 +++++++++++-------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/astro.config.mjs b/astro.config.mjs index 07b0938..a09980d 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -12,10 +12,9 @@ export default defineConfig({ serviceWorker({ path: "./example_sw.js", assetCachePrefix: 'cozy-reader', - // onInstalled: () => console.log('Installed...'), - // onInstalling: () => console.log('Installing...'), - // onActive: () => console.log('Active!'), - // onError: (error) => console.error(`Registration failed on ${error}`) + customRoutes: [ + '/treads' + ] }) ] }); diff --git a/index.js b/index.js index df3021c..97cbf86 100644 --- a/index.js +++ b/index.js @@ -8,10 +8,7 @@ import { randomUUID } from "node:crypto"; * path: string, * assetCachePrefix?: string, * assetCacheVersionID?: string, - * onInstalling?: Function, - * onInstalled?: Function, - * onActive?: Function, - * onError?: Function + * customRoutes?: Array, * }} ServiceWorkerConfig * @typedef {import('astro').AstroIntegration} AstroIntegration */ @@ -27,14 +24,7 @@ export default function serviceWorker(config) { assetCachePrefix, assetCacheVersionID = randomUUID(), path: serviceWorkerPath, - /** - * TODO: use registration hooks callbacks - */ - // onInstalling, - // onInstalled, - // onActive, - // onError, - // onUnsupported, + customRoutes = [] } = config; /** @@ -116,9 +106,16 @@ export default function serviceWorker(config) { .filter(pathname => pathname !== '') ?? []; - assets = [...new Set([...assets, ..._routes, ..._pages, ..._pagesWithoutEndSlash])] + assets = [...new Set([ + ...assets, + ..._routes, + ..._pages, + ..._pagesWithoutEndSlash, + ...customRoutes + ])] .filter(asset => !asset.includes('404')); - console.log('>>> assets', assets); + + console.log('[astro-sw] Assets for caching:', assets); try { console.log('[astro-sw] Using service worker:', swPath);