Skip to content

Commit

Permalink
Fix some bugs in service worker code
Browse files Browse the repository at this point in the history
Fixes #211, by not treating opaque responses as errors. Also logs warnings for failures to refresh the cache, to make this easier to debug in the future, and switches the "needs to be fresh" check to return true for all navigations.
  • Loading branch information
domenic committed Jul 25, 2018
1 parent 189f336 commit 28f6c90
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
14 changes: 8 additions & 6 deletions resources.whatwg.org/standard-service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const standardShortname = location.host.split(".")[0];

const cacheKey = "v4";
const cacheKey = "v5";
const toCache = [
location.origin + "/",
"https://resources.whatwg.org/spec.css",
Expand Down Expand Up @@ -46,11 +46,14 @@ self.onfetch = e => {
caches.match(e.request).then(cachedResponse => {
const networkFetchPromise = fetch(e.request);

// Ignore network fetch or caching errors; they just mean we won't be able to refresh the cache.
// Only warn on network fetch or caching errors; they just mean we won't be able to refresh
// the cache. (But, don't ignore them, because that could hide coding errors.)
e.waitUntil(
networkFetchPromise
.then(res => refreshCacheFromNetworkResponse(e.request, res))
.catch(() => {})
.catch(e => {
console.warn(`Could not refresh the cache for ${e.request.url}`, e);
})
);

return cachedResponse || networkFetchPromise;
Expand All @@ -66,7 +69,7 @@ self.onactivate = e => {
};

function refreshCacheFromNetworkResponse(req, res) {
if (!res.ok) {
if (res.type !== "opaque" && !res.ok) {
throw new Error(`${res.url} is responding with ${res.status}`);
}

Expand All @@ -76,6 +79,5 @@ function refreshCacheFromNetworkResponse(req, res) {
}

function needsToBeFresh(req) {
const requestURL = new URL(req.url);
return requestURL.origin === location.origin && requestURL.pathname === "/";
return req.mode === "navigate";
}
9 changes: 6 additions & 3 deletions resources.whatwg.org/website-service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ self.onfetch = e => {
caches.match(e.request).then(cachedResponse => {
const networkFetchPromise = fetch(e.request);

// Ignore network fetch or caching errors; they just mean we won't be able to refresh the cache.
// Only warn on network fetch or caching errors; they just mean we won't be able to refresh
// the cache. (But, don't ignore them, because that could hide coding errors.)
e.waitUntil(
networkFetchPromise
.then(res => refreshCacheFromNetworkResponse(e.request, res))
.catch(() => {})
.catch(e => {
console.warn(`Could not refresh the cache for ${e.request.url}`, e);
})
);

return cachedResponse || networkFetchPromise;
Expand All @@ -48,7 +51,7 @@ self.onfetch = e => {
};

function refreshCacheFromNetworkResponse(req, res) {
if (!res.ok) {
if (res.type !== "opaque" && !res.ok) {
throw new Error(`${res.url} is responding with ${res.status}`);
}

Expand Down

0 comments on commit 28f6c90

Please sign in to comment.