Service Worker 可以在获取处理程序中使用 waitUntil 处理并发请求吗?
Can a service worker handle concurrent requests with waitUntil in the fetch handler?
我正在尝试使用缓存优先策略实现 PWA,该策略还尝试通过在 waitUntil() 中获取来更新缓存资产。如果有多个请求在(几乎)同时开始,这会阻塞并使副线程并发吗?
这是我的代码:
self.addEventListener("fetch", (oEvent) => { oEvent.respondWith(
caches.match(oEvent.request).then((oRes) => {
if (oRes) {
oEvent.waitUntil(fetch(oEvent.request)
.then((oFetchRes) => {
return caches.open(DYNAMIC_CACHE).then((oCache) => {
oCache.put(oEvent.request.url, oFetchRes);
});
}))
return oRes
} else {
return fetch(oEvent.request)
.then((oFetchRes) => {
return caches.open(DYNAMIC_CACHE).then((oCache) => {
oCache.put(oEvent.request.url, oFetchRes.clone());
return oFetchRes;
});
})
.catch(() => {
return new Response(JSON.stringify({}), {
status: 503,
statusText: "app_offline_and_missing_resource",
});
})
}
}) );});
欢迎任何帮助,我还是一个 PWA 新手。
仔细阅读 https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#the_cache_machine_-_when_to_store_resources 文章后,我得到了想要的答案:stale-while-revalidate 方法正是我所寻找的。
我正在尝试使用缓存优先策略实现 PWA,该策略还尝试通过在 waitUntil() 中获取来更新缓存资产。如果有多个请求在(几乎)同时开始,这会阻塞并使副线程并发吗?
这是我的代码:
self.addEventListener("fetch", (oEvent) => { oEvent.respondWith(
caches.match(oEvent.request).then((oRes) => {
if (oRes) {
oEvent.waitUntil(fetch(oEvent.request)
.then((oFetchRes) => {
return caches.open(DYNAMIC_CACHE).then((oCache) => {
oCache.put(oEvent.request.url, oFetchRes);
});
}))
return oRes
} else {
return fetch(oEvent.request)
.then((oFetchRes) => {
return caches.open(DYNAMIC_CACHE).then((oCache) => {
oCache.put(oEvent.request.url, oFetchRes.clone());
return oFetchRes;
});
})
.catch(() => {
return new Response(JSON.stringify({}), {
status: 503,
statusText: "app_offline_and_missing_resource",
});
})
}
}) );});
欢迎任何帮助,我还是一个 PWA 新手。
仔细阅读 https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#the_cache_machine_-_when_to_store_resources 文章后,我得到了想要的答案:stale-while-revalidate 方法正是我所寻找的。