简体   繁体   English

渐进式Web应用程序上的缓存版本控制

[英]Cache Versioning on Progressive Web Apps

What is the current best practice for service worker caching? 目前服务工作者缓存的最佳实践是什么?

After using service workers and caching for a number of projects, we are having a bit of a maintenance headache to manage the cached assets for the project. 在使用服务工作者和缓存许多项目之后,我们在管理项目的缓存资产时遇到了一些维护问题。

In most cases we are using a static cache (most likely the cause of our headache) and our sw.js code is pretty standard and looks as follows: 在大多数情况下,我们使用静态缓存(很可能是我们头疼的原因),我们的sw.js代码非常标准,如下所示:

var cacheName = 'v1:static';

// cache static assets during install phase
self.addEventListener('install', function (e) {
    e.waitUntil(
        caches.open(cacheName).then(function (cache) {
            return cache.addAll([
                './',
                './css/style.css',
                './css/vendor.css',
                './js/build/script.min.js',
                './js/build/vendor.min.js'
            ]).then(function () {
                self.skipWaiting();
            });
        })
    );
});

self.addEventListener('fetch', function (event) {        
    event.respondWith(
        caches.match(event.request).then(function (response) {
            if (response) {
                // retrieve from cache
                return response;
            }
            // fetch as normal
            return fetch(event.request);
        })
    );
});

The problems begin once we release a new version of our app and we need to invalidate the cache for the user. 一旦我们发布了新版本的应用程序并且我们需要使用户的缓存无效,问题便开始了。

What methods are recommended to update the cache? 建议使用哪些方法来更新缓存? I am aware of sw-precache or developers who manually update the cacheName variable (or perhaps through a task-runner). 我知道sw-precache或手动更新cacheName变量的开发人员(或者可能通过任务运行器)。 Are there other alternatives? 还有其他选择吗? Is there a way to only invalidate one file instead of the entire cache? 有没有办法只使一个文件而不是整个缓存失效?

Thanks 谢谢

I suggest that you do an assets assessment of your project(s). 我建议您对项目进行资产评估。 Determine the static assets from the dynamic ones. 从动态资产中确定静态资产。 Once you have determined the pure static ones, precache it. 一旦你确定了纯静态的,就预先解决它。

There are 2 ways of doing this. 有两种方法可以做到这一点。 1) use sw-toolbox or 2) manually update the version number of the service worker file for the browser to update it. 1)使用sw-toolbox或2)手动更新服务工作文件的版本号,以便浏览器更新它。

I know the pain that you are experiencing with invalidating the cache and everything. 我知道您在缓存和所有内容失效时遇到的痛苦。 You might want to try/implement sw-toolbox . 您可能想尝试/实现sw-toolbox

sw-toolbox method sw-toolbox方法

const OFFLINE_URL = '/pages/offline/';

importScripts('sw-toolbox.js');

self.toolbox.precache([
    <PATH_TO_STATIC_ASSET>
    'manifest.json',
    OFFLINE_URL,
    '/'
]);

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;
        });
    });

Google Service Worker Boilerplate - link Google Service Worker Boilerplate - 链接

This one too is pretty straight forward. 这个也很直接。 The only down side of this is that at every deployment, you have to update the CACHE_VERSION for the service-worker installed to update automatically. 唯一CACHE_VERSION是,在每次部署时,您都必须为安装的服务工作者更新CACHE_VERSION以自动更新。

Take note also that there is Workbox also. 另请注意,还有Workbox I havent played around with it that much yet so my methods could be outdated. 我还没玩过那么多,所以我的方法可能已经过时了。 They say that this is the descendant to precache and toolbox 他们说这是预先缓存和工具箱的后代

我认为最简单的方法是更新服务工作者JS文件中的CACHE_NAME,以便再次安装。

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

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