async function handleRequest(event) {
const { request } = event;
// POST请求不缓存
if (request.method.toUpperCase() === 'POST')
return await fetch(request);
// 创建cacheKey
let cacheUrl = new URL(request.url);
let cacheKey = new Request(cacheUrl, request);
let cache = caches.default;
// 查找是否该cacheKey已缓存
let response = await cache.match(cacheKey);
// 如果没缓存则加入到缓存
if (!response) {
response = await fetch(request);
response = new Response(response.body, response);
// 添加过期时间
response.headers.append('Cache-Control', 'max-age=10');
// 加入到缓存
event.waitUntil(cache.put(cacheKey, response.clone()));
}
return response;
}
addEventListener('fetch', event => {
return event.respondWith(handleRequest(event));
});
使用边缘计算缓存
暂无讨论,说说你的看法吧