如何防止服务工作者删除缓存,PWA

达格·M。

我已经使用Vue,js,节点技术构建了启用PWA的SPA。问题是,每当我关闭PC时,服务工作者都会删除我存储的缓存以脱机查看应用程序,否则它将起作用。第二个问题是,除非我在浏览器中启用CORS,否则它拒绝获取包含Google字体的数据。由于PWA基于浏览器,并且用户将不会在浏览器中安装CORS附加组件,是否有某种方法可以在(Windows)服务器上启用CORS?预先感谢,这是我的服务人员代码。

// service worker file. Every time when you change this file rename staticCache const in order to changes to be visible when user closes tab and reopens it.
const staticCache = 'site-static-1';  // static cache for main site files, app shell (all static files, html, css, starting images, logo etc). If you change code always rename this to new number 
const assets = [  // this is array of app shell API requests for assets. Those are keys and asset values (images etc) will be values of key/value pair in array
    '/',
    '/index.html',
    '/app.c328ef1a.js',
    '/app.c328ef1a.css',
    '/manifest.webmanifest',
    '/photo-login.04703ebf.jpg',
    '/icon_area.9bfa0c9a.png',
    '/icon_144x144.c75152b5.png',
    '/img/avatar_default.png',
    '/img/icon_pwa.png',
    'https://fonts.googleapis.com/css?family=Raleway:300,400,600,700', 
    'https://code.highcharts.com/stock/highstock.js',
    'https://code.highcharts.com/stock/modules/data.js',
    'https://code.highcharts.com/stock/modules/exporting.js',
    'https://cdn.pubnub.com/sdk/javascript/pubnub.4.21.7.min.js',
    'https://fonts.gstatic.com/s/raleway/v14/1Ptrg8zYS_SKggPNwIYqWqhPAMif.woff2',
    'https://fonts.gstatic.com/s/raleway/v14/1Ptug8zYS_SKggPNyCMIT5lu.woff2',
    'https://fonts.gstatic.com/s/raleway/v14/1Ptug8zYS_SKggPNyC0ITw.woff2',
    'https://fonts.gstatic.com/s/raleway/v14/1Ptrg8zYS_SKggPNwPIsWqhPAMif.woff2',
    'https://fonts.gstatic.com/s/raleway/v14/1Ptrg8zYS_SKggPNwPIsWqZPAA.woff2',
    'https://fonts.gstatic.com/s/raleway/v14/1Ptrg8zYS_SKggPNwJYtWqhPAMif.woff2',
    'https://fonts.gstatic.com/s/raleway/v14/1Ptrg8zYS_SKggPNwJYtWqZPAA.woff2'
];  // we hve to fetch separately fonts from links inside fonts.googleapis.com 
// In Chrome Web Tools go to Application>Cache storage and click /css?family=raleway, links are inside value of that key


// installing service worker event
self.addEventListener('install', evt => {
    console.log('Service worker has been installed');
    // programmatically skip awaiting for new changed sw file to become active, because sometimes closing Chrome and tabs is not enough
    // if we change sw.js and want to make sure change is visible ie cache is refreshed, we need to change version number of staticCache constant.
    // NOTE: If we save this file by adding asset to be fetched (image for example) it will be visible in a new cache upon clicking browser reload.
    // ..but if we delete it from the list of items to be fetched, IT WILL REMAIN  in the cache until we change staticCache version number, save  and reload browser page.
    // So it is best practice to always change version number in staticCache whenever you make and save changes.
    self.skipWaiting(); // it will be triggered only if there is a new sw version that awaits to be executed


    evt.waitUntil(  // installing awaits until this is executed first, otherwise it could stop it
        caches.open(staticCache).then(cache => {  // it opent cache, if there isn't it will create one
            cache.addAll(assets);  // add into cache all assets from the assets array []
        })
    );
});

// activating service worker event
self.addEventListener('activate', evt => {
    console.log('Service worker has been activated');
    evt.waitUntil(
        caches.keys().then(keys => {  // get array with keys (of key/value pair) from different cache versions in  Chrome Dev Tools>Application>Cache Storage
            // go thru all caches keys array and delete all values except newest cache, named in staticCache const. That way only the last cache is used by an app
            return Promise.all(keys  
                .filter(key => key !== staticCache)
                .map(key => caches.delete(key))    
            )
        })
    );
});


// fetch event
self.addEventListener('fetch', evt => {
    console.log('SW is fetching data');
    evt.respondWith(  // check if requested data is stored in the cache.
        caches.match(evt.request).then(cacheRes => {
            return cacheRes || fetch(evt.request)  // if item is in cache use it, if isn't go to the server and fetch it        
        })
    )
});
里约什

似乎问题出activate在服务工作者中事件处理程序上。这主要用于从浏览器中删除旧缓存。尝试用以下代码替换事件监听器

self.addEventListener('activate', (event) => {
    event.waitUntil(
        caches.keys().then((cacheNames) => {
            return Promise.all(
                cacheNames.map((cache) => {
                    if (cache !== staticCache) {
                        return caches.delete(cache); //Deleting the old cache (cache v1)
                    }
                })
            );
        })
            .then(function () {
                console.info("Old caches are cleared!");
                return self.clients.claim();
            })
    );
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

PWA服务工作者缓存/更新预期行为

PWA-服务工作者(反应)如何工作?

如何由服务工作者更新缓存的资源

注销时如何清除服务工作者缓存?

服务工作者缓存与 VuexPersistence

如何卸载服务工作者?

服务工作者从磁盘缓存中缓存资源

离子 PWA 服务工作者 - 没有 CORS

使用CSRF令牌缓存请求时,如何减少服务工作者缓存的大小

根据年龄删除服务工作者缓存中的条目

服务工作者在反应中缓存的路由

WebView:以编程方式清除服务工作者缓存

具有SSL证书缓存的服务工作者

服务工作者缓存破坏谷歌地图 api

React PWA:如何正确设置清单文件并注册服务工作者

如何列出服务工作者已经存储在浏览器缓存中的博客帖子?

如何根据服务工作者中的某些标志缓存响应

如何使用服务工作者预缓存名称在每次部署时都会更改的资产?

由于PWA服务工作者缓存,Auth basic 401页面不提示输入凭据

取消注册/删除服务工作者

服务工作者,它是如何工作的?

如何在服务工作者中刷新图片

notificationclick事件服务工作者

服务工作者中的XMLHttpRequest

使用Workbox CLI及其服务工作者生成器的PWA无法离线工作

使用 PWA 服务工作者在离线模式下保存/提供 Web 服务

服务工作者和客户端请求“缓存控制”:“无缓存”

创建反应应用程序 pwa 打字稿模板无法检测服务工作者

Angular PWA错误无法安装站点:未检测到匹配的服务工作者