Skip to content

Make images smaller using best-in-class codecs, right in the browser.

JavaScript

Avoid caching the decoder if the browser already supports it

Jake Archibald4e5a8107704a
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);
- })();
-
- // No point caching the WebP decoder if the browser supports it:
- if (supportsWebP) {
- toCache = toCache.filter(asset => !/webp[\-_]dec/.test(asset));
- }
+ 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 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);