diff --git a/src/components/compress/index.tsx b/src/components/compress/index.tsx index 6048e324..62948abd 100644 --- a/src/components/compress/index.tsx +++ b/src/components/compress/index.tsx @@ -156,7 +156,7 @@ async function processSvg(blob: Blob): Promise { return blobToImg(new Blob([newSource], { type: 'image/svg+xml' })); } -async function getOldestServiceWorker() { +async function getMostActiveServiceWorker() { const reg = await navigator.serviceWorker.getRegistration(); if (!reg) return null; return reg.active || reg.waiting || reg.installing; @@ -209,7 +209,7 @@ export default class Compress extends Component { .then(async (userInteracted: boolean | undefined) => { if (userInteracted) return; set('user-interacted', true); - const serviceWorker = await getOldestServiceWorker(); + const serviceWorker = await getMostActiveServiceWorker(); if (!serviceWorker) return; // Service worker not installing yet. serviceWorker.postMessage('cache-all'); }); diff --git a/src/sw/util.ts b/src/sw/util.ts index fd5582a9..40e2fcc9 100644 --- a/src/sw/util.ts +++ b/src/sw/util.ts @@ -46,6 +46,12 @@ export function cleanupCache(event: FetchEvent, cacheName: string, keepAssets: s }()); } +function getAssetsWithPrefix(assets: string[], prefixes: string[]) { + return assets.filter( + asset => prefixes.some(prefix => asset.startsWith(prefix)), + ); +} + export async function cacheBasics(cacheName: string, buildAssets: string[]) { const toCache = ['/', '/assets/favicon.ico']; @@ -60,9 +66,7 @@ export async function cacheBasics(cacheName: string, buildAssets: string[]) { 'logo.', ]; - const prefixMatches = buildAssets.filter( - asset => prefixesToCache.some(prefix => asset.startsWith(prefix)), - ); + const prefixMatches = getAssetsWithPrefix(buildAssets, prefixesToCache); toCache.push(...prefixMatches); @@ -80,10 +84,7 @@ export async function cacheAdditionalProcessors(cacheName: string, buildAssets: 'process-', ]; - const prefixMatches = buildAssets.filter( - asset => prefixesToCache.some(prefix => asset.startsWith(prefix)), - ); - + const prefixMatches = getAssetsWithPrefix(buildAssets, prefixesToCache); const wasm = buildAssets.filter(asset => asset.endsWith('.wasm')); toCache.push(...prefixMatches, ...wasm);