feat: add excludeRoutes option

This commit is contained in:
Ayo Ayco 2024-08-16 13:17:18 +02:00
parent 8e4083cb01
commit c3df8b33da
3 changed files with 18 additions and 2 deletions

View file

@ -14,6 +14,9 @@ export default defineConfig({
assetCachePrefix: 'cozy-reader', assetCachePrefix: 'cozy-reader',
customRoutes: [ customRoutes: [
'/threads' '/threads'
],
excludeRoutes: [
'/exclude'
] ]
}) })
] ]

View file

@ -9,6 +9,7 @@ import { randomUUID } from "node:crypto";
* assetCachePrefix?: string, * assetCachePrefix?: string,
* assetCacheVersionID?: string, * assetCacheVersionID?: string,
* customRoutes?: Array<string>, * customRoutes?: Array<string>,
* excludeRoutes?: Array<string,
* }} ServiceWorkerConfig * }} ServiceWorkerConfig
* @typedef {import('astro').AstroIntegration} AstroIntegration * @typedef {import('astro').AstroIntegration} AstroIntegration
*/ */
@ -24,7 +25,8 @@ export default function serviceWorker(config) {
assetCachePrefix, assetCachePrefix,
assetCacheVersionID = randomUUID(), assetCacheVersionID = randomUUID(),
path: serviceWorkerPath, path: serviceWorkerPath,
customRoutes = [] customRoutes = [],
excludeRoutes = []
} = config; } = config;
/** /**
@ -110,6 +112,11 @@ export default function serviceWorker(config) {
.filter(pathname => pathname !== '') .filter(pathname => pathname !== '')
?? []; ?? [];
const _excludeRoutes = [
...excludeRoutes,
...excludeRoutes.map(route => `${route}/`)
];
assets = [...new Set([ assets = [...new Set([
...assets, ...assets,
..._routes, ..._routes,
@ -120,7 +127,9 @@ export default function serviceWorker(config) {
])].filter(asset => !!asset ])].filter(asset => !!asset
&& asset !== '' && asset !== ''
&& !asset.includes('404') && !asset.includes('404')
&& !asset.includes('index.html')); && !asset.includes('index.html')
&& !_excludeRoutes.includes(asset)
);
console.log('[astro-sw] Assets for caching:', assets); console.log('[astro-sw] Assets for caching:', assets);

4
src/pages/exclude.astro Normal file
View file

@ -0,0 +1,4 @@
---
---
exclude