服务人员新资产更新

问题描述 投票:0回答:6

我一直在阅读 html5rocks 服务工作者简介文章,并创建了一个基本的服务工作者来缓存页面、JS 和 CSS,它按预期工作:

var CACHE_NAME = 'my-site-cache-v1';
var urlsToCache = [
  '/'
];

// Set the callback for the install step
self.addEventListener('install', function (event) {
  // Perform install steps
  event.waitUntil(
    caches.open(CACHE_NAME)
      .then(function(cache) {
        console.log('Opened cache');
        return cache.addAll(urlsToCache);
      })
  );
});

self.addEventListener('fetch', function (event) {
  event.respondWith(
    caches.match(event.request)
      .then(function(response) {
        // Cache hit - return response
        if (response) {
          return response;
        }

        // IMPORTANT: Clone the request. A request is a stream and
        // can only be consumed once. Since we are consuming this
        // once by cache and once by the browser for fetch, we need
        // to clone the response
        var fetchRequest = event.request.clone();

        return fetch(fetchRequest).then(
          function(response) {
            // Check if we received a valid response
            if(!response || response.status !== 200 || response.type !== 'basic') {
              return response;
            }

            // IMPORTANT: Clone the response. A response is a stream
            // and because we want the browser to consume the response
            // as well as the cache consuming the response, we need
            // to clone it so we have 2 stream.
            var responseToCache = response.clone();

            caches.open(CACHE_NAME)
              .then(function(cache) {
                cache.put(event.request, responseToCache);
              });

            return response;
          }
        );
      })
    );
});

当我对 CSS 进行更改时,此更改不会被拾取,因为服务工作线程正确地从缓存返回 CSS。

我遇到的问题是,如果我要更改 HTML、JS 或 CSS,我如何确保服务工作人员从服务器加载较新的版本(如果可以的话)而不是从缓存加载?我尝试过在 CSS 导入上使用版本标记,但这似乎不起作用。

service-worker
6个回答
59
投票

一个选择是仅使用 Service Worker 的缓存作为后备,并始终尝试通过 fetch() 进入

网络优先
。不过,您会失去缓存优先策略提供的一些性能提升。

另一种方法是使用

sw-precache
生成 Service Worker 脚本作为站点构建过程的一部分。

它生成的服务工作线程将使用文件内容的哈希值来检测更改,并在部署新版本时自动更新缓存。它还将使用缓存清除 URL 查询参数来确保您不会意外地使用 HTTP 缓存中的过时版本填充服务工作线程缓存。

在实践中,您最终会得到一个使用性能友好的缓存优先策略的服务工作人员,但缓存将在页面加载后“在后台”更新,以便下次访问时,一切都是新鲜的。如果您愿意,可以向用户显示一条消息,让他们知道有可用的更新内容并提示他们重新加载。


23
投票

使缓存失效的一种方法是每当您更改缓存文件中的任何内容时都会更改

CACHE_NAME
的版本。由于该更改将更改
service-worker.js
浏览器将加载更新版本,并且您将有机会删除旧缓存并创建新缓存。您可以在
activate
处理程序中删除旧缓存。这是预取示例中描述的策略。 如果您已经在 CSS 文件上使用某种版本标记,请确保它们能够进入 Service Worker 脚本。

这当然不会改变 CSS 文件上的缓存头需要正确设置的事实。否则,服务工作人员将仅加载已缓存在浏览器缓存中的文件。


6
投票

浏览器缓存问题

这里的主要问题是,当您的新 Service Worker 安装时,他会获取由前一个 Service Worker 处理的请求,并且他很可能从缓存中获取资源,因为这是您的缓存策略。然后,即使您使用新代码、新缓存名称更新 Service Worker,调用

self.skipWaiting()
,他仍然会将旧资源放入缓存中!

这就是我完全更新 Service Worker 的方式

需要知道的一件事是,每次代码脚本更改时,Service Worker 都会触发 install 事件,因此您不需要使用版本标记或其他任何东西,只需保留相同的文件名就可以了,甚至建议这样做。 浏览器还有其他方式认为您的 Service Worker 已更新。

1。重写您的 install 事件处理程序:

我不使用

cache.addAll
,因为它坏了。事实上,如果无法获取要缓存的一项且仅有一项资源,则整个安装将失败,甚至不会将一个文件添加到缓存中。现在假设您要缓存的文件列表是从存储桶自动生成的(这是我的情况),并且您的存储桶已更新并删除了一个文件,那么您的 PWA 将安装失败,但它不应该失败。

sw.js

