Search code examples
httpservice-workerhttp-caching

Cache html ignoring query string


On my site, I use query string parameters to pass information to the Javascript, but the actual html returned is independent of the query string (it is just a template which is filled in dynamically by Javascript).

Is there any easy way to get the page cached independent of query string parameters? One idea I had was to use a service worker to strip query strings from requests, but I was wondering if there was any simpler and cleaner approach.


Solution

  • Yes, just cache the non-query-parameters page and then, when responding to a fetch event, pass an additional option to cache.match(request, options) call:

    return cache.match(event.request, {ignoreSearch: true})
        .then(function (response) {
            // resolves with the match regardless of query string
        });
    

    See MDN docs for Cache.match parameters.