Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass-through caching demo. #40

Merged

Conversation

jeffposnick
Copy link
Contributor

@KenjiBaheux @jakearchibald @slightlyoff & co.:

Here's my initial attempt at a pass-through caching example, closing #31.

You can see a live demo of this code at https://rawgit.com/jeffposnick/samples/sw-pass-through-caching/service-worker/pass-through-caching/index.html

console.log(' No response for %s found in cache. About to fetch from network...', event.request.url);

return fetch(event.request).then(function(response) {
console.log(' Response for %s from network is: %O', event.request.url, response);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommended checking the response was valid AND that you got a response.status == 200. If either of those are broken, then bail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would checking that the response is valid entail—just a null check (are there any cases in which fetch() resolves with null instead of rejecting?) or is there some specific property of response that should be checking? I see that there's a termination reason defined as part of the response object, but I don't actually see it mapped to a property of what gets retruned in the current Chrome implementation.

Additionally, since Chrome returns opaque filtered responses for non-CORS requests, it's possible that response.status == 0 is a legitimate, cachable response. So it's a tradeoff between never caching opaque responses or caching them unconditionally, and risking caching a transient error response.

That being said, I will at least check response.status >= 400, to avoid caching any non-opaque error responses.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was discussing this point with a colleague and we both ended up agreeing that caching only 200 and 0 responses would be the safest bet for a recommended sample.

It will save people a lot of pain should a bad response get cached.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I talked to @slightlyoff about this, and he was (somewhat?) surprised that opaque filtered responses always had a status of 0, and suggested that I file whatwg/fetch#14 to see whether that restriction could be relaxed in the fetch() spec.

I'll keep caching whenever response.status < 400 and make a point of specifically highlighting this tradeoff in the comments. As long as the fetch() spec stays the same, it'll be each developer's decision whether they want to err on the side of caching too much or too little.

<link rel="icon" sizes="192x192" href="../../images/touch/chrome-touch-icon-192x192.png">

<!-- Add to homescreen for Safari on iOS -->
<!-- TODO: Replace PLACEHOLDER with feature name. -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PLACEHOLDER seems to have been removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks for catching that.

@jeffposnick
Copy link
Contributor Author

So I've added some caveats to explain that this particular aggressive form of caching is not appropriate for many use cases. I am going to merge this now since I think it's still useful as a sample, but the other samples we put out might be more relevant for real-world usage.

jeffposnick added a commit that referenced this pull request Nov 12, 2014
@jeffposnick jeffposnick merged commit 58333a0 into GoogleChrome:gh-pages Nov 12, 2014
@jeffposnick jeffposnick deleted the sw-pass-through-caching branch November 12, 2014 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants