-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathservice-worker.ts
111 lines (97 loc) · 2.63 KB
/
service-worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/// <reference lib="webworker" />
import {
build,
files,
version
} from '$service-worker';
const worker =
self as unknown as ServiceWorkerGlobalScope;
const FILES = `cache${version}`;
// `build` is an array of all the files generated by the bundler,
// `files` is an array of everything in the `static` directory
const to_cache = build.concat(files);
const staticAssets = new Set(to_cache);
worker.addEventListener('install', (event) => {
event.waitUntil(
caches
.open(FILES)
.then((cache) => cache.addAll(to_cache))
.then(() => {
worker.skipWaiting();
})
);
});
worker.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then(async (keys) => {
// delete old caches
for (const key of keys) {
if (key !== FILES)
await caches.delete(key);
}
worker.clients.claim();
})
);
});
/**
* Fetch the asset from the network and store it in the cache.
* Fall back to the cache if the user is offline.
*/
async function fetchAndCache(request: Request) {
const cache = await caches.open(
`offline${version}`
);
try {
const response = await fetch(request);
cache.put(request, response.clone());
return response;
} catch (err) {
const response = await cache.match(request);
if (response) return response;
throw err;
}
}
worker.addEventListener('fetch', (event) => {
if (
event.request.method !== 'GET' ||
event.request.headers.has('range')
)
return;
const url = new URL(event.request.url);
// don't try to handle e.g. data: URIs
const isHttp = url.protocol.startsWith('http');
const isDevServerRequest =
url.hostname === self.location.hostname &&
url.port !== self.location.port;
const isStaticAsset =
url.host === self.location.host &&
staticAssets.has(url.pathname);
const skipBecauseUncached =
event.request.cache === 'only-if-cached' &&
!isStaticAsset;
// ... adding a `not-same-origin` check for `requests`
// ... more info -> https://stackoverflow.com/questions/48463483/what-causes-a-failed-to-execute-fetch-on-serviceworkerglobalscope-only-if
const skipBecauseNotSameOrigin =
event.request.mode !== 'same-origin';
if (
isHttp &&
!isDevServerRequest &&
!skipBecauseUncached &&
!skipBecauseNotSameOrigin
) {
event.respondWith(
(async () => {
// always serve static files and bundler-generated assets from cache.
// if your application has other URLs with data that will never change,
// set this variable to true for them and they will only be fetched once.
const cachedAsset =
isStaticAsset &&
(await caches.match(event.request));
return (
cachedAsset ||
fetchAndCache(event.request)
);
})()
);
}
});