服务人员不更新

Onz*_*nza 15 javascript web-worker service-worker

我在我的网站上安装了一个服务工作者,一切正常,除非我将更新推送到缓存文件,实际上; 他们永远被抓住了,除非我从`chrome:// serviceworker-internals /取消订阅工作人员,否则我似乎无法使缓存无效

const STATIC_CACHE_NAME = 'static-cache-v1';
const APP_CACHE_NAME = 'app-cache-#VERSION';

const CACHE_APP = [
    '/',
    '/app/app.js'
]
const CACHE_STATIC = [
    'https://fonts.googleapis.com/css?family=Roboto:400,300,500,700',
    'https://cdnjs.cloudflare.com/ajax/libs/normalize/4.1.1/normalize.min.css'
]

self.addEventListener('install',function(e){
    e.waitUntil(
        Promise.all([caches.open(STATIC_CACHE_NAME),caches.open(APP_CACHE_NAME)]).then(function(storage){
            var static_cache = storage[0];
            var app_cache = storage[1];
            return Promise.all([static_cache.addAll(CACHE_STATIC),app_cache.addAll(CACHE_APP)]);
        })
    );
});

self.addEventListener('activate', function(e) {
    e.waitUntil(
        caches.keys().then(function(cacheNames) {
            return Promise.all(
                cacheNames.map(function(cacheName) {
                    if (cacheName !== APP_CACHE_NAME && cacheName !== STATIC_CACHE_NAME) {
                        console.log('deleting',cacheName);
                        return caches.delete(cacheName);
                    }
                })
            );
        })
    );
});

self.addEventListener('fetch',function(e){
    const url = new URL(e.request.url);
    if (url.hostname === 'static.mysite.co' || url.hostname === 'cdnjs.cloudflare.com' || url.hostname === 'fonts.googleapis.com'){
        e.respondWith(
            caches.match(e.request).then(function(response){
                if (response) {
                    return response;
                }
                var fetchRequest = e.request.clone();

                return fetch(fetchRequest).then(function(response) {
                    if (!response || response.status !== 200 || response.type !== 'basic') {
                        return response;
                    }
                    var responseToCache = response.clone();
                    caches.open(STATIC_CACHE_NAME).then(function(cache) {
                        cache.put(e.request, responseToCache);
                    });
                    return response;
                });
            })
        );
    } else if (CACHE_APP.indexOf(url.pathname) !== -1){
        e.respondWith(caches.match(e.request));
    }
});
Run Code Online (Sandbox Code Playgroud)

其中#VERSION是在编译时附加到缓存名称的版本; 请注意,STATIC_CACHE_NAME永远不会更改,因为文件被认为是永久静态的.

此外,行为是不稳定的,我一直在检查删除功能(它记录的部分),并一直记录已删除的删除缓存(据称).当我跑步时,caches.keys().then(function(k){console.log(k)})我得到了一堆本应被删除的旧缓存.

Onz*_*nza 17

在谷歌搜索并观看一些关于udacity的视频后,我发现工作人员的预期行为是留在它控制的页面关闭,并在新服务工作者可以控制时再次重新打开.

解决方案是强制它控制基于https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/skipWaiting以下解决了问题,即使需要2次重新加载才能进行新工作者反映变化(这是有道理的,因为在新工人替换之前的应用程序之前加载了应用程序).

self.addEventListener('install',function(e){
    e.waitUntil(
        Promise.all([caches.open(STATIC_CACHE_NAME),caches.open(APP_CACHE_NAME),self.skipWaiting()]).then(function(storage){
            var static_cache = storage[0];
            var app_cache = storage[1];
            return Promise.all([static_cache.addAll(CACHE_STATIC),app_cache.addAll(CACHE_APP)]);
        })
    );
});

self.addEventListener('activate', function(e) {
    e.waitUntil(
        Promise.all([
            self.clients.claim(),
            caches.keys().then(function(cacheNames) {
                return Promise.all(
                    cacheNames.map(function(cacheName) {
                        if (cacheName !== APP_CACHE_NAME && cacheName !== STATIC_CACHE_NAME) {
                            console.log('deleting',cacheName);
                            return caches.delete(cacheName);
                        }
                    })
                );
            })
        ])
    );
});
Run Code Online (Sandbox Code Playgroud)

  • 记住你可以在promise之外使用`self.skipWaiting()`. (3认同)

Bru*_*our 7

作为“服务工作新手”,我遇到了一种相关情况,即即使在 Chrome Canary 上启用了“JavaScript 控制台>应用程序>重新加载时更新”,服务工作人员也不会刷新。

问题是我的代码中有工作代码/sw/index.js,然后我向/sw/index.js. 当我引入错误时,浏览器拒绝加载更新的代码并继续加载早期工作的服务工作线程。当我更正代码index.js并刷新页面时,出现了服务工作人员的新代码。我本以为错误填充的代码会抛出错误,但事实并非如此。浏览器刚刚加载了早期的无错误版本。