简体   繁体   English

如何缓存胸围sw-toolbox?

[英]How to cache bust sw-toolbox?

I have been toying around with service workers and sw-toolbox. 我一直在与服务工作者和sw-toolbox一起玩弄。 Both are great methods but seems to have their weaknesses. 两者都是很好的方法,但似乎有它们的弱点。

My project started out using Google's method of service workers ( link ). 我的项目开始使用谷歌的服务工作者方法( 链接 )。 The way I see this is that you have to manually update the version number for cache busting. 我看到的方式是你必须手动更新缓存清除的版本号。 I could be wrong also but I don't think the pages that the users has visited will not be cached. 我也错了,但我不认为用户访问过的网页不会被缓存。

Compared to the sw-toolbox method, all I need to add is the following code: 与sw-toolbox方法相比,我需要添加的是以下代码:

self.toolbox.router.default = self.toolbox.networkFirst;

self.toolbox.router.get('/(.*)', function (req, vals, opts) {
    return self.toolbox.networkFirst(req, vals, opts)
        .catch(function (error) {
            if (req.method === 'GET' && req.headers.get('accept').includes('text/html')) {
                return self.toolbox.cacheOnly(new Request(OFFLINE_URL), vals, opts);
            }
            throw error;
        });
});

Then the problem of caching pages will be solved. 然后将解决缓存页面的问题。 Here is my issue: after applying the sw-toolbox to my project, the old service worker doesn't get cleared or replaced by the new one unless I go to the dev tools to clear it. 这是我的问题:在将sw-toolbox应用到我的项目之后,旧的服务工作者不会被新的服务工作者清除或替换,除非我去开发工具清除它。

Any ideas how to get around this? 任何想法如何解决这个问题?

Here is my issue: after applying the sw-toolbox to my project, the old service worker doesn't get cleared or replaced by the new one unless I go to the dev tools to clear it. 这是我的问题:在将sw-toolbox应用到我的项目之后,旧的服务工作者不会被新的服务工作者清除或替换,除非我去开发工具清除它。

The browser checks for updates to the service worker file every time it requests a resource in the service worker scope. 每次请求服务工作者作用域中的资源时,浏览器都会检查服务工作文件的更新。 If there is a byte difference in the service worker files, the browser will install the new service worker. 如果服务工作程序文件中存在字节差异,则浏览器将安装新的服务工作程序。 You only need to update the service worker manually in dev tools because the app is still running, and the browser does not want to activate a new service worker while the old one is still in use. 您只需要在开发工具中手动更新服务工作者,因为该应用程序仍在运行,并且浏览器不希望在旧服务工作者仍在使用时激活它。

If you close all pages associated with the service worker (like a user would when leaving your app), the browser will be able to activate the new service worker the next time your page is opened. 如果您关闭与服务工作者关联的所有页面(就像用户离开您的应用程序时那样),浏览器将能够在您下次打开页面时激活新的服务工作者。

If you want to force the new service worker to take over, you can add self.skipWaiting(); 如果要强制新服务工作者接管,可以添加self.skipWaiting(); to the install event. install事件。 Here is some documentation with an example . 这是一些带有示例的文档

You can learn just about everything you need to know about the service worker's life cycle from this post by Jake Arichbald . 您可以从Jake Arichbald的这篇文章中了解您需要了解的有关服务工作者生命周期的所有信息

As far as caching & cache management goes, tools like sw-toolbox will handle cache busting for you. 就缓存和缓存管理而言,像sw-toolbox这样的工具将为您处理缓存清除。 And actually, Workbox is a new tool that is meant to replace sw-toolbox & sw-precache. 实际上, Workbox是一种新工具,旨在取代sw-toolbox和sw-precache。 It will also handle cache busting and cache management (by comparing file hashes & setting/tracking resource expiration dates). 它还将处理缓存清除和缓存管理(通过比较文件哈希和设置/跟踪资源到期日期)。

Generally speaking, you should always use a tool like Workbox to write your service workers. 一般来说,您应该始终使用像Workbox这样的工具来编写服务工作者。 Writing them by hand is error prone and you are likely to miss corner cases. 手写它们很容易出错,你可能会错过角落案例。

Hope that helps. 希望有所帮助。

PS If you end up not using skipWaiting and instead only updating when the page is closed & re-opened by a user, you can still enable automatic updating for development. PS如果您最终没有使用skipWaiting而只是在用户关闭并重新打开页面时进行更新,您仍然可以启用自动更新以进行开发。 In Chrome's dev tools, Application > Service Workers has an Update on reload option to automatically update the service worker. 在Chrome的开发工具中,“ 应用程序”>“服务工作者”具有“重新加载更新”选项,可自动更新服务工作者。

I don't know if sw_toolbox has cache busting built in. Typically when you change the service worker and need to purge the previous version's cache you should do that with in the activate event handler. 我不知道sw_toolbox是否内置了缓存清除。通常当您更改服务工作者并需要清除以前版本的缓存时,您应该在activate事件处理程序中执行此操作。

The best practice here is to name your caches with the sw version number included. 这里的最佳做法是为包含sw版本号的缓存命名。 Here is some example code from an online course I have on service worker caching that might get you started: 以下是我在服务工作者缓存上的在线课程中的一些示例代码,可以帮助您入门:

self.addEventListener("activate", event => {

console.log("service worker activated");

//on activate
event.waitUntil(caches.keys()
    .then(function (cacheNames) {

        cacheNames.forEach(function (value) {

            if (value.indexOf(config.version) < 0) {

                caches.delete(value);

            }

        });

        return;

    })
);

}); });

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

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