简体   繁体   English

避免使用service worker缓存start_url

[英]Avoid caching start_url with service worker

I'm having some problems on setting up a service worker for my website. 我在为网站设置服务人员时遇到一些问题。

I only want to cache css/js/fonts and some images/svg, I don't want to cache the HTML since all of it is updated every minute. 我只想缓存css / js / fonts和一些图像/ svg,我不想缓存HTML,因为所有内容每分钟都会更新。

It kinda works, but trying on my smartphone I keep getting the notification "Add to homescreen" even when I've already added it. 有点用,但是即使在我已经添加了智能手机的情况下,我仍然尝试在智能手机上不断收到“添加到主屏幕”通知。 And on the Chrome Dev app I don't get the Add button. 在Chrome Dev应用程序上,我没有“添加”按钮。

Also with the Lighthouse I get the following errors: 同样,在Lighthouse上,我得到以下错误:

"Does not respond with a 200 when offline" “脱机时不响应200”

"User will not be prompted to Install the Web App, Failures: Manifest start_url is not cached by a Service Worker." “不会提示用户安装Web应用程序,失败:Service Worker不会缓存清单start_url。”

Right now my sw.js is like this. 现在我的sw.js就是这样。 As you can see I commented the fetch part because it was caching the HTML and also the Cookies weren't working. 如您所见,我评论了fetch部分,因为它正在缓存HTML,并且Cookies也不起作用。

Is there around a simple Service Worker "template" to use? 是否有一个简单的Service Worker“模板”可供使用?

const PRECACHE = 'app-name';
const RUNTIME = 'runtime';

// A list of local resources we always want to be cached.
const PRECACHE_URLS = [
'/css/file.css',
'/js/file.js',
'/images/logo.png',
'/fonts/roboto/Roboto-Regular.woff2'
]

// The install handler takes care of precaching the resources we always need.
self.addEventListener('install', event => {
  event.waitUntil(
    caches.open(PRECACHE)
      .then(cache => cache.addAll(PRECACHE_URLS))
      .then(self.skipWaiting())
  );
});

// The activate handler takes care of cleaning up old caches.
self.addEventListener('activate', event => {
  const currentCaches = [PRECACHE, RUNTIME];
  event.waitUntil(
    caches.keys().then(cacheNames => {
      return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
    }).then(cachesToDelete => {
      return Promise.all(cachesToDelete.map(cacheToDelete => {
        return caches.delete(cacheToDelete);
      }));
    }).then(() => self.clients.claim())
  );
});

// The fetch handler serves responses for same-origin resources from a cache.
// If no response is found, it populates the runtime cache with the response
// from the network before returning it to the page.
self.addEventListener('fetch', event => {
  // Skip cross-origin requests, like those for Google Analytics.
  // if (event.request.url.startsWith(self.location.origin)) {
  //   event.respondWith(
  //     caches.match(event.request).then(cachedResponse => {
  //       if (cachedResponse) {
  //         return cachedResponse;
  //       }

  //       return caches.open(RUNTIME).then(cache => {
  //         return fetch(event.request).then(response => {
  //           // Put a copy of the response in the runtime cache.
  //           return cache.put(event.request, response.clone()).then(() => {
  //             return response;
  //           });
  //         });
  //       });
  //     })
  //   );
  // }
});    

I'm not sure why the install banner appears but the two errors given by lighthouse are related to the missing caching of the very start_url, propably index.html. 我不确定为什么会出现安装横幅,但是lighthouse给出的两个错误与缺少start_url(可能是index.html)的缓存有关。 So Lighthouse will always be telling you about those if you follow the caching strategy you described here. 因此,如果您遵循此处介绍的缓存策略,那么Lighthouse将始终告诉您有关这些内容的信息。

I suggest you could try Workbox and their runtime caching. 我建议您可以尝试Workbox及其运行时缓存。 Runtime caching, in a nutshell, works like so: you specify urls like *.svg, *.css etc. and the SW caches them once the client first asks them. 简而言之,运行时缓存的工作原理是这样的:您指定诸如* .svg,*。css等的url,一旦客户端首次询问它们,SW就会对其进行缓存。 In the future, when the files are already cached, the SW serves them from the cache to the client. 将来,当文件已经被缓存时,SW将从缓存中将它们提供给客户端。 Basically you tell the SW to cache this and that kind of urls when it encounters them and not in advance. 基本上,您告诉SW 在遇到此类URL 缓存它们,而不是事先缓存它们

Runtime caching could very well be accompanied by precaching (that may also be found from Workbox!) to cache a bunch of files. 运行时缓存可能很好地伴随着预缓存(也可以从Workbox中找到)来缓存一堆文件。

Check it out here: https://workboxjs.org 在这里查看: https : //workboxjs.org

They have a couple of examples and plugins for build tooling. 他们有一些用于构建工具的示例和插件。

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

相关问题 渐进式 Web 应用程序 - Service Worker 不提供 start_URL - Progressive Web App - Service Worker not serving start_URL 离线时 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 创建 React App PWA 错误:未注册控制页面和 start_url 的服务工作者 - Create React App PWA ERROR: Does not register a service worker that controls page and start_url 如何检测服务工作者获取请求是否为清单的start_url? - How do I detect in a service worker fetch that the request is for the manifest's start_url? PWA “start_url 确实响应了,但不是通过服务工作者。” — NextJS-PWA - PWA “The start_url did respond, but not via a service worker.” — NextJS-PWA Lighthouse PWA 审核失败“未注册控制页面和 start_url 的服务工作者” - Lighthouse PWA Audit fail "does not register a service worker that controls page and start_url" 如何设置 start_url 和服务工作不正确 - How to set start_url and service work not correct PWA 通用 start_url - PWA generic start_url Service Worker - 图片缓存 - Service Worker - Image Caching Service Worker 没有缓存 - Service worker is not caching
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM