feature: handle multiple configs
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
const config = require("../config.json");
|
||||
let configPath = process.env.CONFIG_PATH || 'config.json';
|
||||
const config = require(`../${configPath}`);
|
||||
|
||||
module.exports = {
|
||||
locals: config
|
||||
|
140
src/sw.js
140
src/sw.js
@ -30,84 +30,86 @@ self.addEventListener('activate', function (e) {
|
||||
self.clients.claim();
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', function (event) {
|
||||
let requestURL = new URL(event.request.url);
|
||||
if (requestURL.origin == location.origin) {
|
||||
// Static asset, cache then network
|
||||
event.respondWith(
|
||||
caches.open(CACHE_NAME).then(function (cache) {
|
||||
return cache.match(event.request).then(function (response) {
|
||||
var fetchPromise = fetch(event.request).then(function (networkResponse) {
|
||||
cache.put(event.request, networkResponse.clone());
|
||||
return networkResponse;
|
||||
if(process.env.NODE_ENV === 'production'){
|
||||
self.addEventListener('fetch', function (event) {
|
||||
let requestURL = new URL(event.request.url);
|
||||
if (requestURL.origin == location.origin) {
|
||||
// Static asset, cache then network
|
||||
event.respondWith(
|
||||
caches.open(CACHE_NAME).then(function (cache) {
|
||||
return cache.match(event.request).then(function (response) {
|
||||
var fetchPromise = fetch(event.request).then(function (networkResponse) {
|
||||
cache.put(event.request, networkResponse.clone());
|
||||
return networkResponse;
|
||||
});
|
||||
return response || fetchPromise;
|
||||
});
|
||||
return response || fetchPromise;
|
||||
});
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
if (
|
||||
event.request.method == 'POST' ||
|
||||
event.request.method == 'PUT'
|
||||
) {
|
||||
// disabled: lead to cors errors
|
||||
// // POST/PUT to api, rewrite the cache
|
||||
// event.respondWith(
|
||||
// caches.open(CACHE_NAME + '-api').then(function (cache) {
|
||||
// return fetch(event.request).then(function (response) {
|
||||
// cache.put(event.request, response.clone());
|
||||
// return response;
|
||||
// })
|
||||
// }));
|
||||
// api: no cache
|
||||
event.respondWith(fetch(event.request));
|
||||
} else if (
|
||||
/matomo/.test(requestURL.origin) ||
|
||||
/sentry/.test(requestURL.origin) ||
|
||||
/jabber/.test(requestURL.origin) ||
|
||||
/xmpp/.test(requestURL.origin)
|
||||
) {
|
||||
// analytics, always distant
|
||||
event.respondWith(fetch(event.request));
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
if (
|
||||
/unpkg/.test(requestURL.origin) ||
|
||||
/skypack/.test(requestURL.origin) ||
|
||||
/jspm/.test(requestURL.origin) ||
|
||||
/jsdeliver/.test(requestURL.origin) ||
|
||||
/cdn/.test(requestURL.origin) ||
|
||||
/googleapis/.test(requestURL.origin)
|
||||
event.request.method == 'POST' ||
|
||||
event.request.method == 'PUT'
|
||||
) {
|
||||
// cdn: cache then network
|
||||
event.respondWith(
|
||||
caches.open(CACHE_NAME + '-cdn').then(function (cache) {
|
||||
return cache.match(event.request).then(function (response) {
|
||||
var fetchPromise = fetch(event.request).then(function (networkResponse) {
|
||||
cache.put(event.request, networkResponse.clone());
|
||||
return networkResponse;
|
||||
});
|
||||
return response || fetchPromise;
|
||||
});
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
// disabled: lead to cors errors
|
||||
// // api: distant then cache
|
||||
// // POST/PUT to api, rewrite the cache
|
||||
// event.respondWith(
|
||||
// fetch(event.request)
|
||||
// .then((response) => {
|
||||
// caches.open(CACHE_NAME + '-api').then(function (cache) {
|
||||
// caches.open(CACHE_NAME + '-api').then(function (cache) {
|
||||
// return fetch(event.request).then(function (response) {
|
||||
// cache.put(event.request, response.clone());
|
||||
// return response;
|
||||
// });
|
||||
// })
|
||||
// .catch(() => {
|
||||
// return caches.match(event.request);
|
||||
// })
|
||||
// );
|
||||
// })
|
||||
// }));
|
||||
// api: no cache
|
||||
event.respondWith(fetch(event.request));
|
||||
} else if (
|
||||
/matomo/.test(requestURL.origin) ||
|
||||
/sentry/.test(requestURL.origin) ||
|
||||
/jabber/.test(requestURL.origin) ||
|
||||
/xmpp/.test(requestURL.origin)
|
||||
) {
|
||||
// analytics, always distant
|
||||
event.respondWith(fetch(event.request));
|
||||
} else {
|
||||
if (
|
||||
/unpkg/.test(requestURL.origin) ||
|
||||
/skypack/.test(requestURL.origin) ||
|
||||
/jspm/.test(requestURL.origin) ||
|
||||
/jsdeliver/.test(requestURL.origin) ||
|
||||
/cdn/.test(requestURL.origin) ||
|
||||
/googleapis/.test(requestURL.origin)
|
||||
) {
|
||||
// cdn: cache then network
|
||||
event.respondWith(
|
||||
caches.open(CACHE_NAME + '-cdn').then(function (cache) {
|
||||
return cache.match(event.request).then(function (response) {
|
||||
var fetchPromise = fetch(event.request).then(function (networkResponse) {
|
||||
cache.put(event.request, networkResponse.clone());
|
||||
return networkResponse;
|
||||
});
|
||||
return response || fetchPromise;
|
||||
});
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
// disabled: lead to cors errors
|
||||
// // api: distant then cache
|
||||
// event.respondWith(
|
||||
// fetch(event.request)
|
||||
// .then((response) => {
|
||||
// caches.open(CACHE_NAME + '-api').then(function (cache) {
|
||||
// cache.put(event.request, response.clone());
|
||||
// return response;
|
||||
// });
|
||||
// })
|
||||
// .catch(() => {
|
||||
// return caches.match(event.request);
|
||||
// })
|
||||
// );
|
||||
// api: no cache
|
||||
event.respondWith(fetch(event.request));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user