Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

Improve Service Worker (#8089)

颖逸99c4f1f89098
diff --git a/app/src/util/serviceWorker.ts b/app/src/util/serviceWorker.ts
index a7409745e1..e2812a07dc 100644
--- a/app/src/util/serviceWorker.ts
+++ b/app/src/util/serviceWorker.ts
@@ -1,5 +1,5 @@
// https://github.com/siyuan-note/siyuan/pull/8012
-export const registerServiceWorker = (scriptURL: string) => {
+export const registerServiceWorker = (scriptURL: string, scope: string = "/", workerType: WorkerType = "module") => {
if (!("serviceWorker" in navigator) || typeof (navigator.serviceWorker) === "undefined" ||
!("caches" in window) || !("fetch" in window)) {
return;
@@ -7,8 +7,8 @@ export const registerServiceWorker = (scriptURL: string) => {
// REF https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration
window.navigator.serviceWorker
.register(scriptURL, {
- scope: "./",
- type: "module",
+ scope,
+ type: workerType,
}).then(registration => {
registration.update();
}).catch(e => {
diff --git a/app/stage/service-worker.js b/app/stage/service-worker.js
index 6e3a0181d6..764230d040 100644
--- a/app/stage/service-worker.js
+++ b/app/stage/service-worker.js
@@ -7,8 +7,6 @@ const INITIAL_CACHED_RESOURCES = [
"/stage/icon.png",
"/stage/loading-pure.svg",
"/stage/loading.svg",
- "/stage/manifest.webmanifest",
- "/stage/service-worker.js",
"/stage/build/fonts/JetBrainsMono-Regular.woff",
"/stage/protyle/js/lute/lute.min.js",
"/stage/protyle/js/protyle-html.js"
@@ -41,19 +39,26 @@ self.addEventListener("fetch", event => {
// Don't care about other-origin URLs.
if (url.origin !== location.origin &&
- url.origin !== "https://assets.b3logfile.com") {
+ url.origin !== "https://assets.b3logfile.com"
+ ) {
return;
}
// Don't care about anything else than GET.
if (event.request.method !== 'GET' ||
- event.request.destination === "document") {
+ event.request.destination === "document"
+ ) {
return;
}
- // Don't care about widget requests.
- if (!url.pathname.startsWith("/stage/") && !url.pathname.startsWith("/appearance/") &&
- "/favicon.ico" !== url.pathname &&
- url.href.indexOf("https://assets.b3logfile.com/avatar/") === -1) {
+
+ // Don't care about other requests.
+ if (!url.pathname.startsWith("/stage/") &&
+ !url.pathname.startsWith("/appearance/boot/") &&
+ !url.pathname.startsWith("/appearance/emojis/") &&
+ !url.pathname.startsWith("/appearance/langs/") &&
+ !url.href.startsWith("https://assets.b3logfile.com/avatar/") &&
+ url.pathname !== "/favicon.ico"
+ ) {
return;
}