You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
793 B
TypeScript
31 lines
793 B
TypeScript
const base = 'https://hydro.ac';
|
|
const target = [
|
|
'https://hydro.ac',
|
|
'https://us.hydro.ac',
|
|
];
|
|
|
|
this.addEventListener('install', () => {
|
|
console.log('Service Worker installing');
|
|
});
|
|
async function get(url) {
|
|
let current = 0;
|
|
let response;
|
|
while (target[current]) {
|
|
response = fetch(url.replace(base, target[current]));
|
|
try {
|
|
await response;
|
|
if (current) console.log('From ', target[current]);
|
|
return response;
|
|
} catch (error) {
|
|
console.warn(target[current], ' Load fail ', error);
|
|
current++;
|
|
}
|
|
}
|
|
return response;
|
|
}
|
|
this.addEventListener('fetch', (event) => {
|
|
// eslint-disable-next-line no-restricted-globals
|
|
if (new URL(event.request.url).origin !== location.origin) return;
|
|
event.respondWith(get(event.request.url));
|
|
});
|