sw-toolbox icon indicating copy to clipboard operation
sw-toolbox copied to clipboard

Feature request: support cache busting via query params

Open markbrocato opened this issue 8 years ago • 1 comments

Apps often make requests like "/api/users?_dc=123456789", where _dc is the timestamp at which the request is generated and is used to prevent caching. This always results in a cache miss when using sw-toolbox. When it is desirable to fetch fresh data from the server when connected to the network, but fall back on the last cached response when offline, it would be great if there was a way read from the cache for /api/users when /api/users?_dc=123456789 fails to connect.

The sw-precache library already contains a similar option called ignoreUrlParametersMatching. Building on that, one could imagine the following:

toolbox.router.get(/api\/users/, toolbox.networkFirst, { ignoreUrlParametersMatching: /^_dc$/ })

markbrocato avatar Oct 10 '16 22:10 markbrocato

Hey Mark, that would be an awesome pull-request. I did a quick look through the codebase, and it looks like the place where the query strings are being ignored is in /lib/router.js on line 98. Where it says var path = urlObject.pathname. For example, to always treat query strings as their own unique requests you would modify this line to read: var path = urlObject.pathname + urlObject.search. Hopefully this helps get you started.

mudcube avatar Oct 11 '16 03:10 mudcube