{"action":"created","issue":{"url":"https://api.github.com/repos/slightlyoff/ServiceWorker/issues/805","labels_url":"https://api.github.com/repos/slightlyoff/ServiceWorker/issues/805/labels{/name}","comments_url":"https://api.github.com/repos/slightlyoff/ServiceWorker/issues/805/comments","events_url":"https://api.github.com/repos/slightlyoff/ServiceWorker/issues/805/events","html_url":"https://github.com/slightlyoff/ServiceWorker/issues/805","id":123994616,"number":805,"title":"Allow custom cache keys for request caching","user":{"login":"adrianholovaty","id":180401,"avatar_url":"https://avatars.githubusercontent.com/u/180401?v=3","gravatar_id":"","url":"https://api.github.com/users/adrianholovaty","html_url":"https://github.com/adrianholovaty","followers_url":"https://api.github.com/users/adrianholovaty/followers","following_url":"https://api.github.com/users/adrianholovaty/following{/other_user}","gists_url":"https://api.github.com/users/adrianholovaty/gists{/gist_id}","starred_url":"https://api.github.com/users/adrianholovaty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adrianholovaty/subscriptions","organizations_url":"https://api.github.com/users/adrianholovaty/orgs","repos_url":"https://api.github.com/users/adrianholovaty/repos","events_url":"https://api.github.com/users/adrianholovaty/events{/privacy}","received_events_url":"https://api.github.com/users/adrianholovaty/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-12-27T20:09:02Z","updated_at":"2015-12-27T20:57:48Z","closed_at":null,"body":"At the moment, `Cache.match()` and `Cache.matchAll()` use a `RequestInfo` object as the cache key (i.e., either a `Request` or a string that turns into a `Request`). This is nice and simple for the common case, but it'd be nice to provide a way for developers to explicitly specify the cache key as a string.\r\n\r\nUse cases:\r\n\r\n* In case of one-time/expiring URLs, the cache key should be the subset of the URL that doesn't expire (stripping out the authentication stuff).\r\n* In case of offline mode, if you haven't cached user avatars but you *have* cached a generic avatar, you could rewrite a request to the user avatar to fall back to the generic avatar.\r\n* In case of multiple requests actually resolving to the same content (despite having different URLs), a developer could save cache space by reusing the same single cache for multiple requests. You might say \"Why use different URLs if the content is the same?\" but this might be some legacy system out of the developer's control.\r\n\r\n(I am aware of `ignoreSearch`, `ignoreVary` and `ignoreMethod` but they might not cover all potential cases.)\r\n\r\nI wrote about the expiring-URL use case in detail [on my blog](http://www.holovaty.com/writing/service-worker-cache-names/), and in my own projects I'm solving it with a hack that I discovered in a [comment](https://github.com/slightlyoff/ServiceWorker/issues/657#issuecomment-151866195) that @jeffposnick made in this project's issue tracker. You can hack around the lack of customizable cache keys by simply using a separate `Cache` for each object you're caching (because you can indeed control the name of `Cache` objects). I made a working demo of this [here](https://gist.github.com/adrianholovaty/0568c895fafd9417e13c#file-serviceworker-cache-wrapper-js).\r\n\r\nAnother (possibly lousy) reason to add this is: it's already possible with the aforementioned hack, so we might as well make a cleaner way to do it.\r\n\r\nIf we agree to make this addition to the API, it might make more sense to create new `Cache` methods rather than overloading `match()` and `matchAll()` -- because these existing methods already accept strings as their first argument. I think `get(cacheKeyString)` is a decent contender, though I could see people confusing `match()` and `get()` regularly."},"comment":{"url":"https://api.github.com/repos/slightlyoff/ServiceWorker/issues/comments/167440862","html_url":"https://github.com/slightlyoff/ServiceWorker/issues/805#issuecomment-167440862","issue_url":"https://api.github.com/repos/slightlyoff/ServiceWorker/issues/805","id":167440862,"user":{"login":"adrianholovaty","id":180401,"avatar_url":"https://avatars.githubusercontent.com/u/180401?v=3","gravatar_id":"","url":"https://api.github.com/users/adrianholovaty","html_url":"https://github.com/adrianholovaty","followers_url":"https://api.github.com/users/adrianholovaty/followers","following_url":"https://api.github.com/users/adrianholovaty/following{/other_user}","gists_url":"https://api.github.com/users/adrianholovaty/gists{/gist_id}","starred_url":"https://api.github.com/users/adrianholovaty/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adrianholovaty/subscriptions","organizations_url":"https://api.github.com/users/adrianholovaty/orgs","repos_url":"https://api.github.com/users/adrianholovaty/repos","events_url":"https://api.github.com/users/adrianholovaty/events{/privacy}","received_events_url":"https://api.github.com/users/adrianholovaty/received_events","type":"User","site_admin":false},"created_at":"2015-12-27T20:57:48Z","updated_at":"2015-12-27T20:57:48Z","body":"Well, I just had a Twitter back-and-forth with @slightlyoff and he pointed out that this can be achieved by creating a custom request, then caching that:\r\n\r\n```javascript\r\nlet request = new Request(\"<simplified URL here>\");\r\ncache.put(request, response);\r\n```\r\n\r\nWith this solution in mind, I'm not sure whether this particular issue needs to stay open. But I'll leave it for the maintainers to decide...!"}}