diff options
author | crupest <crupest@outlook.com> | 2021-06-15 18:14:58 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-06-15 18:14:58 +0800 |
commit | 08aed0d21a8e7fd5e225140fa1ee8f0e879841c5 (patch) | |
tree | 8dc987e49ce3054f06fe9c30065333a929eafdba /FrontEnd/src/sw.ts | |
parent | 5c979373b53f40f2d0f263f52071caf8ab188e44 (diff) | |
download | timeline-08aed0d21a8e7fd5e225140fa1ee8f0e879841c5.tar.gz timeline-08aed0d21a8e7fd5e225140fa1ee8f0e879841c5.tar.bz2 timeline-08aed0d21a8e7fd5e225140fa1ee8f0e879841c5.zip |
...
Diffstat (limited to 'FrontEnd/src/sw.ts')
-rw-r--r-- | FrontEnd/src/sw.ts | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/FrontEnd/src/sw.ts b/FrontEnd/src/sw.ts new file mode 100644 index 00000000..0130e345 --- /dev/null +++ b/FrontEnd/src/sw.ts @@ -0,0 +1,66 @@ +/// <reference no-default-lib="true"/> +/// <reference lib="esnext" /> +/// <reference lib="webworker" /> + +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); +}); |