使用firebase更新PWA

asv*_*asv 2 javascript firebase firebase-hosting progressive-web-apps

我用firebase托管做了一个渐进的Web应用程序.当我进行一些更改并部署时,我从浏览器访问应用程序时看不到更改.

这是我在sw.js中的代码:

self.addEventListener('install', function (event) {
  console.log('SW Installed');
  event.waitUntil(
    caches.open('static')
      .then(function (cache) {
        cache.addAll([
          '/',
          '/index.html',
          '/app.js',
          '/images/icons/icon-144x144.png'
        ]);
      })
  );
});

self.addEventListener('activate', function () {
  console.log('SW Activated');
});

self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request)
      .then(function(res) {
        if (res) {
          return res;
        } else {
          return fetch(event.request);
        }
      })
  );
});
Run Code Online (Sandbox Code Playgroud)

这是我在firebase.json中的代码:

{
  "hosting": {
    "public": "public"
  }
}
Run Code Online (Sandbox Code Playgroud)

Abe*_*ins 6

您的sw.js文件正在将应用的资源缓存到名为的缓存中static.

caches.open('static')
  .then(function (cache) {
    cache.addAll([
      '/',
      '/index.html',
      '/app.js',
      '/images/icons/icon-144x144.png'
    ]);
  })
Run Code Online (Sandbox Code Playgroud)

重新部署Firebase托管应用时,此缓存不会被清除,因此您不会看到任何更改.

在chrome中的开发人员工具中,您将能够看到将列出缓存的缓存部分(下面的屏幕截图中的那个被调用offline)

从这里,您可以右键单击并选择Delete清除它.

缓存示例

或者你可以跑......

caches.delete("static");
Run Code Online (Sandbox Code Playgroud)

在控制台中删除缓存(或在应用程序中添加"加载新版本"按钮),清除缓存并刷新页面.

  • 看一下这一章https://www.safaribooksonline.com/library/view/building-progressive-web/9781491961643/ch04.html它涵盖了有关PWA生命周期和缓存管理的大量信息.短版本,如果您需要查看服务工作者的状态,并在检测到新的服务工作程序时(通常在刷新一两次后)自动清理旧缓存.所以是的,它是可行的,但它作为开发人员有点笨拙. (3认同)