Avoid caching the decoder if the browser already supports it

This commit is contained in:
Jake Archibald
2020-08-25 14:31:20 +01:00
parent 8a81792bd5
commit 4e5a810770

View File

@@ -1,4 +1,5 @@
import webpDataUrl from 'url-loader!../codecs/tiny.webp'; import webpDataUrl from 'url-loader!../codecs/tiny.webp';
import avifDataUrl from 'url-loader!../codecs/tiny.avif';
// Give TypeScript the correct global. // Give TypeScript the correct global.
declare var self: ServiceWorkerGlobalScope; declare var self: ServiceWorkerGlobalScope;
@@ -109,17 +110,20 @@ export async function cacheAdditionalProcessors(cacheName: string, buildAssets:
toCache.push(...prefixMatches, ...wasm); toCache.push(...prefixMatches, ...wasm);
const supportsWebP = await (async () => { const [supportsWebP, supportsAvif] = await Promise.all(
if (!self.createImageBitmap) return false; [webpDataUrl, avifDataUrl].map(async (dataUrl) => {
const response = await fetch(webpDataUrl); if (!self.createImageBitmap) return false;
const blob = await response.blob(); const response = await fetch(dataUrl);
return createImageBitmap(blob).then(() => true, () => false); const blob = await response.blob();
})(); return createImageBitmap(blob).then(() => true, () => false);
}),
)
// No point caching the WebP decoder if the browser supports it: // No point caching decoders the browser already supports:
if (supportsWebP) { toCache = toCache.filter(asset =>
toCache = toCache.filter(asset => !/webp[\-_]dec/.test(asset)); (supportsWebP ? !/webp[\-_]dec/.test(asset) : true) &&
} (supportsAvif ? !/avif[\-_]dec/.test(asset) : true),
);
const cache = await caches.open(cacheName); const cache = await caches.open(cacheName);
await cache.addAll(toCache); await cache.addAll(toCache);