diff options
author | crupest <crupest@outlook.com> | 2020-06-11 17:27:15 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-06-11 17:27:15 +0800 |
commit | 93ce8560fa19c3a91de99643fdbbe4f895a47b84 (patch) | |
tree | 66a9e6f1bbbbd5a0a25c13a0e51e7a7c1225871c /Timeline/ClientApp/src/sw/sw.ts | |
parent | 6893a1c1e43b8fc17eaaba72db90494d946b5091 (diff) | |
download | timeline-93ce8560fa19c3a91de99643fdbbe4f895a47b84.tar.gz timeline-93ce8560fa19c3a91de99643fdbbe4f895a47b84.tar.bz2 timeline-93ce8560fa19c3a91de99643fdbbe4f895a47b84.zip |
feat(front): Service worker is coming!
Diffstat (limited to 'Timeline/ClientApp/src/sw/sw.ts')
-rw-r--r-- | Timeline/ClientApp/src/sw/sw.ts | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Timeline/ClientApp/src/sw/sw.ts b/Timeline/ClientApp/src/sw/sw.ts new file mode 100644 index 00000000..67f5dfd4 --- /dev/null +++ b/Timeline/ClientApp/src/sw/sw.ts @@ -0,0 +1,22 @@ +import { precacheAndRoute, matchPrecache } from 'workbox-precaching'; +import { setDefaultHandler } from 'workbox-routing'; +import { NetworkOnly } from 'workbox-strategies'; + +declare let self: ServiceWorkerGlobalScope; + +precacheAndRoute(self.__WB_MANIFEST); + +const networkOnly = new NetworkOnly(); + +setDefaultHandler((options) => { + const { request, url } = options; + if (url && url.pathname.startsWith('/api/')) { + return networkOnly.handle(options); + } + + if (request instanceof Request && request.destination === 'document') + return matchPrecache('/index.html').then((r) => + r == null ? Response.error() : r + ); + else return networkOnly.handle(options); +}); |