简体   繁体   English

PWA应用程序的自动更新解决方案

[英]Auto-update solution for PWA Apps

I want to turn my normal website into a progressive web app. 我想将我的普通网站变成渐进式Web应用程序。 For that everything is finished. 为此,一切都完成了。 The Website is responsible and I already set up a manifest. 该网站负责,我已经建立了清单。 Now the last part comes to me. 现在最后一部分来了。 The ServiceWorker to save the website to the cache of the device. ServiceWorker将网站保存到设备的缓存中。 My problem is that I can not find a simple solution for that. 我的问题是我找不到一个简单的解决方案。 Because for my website it is important that the user runs the new version every time I made an update. 因为对于我的网站,每次我进行更新时用户都运行新版本非常重要。 Do you know a solution how let the ServiceWorker check if anything changed once the user opens the app and after that delete the old cache and make a new one based on the new files and the new code? 您知道一种解决方案吗?一旦用户打开应用程序,然后让ServiceWorker检查是否有任何更改,然后删除旧的缓存并根据新文件和新代码创建一个新的缓存?
It would be very great, when somebody could provide a code example. 当有人可以提供代码示例时,这将非常棒。
My HTML SIDE: 我的HTML侧:

<script>
  if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('./sw.js')
    .then(reg => console.log(reg))
    .catch(err => console.log(err));
  }
</script>  

AND HERE THE sw.js: 然后在sw.js:

    self.addEventListener('install', event => event.waitUntil(
  caches.open('web_pwa').then(cache => cache.add('/'))
));

self.addEventListener('fetch', event => event.respondWith(
  caches.open('web_pwa')
    .then(cache => cache.match(event.request))
    .then(response => response || fetch(event.request))
));

Short description: Update SW file a bit and handle removing old cache in activate event. 简短说明:稍微更新SW文件并在activate事件中处理删除旧缓存。

Detailed description 详细说明

Whenever you do changes in your code files (other than sw.js ) . 每当您更改代码文件sw.js )时 You need to trigger both the install and activate events in service worker. 您需要在Service Worker中触发安装激活事件。 In order to update your changes. 为了更新您的更改。

In order to trigger these events the sw.js should be updated even a single bit. 为了触发这些事件, sw.js甚至应进行一点更新。 (simply atleast a character). (只需至少一个字符)。

By convention developers handle this by maintaining Versioning . 按照惯例,开发人员可以通过维护Versioning来解决此问题 Also it will be easy to add and delete caches. 另外, 添加和删​​除缓存也很容易

const version = 'v2';

self.addEventListener('install', res => {
  caches.open(version).then(cache => {
    cache.addAll(caches);
  });
  console.log('[sw] Installed successfully :');
  // self.skipWaiting();
});

self.addEventListener('fetch', event => event.respondWith(
  caches.open(version)
    .then(cache => cache.match(event.request))
    .then(response => response || fetch(event.request))
));

If you need to directly update the sw.js inspite of user's status then uncomment the self.skipWaiting() as it skips waiting for activation and gets activated immediately. 如果您不考虑用户状态而需要直接更新sw.js ,则取消对self.skipWaiting()注释,因为它会跳过等待激活并立即被激活的状态。

Inside activate event is where you have to delete the old caches based on cache version. 内部activate事件是您必须根据缓存版本删除旧缓存的地方。

self.addEventListener('activate', event => {
  caches.keys().then(function(cacheNames) {
    cacheNames.forEach(function(cacheName) {
      if (cacheName !== version) {
        caches.delete(cacheName);
      }
    });
  });
}

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

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