Skip to content

Commit

Permalink
Website service worker fixups
Browse files Browse the repository at this point in the history
* The caching logic was not properly distributed between the individual service workers and the shared website service worker
* The service worker registration was missing from idea.whatwg.org
  • Loading branch information
domenic committed Jul 28, 2018
1 parent 28f6c90 commit 03cd927
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 32 deletions.
5 changes: 5 additions & 0 deletions idea.whatwg.org/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ <h2>Ideas</h2>
<footer>
<p><small>Copyright © 2018 WHATWG (Apple, Google, Mozilla, Microsoft). This work is licensed under a <a rel="license" href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>.</small></p>
</footer>

<script>
"use strict";
navigator.serviceWorker.register("/service-worker.js");
</script>
9 changes: 0 additions & 9 deletions idea.whatwg.org/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
"use strict";

self.cacheKey = "v1";
self.toCache = [
"/",
"https://whatwg.org/style/shared.css",
"https://whatwg.org/style/subpages.css",
"https://resources.whatwg.org/logo.svg"
];

importScripts("https://resources.whatwg.org/website-service-worker.js");
19 changes: 11 additions & 8 deletions resources.whatwg.org/website-service-worker.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
"use strict";
/* USAGE:
self.cacheKey = "v1";
self.toCache = "...";
// Optional:
self.cacheKey = "v3";
self.toCache = ["..."];
// Then:
importScripts("https://resources.whatwg.org/website-service-worker.js");
*/

const cacheKey = "v1";
const toCache = [
const cacheKeyToUse = "v2_" + (self.cacheKey || "v0");
const everythingToCache = [
"/",
"https://whatwg.org/style/shared.css",
"https://whatwg.org/style/subpages.css",
"https://resources.whatwg.org/logo.svg"
];
].concat(self.toCache || []);

self.oninstall = e => {
e.waitUntil(caches.open(self.cacheKey).then(cache => cache.addAll(self.toCache)));
e.waitUntil(caches.open(cacheKeyToUse).then(cache => cache.addAll(everythingToCache)));
};

self.onactivate = e => {
e.waitUntil(caches.keys().then(keys => {
return Promise.all(keys.filter(key => key !== self.cacheKey).map(key => caches.delete(key)));
return Promise.all(keys.filter(key => key !== cacheKeyToUse).map(key => caches.delete(key)));
}));
};

Expand Down Expand Up @@ -57,5 +60,5 @@ function refreshCacheFromNetworkResponse(req, res) {

const resForCache = res.clone();

return caches.open(self.cacheKey).then(cache => cache.put(req, resForCache));
return caches.open(cacheKeyToUse).then(cache => cache.put(req, resForCache));
}
9 changes: 0 additions & 9 deletions spec.whatwg.org/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
"use strict";

self.cacheKey = "v1";
self.toCache = [
"/",
"https://whatwg.org/style/shared.css",
"https://whatwg.org/style/subpages.css",
"https://resources.whatwg.org/logo.svg"
];

importScripts("https://resources.whatwg.org/website-service-worker.js");
7 changes: 1 addition & 6 deletions whatwg.org/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"use strict";

self.cacheKey = "v1";
self.toCache = [
"/",
"/shared.css",
"/subpages.css",
"/img/arrow.svg",
"/img/bird-webpage.svg",
"/img/checker.svg",
Expand All @@ -21,8 +17,7 @@ self.toCache = [
"/code-of-conduct",
"/working-mode",
"/sg-agreement",
"/sg-policy",
"https://resources.whatwg.org/logo.svg"
"/sg-policy"
];

importScripts("https://resources.whatwg.org/website-service-worker.js");

0 comments on commit 03cd927

Please sign in to comment.