From 43dfab51739b926c3952a18d25895a8a10035188 Mon Sep 17 00:00:00 2001 From: ayoayco Date: Thu, 15 Aug 2024 16:43:08 +0200 Subject: [PATCH] feat: add routes to assets to be cached --- index.js | 14 ++++++++++++-- public/sample.asset.txt | 1 + src/pages/blog/index.astro | 3 +++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 public/sample.asset.txt create mode 100644 src/pages/blog/index.astro diff --git a/index.js b/index.js index 7f62728..da50ff7 100644 --- a/index.js +++ b/index.js @@ -77,13 +77,23 @@ export default function serviceWorker(config) { injectScript('page', registrationScript); }, 'astro:build:ssr': ({ manifest }) => { - assets = [...new Set(manifest.assets ?? [])].filter(ass => !ass.includes('sw.js')) + assets = manifest.assets.filter(ass => !ass.includes('sw.js')) }, - 'astro:build:done': async ({ dir }) => { + 'astro:build:done': async ({ dir, routes }) => { const outFile = fileURLToPath(new URL('./sw.js', dir)); const __dirname = path.resolve(path.dirname('.')); const swPath = path.join(__dirname, serviceWorkerPath ?? ''); let originalScript; + + const _routes = routes + .filter(({isIndex}) => isIndex) + .map(({pathname}) => pathname) + ?? []; + + assets = [...new Set([...assets, ..._routes])] + + console.log('>>> assets', assets); + try { console.log('[astro-sw] Using service worker:', swPath); originalScript = await readFile(swPath); diff --git a/public/sample.asset.txt b/public/sample.asset.txt new file mode 100644 index 0000000..a95e94f --- /dev/null +++ b/public/sample.asset.txt @@ -0,0 +1 @@ +asset \ No newline at end of file diff --git a/src/pages/blog/index.astro b/src/pages/blog/index.astro new file mode 100644 index 0000000..7003235 --- /dev/null +++ b/src/pages/blog/index.astro @@ -0,0 +1,3 @@ +--- +--- +blog index \ No newline at end of file