概述压缩变体在缓存与匹配中需要区分。本文给出前端键命名与服务端 Vary 协作建议。前端命名async function cacheByEncoding(event) {
const res = await fetch(event.request);
const enc = (res.headers.get('Content-Encoding') || 'identity');
const cache = await caches.open('enc-v1');
const url = new URL(event.request.url);
const key = new Request(url.origin + url.pathname + '?enc=' + enc, { headers: event.request.headers });
await cache.put(key, res.clone());
return res;
}

发表评论 取消回复