概述清单与哈希命名能确保缓存一致性。本文提供校验与缓存写入示例。清单校验与写入async function sha256(bytes) { const buf = await crypto.subtle.digest('SHA-256', bytes); return Array.from(new Uint8Array(buf)).map(x=>x.toString(16).padStart(2,'0')).join(''); }

async function cacheWithManifest(manifest, request) {

const res = await fetch(request);

const ab = await res.clone().arrayBuffer();

const hex = await sha256(new Uint8Array(ab));

const url = new URL(request.url);

const key = `${url.pathname}?v=${hex}`;

if (manifest[url.pathname] !== hex) throw new Error('mismatch');

const cache = await caches.open('assets-hash');

await cache.put(new Request(key), res.clone());

return res;

}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部