忽略 Service Worker 请求中的查询参数

Bri*_*ach 6 javascript service-worker

我正在尝试使用服务人员构建离线应用程序。我有一个资源列表,定义如下

var urlsToPrefetch = ['foo.html', 'bar.js']

查询字符串被添加到许多 URL 中,这导致服务工作人员的fetch事件失败,因为请求与缓存中定义的内容不匹配。查询字符串主要用于强制获取新文件版本。例如,bar.js要求为bar.js?version=2

有一个选项可以忽略查询字符串(ignoreSearch如下),尽管从 Chrome V50 开始尚未实现。Chrome Canary 仅适用于 Win/Mac

下面的代码位于fetch eventListener

event.respondWith(
    // caches.match() will look for a cache entry in all of the caches available to the service worker.
    // It's an alternative to first opening a specific named cache and then matching on that.

    caches.match(event.request, {'ignoreSearch': true} ).then(function(response) {
        if (response) {

            return response;
        }

        ...
)
Run Code Online (Sandbox Code Playgroud)

jsx*_*xqf -1

使用Request

var requestsToCache = [
  new Request('foo.html'),
  new Request('bar.js')
];
// ...
  cache.addAll(requestsToCache);
// ...
Run Code Online (Sandbox Code Playgroud)