mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-14 17:49:52 +00:00
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' }));
|
return blobToImg(new Blob([newSource], { type: 'image/svg+xml' }));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getOldestServiceWorker() {
|
async function getMostActiveServiceWorker() {
|
||||||
const reg = await navigator.serviceWorker.getRegistration();
|
const reg = await navigator.serviceWorker.getRegistration();
|
||||||
if (!reg) return null;
|
if (!reg) return null;
|
||||||
return reg.active || reg.waiting || reg.installing;
|
return reg.active || reg.waiting || reg.installing;
|
||||||
@@ -209,7 +209,7 @@ export default class Compress extends Component<Props, State> {
|
|||||||
.then(async (userInteracted: boolean | undefined) => {
|
.then(async (userInteracted: boolean | undefined) => {
|
||||||
if (userInteracted) return;
|
if (userInteracted) return;
|
||||||
set('user-interacted', true);
|
set('user-interacted', true);
|
||||||
const serviceWorker = await getOldestServiceWorker();
|
const serviceWorker = await getMostActiveServiceWorker();
|
||||||
if (!serviceWorker) return; // Service worker not installing yet.
|
if (!serviceWorker) return; // Service worker not installing yet.
|
||||||
serviceWorker.postMessage('cache-all');
|
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[]) {
|
export async function cacheBasics(cacheName: string, buildAssets: string[]) {
|
||||||
const toCache = ['/', '/assets/favicon.ico'];
|
const toCache = ['/', '/assets/favicon.ico'];
|
||||||
|
|
||||||
@@ -60,9 +66,7 @@ export async function cacheBasics(cacheName: string, buildAssets: string[]) {
|
|||||||
'logo.',
|
'logo.',
|
||||||
];
|
];
|
||||||
|
|
||||||
const prefixMatches = buildAssets.filter(
|
const prefixMatches = getAssetsWithPrefix(buildAssets, prefixesToCache);
|
||||||
asset => prefixesToCache.some(prefix => asset.startsWith(prefix)),
|
|
||||||
);
|
|
||||||
|
|
||||||
toCache.push(...prefixMatches);
|
toCache.push(...prefixMatches);
|
||||||
|
|
||||||
@@ -80,10 +84,7 @@ export async function cacheAdditionalProcessors(cacheName: string, buildAssets:
|
|||||||
'process-',
|
'process-',
|
||||||
];
|
];
|
||||||
|
|
||||||
const prefixMatches = buildAssets.filter(
|
const prefixMatches = getAssetsWithPrefix(buildAssets, prefixesToCache);
|
||||||
asset => prefixesToCache.some(prefix => asset.startsWith(prefix)),
|
|
||||||
);
|
|
||||||
|
|
||||||
const wasm = buildAssets.filter(asset => asset.endsWith('.wasm'));
|
const wasm = buildAssets.filter(asset => asset.endsWith('.wasm'));
|
||||||
|
|
||||||
toCache.push(...prefixMatches, ...wasm);
|
toCache.push(...prefixMatches, ...wasm);
|
||||||
|
|||||||
Reference in New Issue
Block a user