Skip to content

Commit

Permalink
Use more generic url hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Oct 7, 2016
1 parent 7b1f423 commit 2dc4e04
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions static/service-worker.js
@@ -1,10 +1,6 @@
self.importScripts('idb-keyval.js');

var aliases = {
index: /^https?:\/\/[^/]+\/(index.html)?$/,
};

var configs;
var hooks;
var updateConfig = (function () {
var updating;
var recheck;
Expand All @@ -18,9 +14,13 @@ var updateConfig = (function () {
return idbKeyval.get("hookConfigs").then(onConfig);
}

function onConfig(newConfigs) {
if ((configs || newConfigs) && JSON.stringify(newConfigs) !== JSON.stringify(configs)) {
configs = newConfigs;
function onConfig(configs) {
if (configs || hooks) {
hooks = [];
for (var key in configs) {
hooks.push(new RegExp(key), configs[key]);
}
if (hooks.length === 0) hooks = undefined;
console.log("Hook Configs Updated");
console.log(configs);
}
Expand Down Expand Up @@ -48,17 +48,15 @@ self.addEventListener('message', updateConfig);
self.addEventListener('fetch', event => {
updateConfig();
// If there are no custom configs (default state in production), do nothing.
if (!configs) return;
if (!hooks) return;

var url = event.request.url;
console.log("fetch", event.request.url);

// Look for matching configured hooks
var keys = Object.keys(configs);
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
if (aliases[key] ? aliases[key].test(url) : key === url) {
var customUrl = configs[key];
for (var i = 0, l = hooks.length; i < l; i += 2) {
if (hooks[i].test(url)) {
var customUrl = url.replace(hooks[i], hooks[i + 1]);
console.warn("URL override via custom hook", url, customUrl);
return event.respondWith(fetch(customUrl));
}
Expand Down

0 comments on commit 2dc4e04

Please sign in to comment.