微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Service Worker 在 Chrome 中不再按预期工作

如何解决Service Worker 在 Chrome 中不再按预期工作

再次像其他一千次一样,我更新了我的服务工作者文件版本,以检查任何错误,我打开了 Chrome(浏览器)开发人员工具,我看到了......以一种新的方式获取工作......错误Fetch finished loading: GET "https://www.example.com/favicon.ico". etc...some more CSS and image,I don't kNow what this is: Fetch Failed loading: GET "https://www.example.com/".(控制台日志的最后一行)

为什么每次都需要请求域顶级根... 现在我检查标题(DEV 工具 - 网络 - 标题),因为网络状态 = (Failed)

Request URL: https://www.example.com/
Referrer Policy: unsafe-url

几乎没有标题信息或任何内容???

如果我使用预加载,它会以红色 (The service worker navigation preload request Failed with network error:net::ERR_INTERNET_disCONNECTED) 显示额外的 ERROR,因此我暂时禁用了预加载,Service Worker 仍会处理此错误

我刚刚更新到 PHP 8.0,所以也许这是在做一些事情,但在回到旧版本后没有任何改变。也许我的服务器开始阻止某种请求,但这不太可能,更像是来自 Chrome 服务工作者的错误请求。

如果 Chrome 尝试检查最后一个请求的某种离线容量,我会在获取错误显示一个离线页面,如果这与此有关。

无论如何,尽管存在上述问题/错误,Service Worker 仍能正常工作。

无论如何,这里是软件代码示例:

    const OFFLINE_VERSION = 1;
var fiLevers='xxxx';
const CACHE_NAME = 'offline'+fiLevers;
// Customize this with a different URL if needed.
const OFFLINE_URL = 'offlineurl.PHP';
const OFFLINE_URL_ALL = [
  'stylesheet'+fiLevers+'.css','offlineurl.PHP','favicon.ico','img/logo.png'
].map(url => new Request(url,{credentials: 'include'}));
self.addEventListener('install',(event) => {
  event.waitUntil((async () => {
    const cache = await caches.open(CACHE_NAME);
    // Setting {cache: 'reload'} in the new request will ensure that the response
    // isn't fulfilled from the HTTP cache; i.e.,it will be from the network.
  await cache.addAll(OFFLINE_URL_ALL);
  })());
});

self.addEventListener('activate',(event) => {
  event.waitUntil((async () => {
    // Enable navigation preload if it's supported.
    // See https://developers.google.com/web/updates/2017/02/navigation-preload
//removed for Now
  })());

  // Tell the active service worker to take control of the page immediately.
  self.clients.claim();
});

self.addEventListener('fetch',(event) => {
  // We only want to call event.respondWith() if this is a navigation request
  // for an HTML page.
            const destination = event.request.destination;
if (destination == "style" ||  destination == "script" ||  destination == "document" ||  destination == "image" ||  destination == "font") {    
    event.respondWith((async () => {
      try {
           const cache = await caches.open(CACHE_NAME);
        const cachedResponse = await cache.match(event.request);
         if (cachedResponse) {
             return cachedResponse;
             } else {
        // First,try to use the navigation preload response if it's supported.
//removed for Now
                const networkResponse = await fetch(event.request);
        return networkResponse;
         
        } 

      } catch (error) {
           if (event.request.mode === 'navigate') {
        // catch is only triggered if an exception is thrown,which is likely
        // due to a network error.
        // If fetch() returns a valid HTTP response with a response code in
        // the 4xx or 5xx range,the catch() will NOT be called.

        const cache = await caches.open(CACHE_NAME);
        const cachedResponse = await cache.match(OFFLINE_URL);
        return cachedResponse;
        }
      }
    })());
}

});

任何建议,可能导致错误的原因是什么?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。