最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ignore query params in service worker requests - Stack Overflow

programmeradmin4浏览0评论

I am trying to build an offline app using service workers. I have a list of resources defined like

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

Query strings are added to many of the urls and that is causing the service worker's fetch event to fail since the request doesn't match what is defined in the cache. The query strings are mainly used to force fetching of new file versions. For instance, bar.js is requested as bar.js?version=2

There is an option for ignoring query strings (ignoreSearch below), although this is not yet implemented as of Chrome V50. Chrome Canary is only available for Win/Mac

The below code is in the 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;
        }

        ...
)

I am trying to build an offline app using service workers. I have a list of resources defined like

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

Query strings are added to many of the urls and that is causing the service worker's fetch event to fail since the request doesn't match what is defined in the cache. The query strings are mainly used to force fetching of new file versions. For instance, bar.js is requested as bar.js?version=2

There is an option for ignoring query strings (ignoreSearch below), although this is not yet implemented as of Chrome V50. Chrome Canary is only available for Win/Mac

The below code is in the 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;
        }

        ...
)
Share Improve this question edited Mar 5, 2016 at 7:06 Brian Leach asked Mar 5, 2016 at 3:33 Brian LeachBrian Leach 4,1548 gold badges40 silver badges78 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You can create a new request removing the query string and then use it instead of the original one:

var url = new URL(request.url);
url.search = '';
url.fragment = '';

let cleanRequest = new Request(url, {
  method: request.method,
  headers: request.headers,
  mode: request.mode,
  credentials: request.credentials,
  cache: request.cache,
  redirect: request.redirect,
  referrer: request.referrer,
  integrity: request.integrity,
});

Try...

function stripQueryStringAndHashFromPath(url) {
  return url.split("?")[0].split("#")[0];
}

function onFetch(event) {
  const requestUrl = stripQueryStringAndHashFromPath(event.request.url.replace(/^.*\/\/[^\/]+/, ''));
... all the other jazz
}

This will give you just the path name. For example /?test=1#three will give you just /.

use Request instead:

var requestsToCache = [
  new Request('foo.html'),
  new Request('bar.js')
];
// ...
  cache.addAll(requestsToCache);
// ...
发布评论

评论列表(0)

  1. 暂无评论