Service Worker 可以在获取处理程序中使用 waitUntil 处理并发请求吗?

我正在尝试使用缓存优先策略实现 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 新手。


慕田峪7331174
浏览 102回答 1
1回答

饮歌长啸

仔细阅读https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#the_cache_machine_-_when_to_store_resources文章让我得到了想要的答案:stale-while-revalidate 方法是什么我在寻找。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript