mirror of
https://github.com/hzrd149/nostrudel.git
synced 2025-09-18 19:41:48 +02:00
add custom service worker
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { lazy, Suspense } from "react";
|
import { lazy, Suspense } from "react";
|
||||||
import { createHashRouter, Outlet, RouterProvider, ScrollRestoration } from "react-router-dom";
|
import { createBrowserRouter, Outlet, RouterProvider, ScrollRestoration } from "react-router-dom";
|
||||||
import { Spinner } from "@chakra-ui/react";
|
import { Spinner } from "@chakra-ui/react";
|
||||||
|
|
||||||
import { ErrorBoundary } from "./components/error-boundary";
|
import { ErrorBoundary } from "./components/error-boundary";
|
||||||
@@ -184,7 +184,7 @@ const NoLayoutPage = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const router = createHashRouter([
|
const router = createBrowserRouter([
|
||||||
{
|
{
|
||||||
path: "signin",
|
path: "signin",
|
||||||
element: <SigninView />,
|
element: <SigninView />,
|
||||||
|
54
src/worker.ts
Normal file
54
src/worker.ts
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
// / <reference no-default-lib="true"/>
|
||||||
|
/// <reference lib="ES2022" />
|
||||||
|
/// <reference lib="DOM" />
|
||||||
|
/// <reference lib="webworker" />
|
||||||
|
|
||||||
|
// Default type of `self` is `WorkerGlobalScope & typeof globalThis`
|
||||||
|
// https://github.com/microsoft/TypeScript/issues/14877
|
||||||
|
declare var self: ServiceWorkerGlobalScope;
|
||||||
|
|
||||||
|
self.addEventListener("install", () => {
|
||||||
|
self.skipWaiting();
|
||||||
|
});
|
||||||
|
|
||||||
|
// caching
|
||||||
|
import { cleanupOutdatedCaches, precacheAndRoute, createHandlerBoundToURL } from "workbox-precaching";
|
||||||
|
import { registerRoute, NavigationRoute } from "workbox-routing";
|
||||||
|
|
||||||
|
precacheAndRoute(self.__WB_MANIFEST);
|
||||||
|
cleanupOutdatedCaches();
|
||||||
|
|
||||||
|
let allowlist: undefined | RegExp[] = undefined;
|
||||||
|
if (import.meta.env.DEV) allowlist = [/^\/$/];
|
||||||
|
registerRoute(new NavigationRoute(createHandlerBoundToURL("index.html"), { allowlist }));
|
||||||
|
|
||||||
|
// notifications
|
||||||
|
import { type WebPushNotification } from "@satellite-earth/core/types/control-api/notifications.js";
|
||||||
|
|
||||||
|
self.addEventListener("push", (event) => {
|
||||||
|
const data = event.data?.json() as WebPushNotification | undefined;
|
||||||
|
|
||||||
|
if (!data) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
event.waitUntil(self.registration.showNotification(data.title, { body: data.body, data, icon: data.icon }));
|
||||||
|
} catch (error) {}
|
||||||
|
});
|
||||||
|
|
||||||
|
self.addEventListener("notificationclick", (event) => {
|
||||||
|
event.notification.close();
|
||||||
|
|
||||||
|
const data: WebPushNotification = event.notification.data;
|
||||||
|
|
||||||
|
event.waitUntil(
|
||||||
|
self.clients.matchAll({ type: "window" }).then((clientList) => {
|
||||||
|
const firstClient = clientList[0];
|
||||||
|
if (firstClient) {
|
||||||
|
firstClient.focus();
|
||||||
|
firstClient.navigate(data.url);
|
||||||
|
} else {
|
||||||
|
self.clients.openWindow(data.url).then((window) => window?.focus());
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
@@ -18,15 +18,17 @@ export default defineConfig({
|
|||||||
plugins: [
|
plugins: [
|
||||||
react(),
|
react(),
|
||||||
VitePWA({
|
VitePWA({
|
||||||
registerType: "prompt",
|
strategies: "injectManifest",
|
||||||
// strategies: "injectManifest",
|
registerType: "autoUpdate",
|
||||||
// srcDir: "src",
|
injectRegister: null,
|
||||||
// filename: "sw.ts",
|
srcDir: "src",
|
||||||
// devOptions: {
|
filename: "worker.ts",
|
||||||
// // NOTE: ESM service workers is not supported by firefox
|
injectManifest: {
|
||||||
// type: "module",
|
minify: false,
|
||||||
// enabled: true,
|
sourcemap: true,
|
||||||
// },
|
// This increase the cache limit to 4mB
|
||||||
|
maximumFileSizeToCacheInBytes: 2097152 * 2,
|
||||||
|
},
|
||||||
workbox: {
|
workbox: {
|
||||||
// This increase the cache limit to 4mB
|
// This increase the cache limit to 4mB
|
||||||
maximumFileSizeToCacheInBytes: 2097152 * 2,
|
maximumFileSizeToCacheInBytes: 2097152 * 2,
|
||||||
@@ -50,34 +52,24 @@ export default defineConfig({
|
|||||||
start_url: "/",
|
start_url: "/",
|
||||||
scope: "/",
|
scope: "/",
|
||||||
shortcuts: [
|
shortcuts: [
|
||||||
{
|
|
||||||
name: "Notifications",
|
|
||||||
url: "/#/notifications",
|
|
||||||
description: "",
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "Notes",
|
name: "Notes",
|
||||||
url: "/#/",
|
url: "/",
|
||||||
description: "",
|
description: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Notifications",
|
name: "Notifications",
|
||||||
url: "/#/notifications",
|
url: "/notifications",
|
||||||
description: "",
|
description: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Messages",
|
name: "Messages",
|
||||||
url: "/#/dm",
|
url: "/messages",
|
||||||
description: "",
|
description: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Streams",
|
name: "Streams",
|
||||||
url: "/#/streams",
|
url: "/streams",
|
||||||
description: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Wiki",
|
|
||||||
url: "/#/wiki",
|
|
||||||
description: "",
|
description: "",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
Reference in New Issue
Block a user