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',
customRoutes: [
'/threads'
],
excludeRoutes: [
'/exclude'
]
})
]

View file

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

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

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