forked from external-repos/squoosh
Addressing nits from service worker PR.
This commit is contained in:
@@ -156,7 +156,7 @@ async function processSvg(blob: Blob): Promise<HTMLImageElement> {
|
||||
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<Props, State> {
|
||||
.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');
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user