feat: implement customRoutes for caching
This commit is contained in:
parent
c17731ecbb
commit
9d9ae8e17b
2 changed files with 14 additions and 18 deletions
|
@ -12,10 +12,9 @@ export default defineConfig({
|
||||||
serviceWorker({
|
serviceWorker({
|
||||||
path: "./example_sw.js",
|
path: "./example_sw.js",
|
||||||
assetCachePrefix: 'cozy-reader',
|
assetCachePrefix: 'cozy-reader',
|
||||||
// onInstalled: () => console.log('Installed...'),
|
customRoutes: [
|
||||||
// onInstalling: () => console.log('Installing...'),
|
'/treads'
|
||||||
// onActive: () => console.log('Active!'),
|
]
|
||||||
// onError: (error) => console.error(`Registration failed on ${error}`)
|
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
25
index.js
25
index.js
|
@ -8,10 +8,7 @@ import { randomUUID } from "node:crypto";
|
||||||
* path: string,
|
* path: string,
|
||||||
* assetCachePrefix?: string,
|
* assetCachePrefix?: string,
|
||||||
* assetCacheVersionID?: string,
|
* assetCacheVersionID?: string,
|
||||||
* onInstalling?: Function,
|
* customRoutes?: Array<string>,
|
||||||
* onInstalled?: Function,
|
|
||||||
* onActive?: Function,
|
|
||||||
* onError?: Function
|
|
||||||
* }} ServiceWorkerConfig
|
* }} ServiceWorkerConfig
|
||||||
* @typedef {import('astro').AstroIntegration} AstroIntegration
|
* @typedef {import('astro').AstroIntegration} AstroIntegration
|
||||||
*/
|
*/
|
||||||
|
@ -27,14 +24,7 @@ export default function serviceWorker(config) {
|
||||||
assetCachePrefix,
|
assetCachePrefix,
|
||||||
assetCacheVersionID = randomUUID(),
|
assetCacheVersionID = randomUUID(),
|
||||||
path: serviceWorkerPath,
|
path: serviceWorkerPath,
|
||||||
/**
|
customRoutes = []
|
||||||
* TODO: use registration hooks callbacks
|
|
||||||
*/
|
|
||||||
// onInstalling,
|
|
||||||
// onInstalled,
|
|
||||||
// onActive,
|
|
||||||
// onError,
|
|
||||||
// onUnsupported,
|
|
||||||
} = config;
|
} = config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -116,9 +106,16 @@ export default function serviceWorker(config) {
|
||||||
.filter(pathname => pathname !== '')
|
.filter(pathname => pathname !== '')
|
||||||
?? [];
|
?? [];
|
||||||
|
|
||||||
assets = [...new Set([...assets, ..._routes, ..._pages, ..._pagesWithoutEndSlash])]
|
assets = [...new Set([
|
||||||
|
...assets,
|
||||||
|
..._routes,
|
||||||
|
..._pages,
|
||||||
|
..._pagesWithoutEndSlash,
|
||||||
|
...customRoutes
|
||||||
|
])]
|
||||||
.filter(asset => !asset.includes('404'));
|
.filter(asset => !asset.includes('404'));
|
||||||
console.log('>>> assets', assets);
|
|
||||||
|
console.log('[astro-sw] Assets for caching:', assets);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log('[astro-sw] Using service worker:', swPath);
|
console.log('[astro-sw] Using service worker:', swPath);
|
||||||
|
|
Loading…
Reference in a new issue