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
|
import { precacheAndRoute, matchPrecache } from "workbox-precaching";
import { registerRoute, setDefaultHandler } from "workbox-routing";
import {
NetworkFirst,
NetworkOnly,
StaleWhileRevalidate,
} from "workbox-strategies";
import { CacheableResponsePlugin } from "workbox-cacheable-response";
import { ExpirationPlugin } from "workbox-expiration";
declare let self: ServiceWorkerGlobalScope;
self.addEventListener("message", (event) => {
if (event.data && (event.data as { type: string }).type === "SKIP_WAITING") {
void self.skipWaiting();
}
});
precacheAndRoute(self.__WB_MANIFEST);
const networkOnly = new NetworkOnly();
registerRoute(new RegExp("/swagger/.+"), new NetworkOnly());
registerRoute(new RegExp("/api/token/.+"), new NetworkOnly());
registerRoute(new RegExp("/api/search/.+"), new NetworkOnly());
registerRoute(
new RegExp("/api/users/.+/avatar"),
new StaleWhileRevalidate({
cacheName: "avatars",
plugins: [
new CacheableResponsePlugin({
statuses: [200],
}),
new ExpirationPlugin({
maxAgeSeconds: 60 * 60 * 24 * 30 * 3, // 3 months
}),
],
})
);
registerRoute(
new RegExp("/api/.+"),
new NetworkFirst({
plugins: [
new CacheableResponsePlugin({
statuses: [200],
}),
],
})
);
setDefaultHandler((options) => {
const { request } = options;
if (request instanceof Request && request.destination === "document")
return matchPrecache("/index.html").then((r) =>
r == null ? Response.error() : r
);
else return networkOnly.handle(options);
});
|