简体   繁体   English

离线时 start_url 不响应 200:start_url 确实响应,但不是通过服务工作者响应。 灯塔审计问题

[英]start_url does not respond with a 200 when offline: The start_url did respond, but not via a service worker. Lighthouse Audit problem

I am creating a PWA that works offline with a service worker.我正在创建一个与服务工作者脱机工作的 PWA。

Right now it works correctly, but there is a problem in Lighthouse Audit.现在它可以正常工作,但是 Lighthouse Audit 中存在问题。

When I run Lighthouse, in the PWA section I get this problem: start_url does not respond with a 200 when offline The start_url did respond, but not via a service worker.当我运行 Lighthouse 时,在 PWA 部分我遇到了这个问题:离线时 start_url 不响应 200 start_url 确实响应,但不是通过服务工作者响应。

How do I pass that audit, even if there are other audits that say that I have successfully installed a service worker?即使有其他审核表明我已成功安装 Service Worker,我该如何通过该审核?

My website is here: https://nariohtools.com and the service worker is here: https://nariohtools.com/sw.js我的网站在这里: https : //nariohtools.com ,服务工作者在这里: https : //nariohtools.com/sw.js

Thanks in advance.提前致谢。

The related code is here:相关代码在这里:

caches.open(CACHE_NAME).then((cache) => {
  return fetch(evt.request)

You are opening the cache but you're not using the cached response and the request is forwarded to the network:您正在打开缓存,但没有使用缓存的响应,并且请求被转发到网络:

Use something like this instead:使用类似这样的东西:

caches.open(CACHE_NAME).then(cache => {
  return cache.match(evt.request).then(cacheResponse => cacheResponse || fetch(evt.request).then(networkResponse => {
  cache.put(evt.request, networkResponse.clone());
  return networkResponse;
}));

以防万一,Lighthouse 中有一个错误已在 Chrome 89 版中修复。https://github.com/antfu/vite-plugin-pwa/issues/20#issuecomment-773019940

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM