feat: implement customRoutes for caching

This commit is contained in:
Ayo Ayco 2024-08-15 20:51:15 +02:00
parent c17731ecbb
commit 9d9ae8e17b
2 changed files with 14 additions and 18 deletions

View file

@ -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'
]
})
]
});

View file

@ -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<string>,
* }} 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);