-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
24 lines (22 loc) · 872 Bytes
/
sw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Based on: https://adactio.com/journal/13540
// https://gist.github.com/adactio/3717b7da007a9363ddf21f584aae34af/revisions
// Original license:
// Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// http://creativecommons.org/publicdomain/zero/1.0/
const cacheName = 'files';
addEventListener('fetch', fetchEvent => {
const request = fetchEvent.request;
if (request.method !== 'GET') {
return;
}
fetchEvent.respondWith(async function() {
const responseFromFetch = fetch(request);
fetchEvent.waitUntil(async function() {
const responseCopy = (await responseFromFetch).clone();
const myCache = await caches.open(cacheName);
await myCache.put(request, responseCopy);
}());
const responseFromCache = await caches.match(request);
return responseFromCache || responseFromFetch;
}());
});