aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-06-15 18:14:58 +0800
committercrupest <crupest@outlook.com>2021-06-15 18:14:58 +0800
commit08aed0d21a8e7fd5e225140fa1ee8f0e879841c5 (patch)
tree8dc987e49ce3054f06fe9c30065333a929eafdba /FrontEnd/src
parent5c979373b53f40f2d0f263f52071caf8ab188e44 (diff)
downloadtimeline-08aed0d21a8e7fd5e225140fa1ee8f0e879841c5.tar.gz
timeline-08aed0d21a8e7fd5e225140fa1ee8f0e879841c5.tar.bz2
timeline-08aed0d21a8e7fd5e225140fa1ee8f0e879841c5.zip
...
Diffstat (limited to 'FrontEnd/src')
-rw-r--r--FrontEnd/src/index.tsx1
-rw-r--r--FrontEnd/src/service-worker.tsx (renamed from FrontEnd/src/service-worker.txt)0
-rw-r--r--FrontEnd/src/sw.ts66
-rw-r--r--FrontEnd/src/views/timeline-common/ConnectionStatusBadge.tsx2
4 files changed, 68 insertions, 1 deletions
diff --git a/FrontEnd/src/index.tsx b/FrontEnd/src/index.tsx
index 83c25792..28034601 100644
--- a/FrontEnd/src/index.tsx
+++ b/FrontEnd/src/index.tsx
@@ -13,6 +13,7 @@ import "./index.css";
import "./i18n";
import "./palette";
+import "./service-worker";
import App from "./App";
diff --git a/FrontEnd/src/service-worker.txt b/FrontEnd/src/service-worker.tsx
index ea8dfc32..ea8dfc32 100644
--- a/FrontEnd/src/service-worker.txt
+++ b/FrontEnd/src/service-worker.tsx
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);
+});
diff --git a/FrontEnd/src/views/timeline-common/ConnectionStatusBadge.tsx b/FrontEnd/src/views/timeline-common/ConnectionStatusBadge.tsx
index 1b9d6d2a..df43d8d2 100644
--- a/FrontEnd/src/views/timeline-common/ConnectionStatusBadge.tsx
+++ b/FrontEnd/src/views/timeline-common/ConnectionStatusBadge.tsx
@@ -1,6 +1,6 @@
import React from "react";
import classnames from "classnames";
-import { HubConnectionState } from "srcmicrosoft/signalr";
+import { HubConnectionState } from "@microsoft/signalr";
import { useTranslation } from "react-i18next";
export interface ConnectionStatusBadgeProps {