self.addEventListener('install', (event) => {
  // prevents the waiting, meaning the service worker activates
  // as soon as it's finished installing
  // NOTE: don't use this if you don't want your sw to control pages
  // that were loaded with an older version
  self.skipWaiting();

  event.waitUntil((async () => {
    try {
      // self.cacheName and self.contentToCache are imported via a script
      const cache = await caches.open(self.cacheName);
      const total = self.contentToCache.length;
      let installed = 0;

      await Promise.all(self.contentToCache.map(async (url) => {
        let controller;

        try {
          controller = new AbortController();
          const { signal } = controller;
          // the cache option set to reload will force the browser to
          // request any of these resources via the network,
          // which avoids caching older files again
          const req = new Request(url, { cache: 'reload' });
          const res = await fetch(req, { signal });

          if (res && res.status === 200) {
            await cache.put(req, res.clone());
            installed += 1;
          } else {
            console.info(`unable to fetch ${url} (${res.status})`);
          }
        } catch (e) {
          console.info(`unable to fetch ${url}, ${e.message}`);
          // abort request in any case
          controller.abort();
        }
      }));

      if (installed === total) {
        console.info(`application successfully installed (${installed}/${total} files added in cache)`);
      } else {
        console.info(`application partially installed (${installed}/${total} files added in cache)`);
      }
    } catch (e) {
      console.error(`unable to install application, ${e.message}`);
    }
  })());
});

2。当(新)Service Worker 激活时清理旧缓存:

sw.js

// remove old cache if any
self.addEventListener('activate', (event) => {
  event.waitUntil((async () => {
    const cacheNames = await caches.keys();

    await Promise.all(cacheNames.map(async (cacheName) => {
      if (self.cacheName !== cacheName) {
        await caches.delete(cacheName);
      }
    }));
  })());
});

3.每次更新资源时我都会更新缓存名称:

sw.js

// this imported script has the newly generated cache name (self.cacheName)
// and a list of all the files on my bucket I want to be cached (self.contentToCache),
// and is automatically generated in Gitlab based on the tag version
self.importScripts('cache.js');

// the install event will be triggered if there's any update,
// a new cache will be created (see 1.) and the old one deleted (see 2.)

4。处理缓存中的

Expires
Cache-Control
响应标头

我在服务工作人员的 fetch 事件处理程序中使用这些标头来捕获当资源过期/应该刷新时是否应该通过网络请求资源。

基本示例:

// ...

try {
  const cachedResponse = await caches.match(event.request);

  if (exists(cachedResponse)) {
    const expiredDate = new Date(cachedResponse.headers.get('Expires'));

    if (expiredDate.toString() !== 'Invalid Date' && new Date() <= expiredDate) {
      return cachedResponse.clone();
    }
  }

  // expired or not in cache, request via network...
} catch (e) {
  // do something...
}
// ...

1
投票

对我来说最简单:

const cacheName = 'my-app-v1';

self.addEventListener('activate', async (event) => {

    const existingCaches = await caches.keys();
    const invalidCaches = existingCaches.filter(c => c !== cacheName);
    await Promise.all(invalidCaches.map(ic => caches.delete(ic)));

    // do whatever else you need to...

});

如果您有多个缓存,您只需修改代码以进行选择性即可。


0
投票

在我的主页中,我使用一些 PHP 从 mySQL 获取数据。

为了在有互联网的情况下始终保持最新的 php 数据,我使用以毫秒为单位的日期作为我的服务人员的版本。

在这种情况下,当您有互联网并重新加载页面时,兑现的页面将始终更新。

//SET VERSION
const version = Date.now();
const staticCacheName = version + 'staticfiles';

//INSTALL
self.addEventListener('install', function(e) {
    e.waitUntil(
        caches.open(staticCacheName).then(function(cache) {
            return cache.addAll([

0
投票

我也有同样的问题。我用下面的逻辑解决了它。

如果请求被缓存,我的代码将返回缓存的响应,并在后台触发对服务器的获取请求,该请求在完成后更新缓存。

所以我们第一次看到陈旧的数据。 但是,如果用户刷新页面,缓存就会更新以反映新的更改。

self.addEventListener("fetch", (event) => {
  event.respondWith(
    caches
      .match(event.request)
      .then((res) => {
        // trigger a fetch request to update/create cache
        const actualRespone = fetch(event.request)
          .then((response) => {
            // cache response
            const resClone = response.clone();
            caches.open(cacheName).then((cache) => {
              cache.put(event.request, resClone);
            });
            return response;
          })

        // return cached response or actual response if not cached
        return res ? res : actualRespone;
      })
  );
});
© www.soinside.com 2019 - 2024. All rights reserved.