From 4e5a8107704aa66873f3f01d8348285f69fde069 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Tue, 25 Aug 2020 14:31:20 +0100 Subject: [PATCH] Avoid caching the decoder if the browser already supports it --- src/sw/util.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/sw/util.ts b/src/sw/util.ts index 8faa1020..6a5fb7cc 100644 --- a/src/sw/util.ts +++ b/src/sw/util.ts @@ -1,4 +1,5 @@ import webpDataUrl from 'url-loader!../codecs/tiny.webp'; +import avifDataUrl from 'url-loader!../codecs/tiny.avif'; // Give TypeScript the correct global. declare var self: ServiceWorkerGlobalScope; @@ -109,17 +110,20 @@ export async function cacheAdditionalProcessors(cacheName: string, buildAssets: toCache.push(...prefixMatches, ...wasm); - const supportsWebP = await (async () => { - if (!self.createImageBitmap) return false; - const response = await fetch(webpDataUrl); - const blob = await response.blob(); - return createImageBitmap(blob).then(() => true, () => false); - })(); + const [supportsWebP, supportsAvif] = await Promise.all( + [webpDataUrl, avifDataUrl].map(async (dataUrl) => { + if (!self.createImageBitmap) return false; + const response = await fetch(dataUrl); + const blob = await response.blob(); + return createImageBitmap(blob).then(() => true, () => false); + }), + ) - // No point caching the WebP decoder if the browser supports it: - if (supportsWebP) { - toCache = toCache.filter(asset => !/webp[\-_]dec/.test(asset)); - } + // No point caching decoders the browser already supports: + toCache = toCache.filter(asset => + (supportsWebP ? !/webp[\-_]dec/.test(asset) : true) && + (supportsAvif ? !/avif[\-_]dec/.test(asset) : true), + ); const cache = await caches.open(cacheName); await cache.addAll(toCache);