对 Service Worker 的长时间等待请求 [英] Long time waiting Request to Service Worker

查看:49
本文介绍了对 Service Worker 的长时间等待请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到等待 Service Worker 响应缓存中的项目的时间并不像您预期​​的那么快.我在 sw-precache 和自定义编写的 Service Worker 中看到了相同的等待时间.

I have noticed that the time waiting for service workers to respond with items from the cache is not as fast as you would expect it to be. I have seen the same wait times with both sw-precache and a custom written service worker.

这个等待时间的可能原因是什么,我该如何减少它?

What are the possible causes for this wait time time and how could I reduce it?

我在自定义 Service Worker 上的 fetch 事件如下所示:

My fetch event on the custom service worker looks like:

self.addEventListener('fetch', function(event) {
    event.respondWith(
        caches.match(event.request).then(function(response) {
            if (response) {
                return response;
            }
            return fetch(event.request);
        })
    );
});

推荐答案

您是否在 Chrome Dev Tools 中的 Application -> Service Worker 选项卡下选中了Update on reload"?

Do you have 'Update on reload' checked within your Chrome Dev Tools under the Application -> Service Worker tab?

如果是这样,那么我认为这可能是问题所在,因为它会重新运行您所有的 Service Worker 代码,当使用 sw-precache 时,这些代码可能是相当多的代码,在每个重新加载.

If so then I think this may be the problem as it'll be re-running all your Service Worker code, which can be quite a lot of code when using sw-precache, on each reload.

这篇关于对 Service Worker 的长时间等待请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