An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Correct service worker precaching and handle resource failures
Daniel64811c1de575
diff --git a/app/scripts/serviceWorker.test.js b/app/scripts/serviceWorker.test.jsnew file mode 100644index 0000000000..8c8bae0b2c--- /dev/null+++ b/app/scripts/serviceWorker.test.js@@ -0,0 +1,55 @@+const assert = require("node:assert/strict");+const {readFileSync, existsSync} = require("node:fs");+const path = require("node:path");+const {test} = require("node:test");+const vm = require("node:vm");++const source = readFileSync(path.join(__dirname, "../stage/service-worker.js"), "utf8");++test("precache waits for every resource and preserves successes when one request fails", async () => {+ const listeners = {};+ const cached = [];+ const warnings = [];+ let finishPending;+ const pending = new Promise(resolve => {+ finishPending = resolve;+ });+ vm.runInNewContext(source, {+ URL,+ location: {href: "https://example.com/service-worker.js?v=test"},+ self: {+ skipWaiting() {},+ addEventListener(type, listener) {+ listeners[type] = listener;+ }+ },+ caches: {+ async open() {+ return {+ async add(resource) {+ if (resource === "/favicon.ico") {+ throw new Error("HTTP 404");+ }+ await pending;+ cached.push(resource);+ }+ };+ }+ },+ console: {warn: (...args) => warnings.push(args)}+ });+ let installation;+ listeners.install({waitUntil: promise => { installation = promise; }});+ let completed = false;+ installation.then(() => { completed = true; });+ await new Promise(resolve => setImmediate(resolve));+ assert.equal(completed, false);+ finishPending();+ await installation;+ assert.equal(cached.length, 6);+ assert.equal(warnings.length, 1);+ assert.equal(warnings[0][1], "/favicon.ico");+ const font = cached.find(resource => resource.endsWith(".woff2"));+ assert.ok(font);+ assert.ok(existsSync(path.join(__dirname, "..", font)));+});diff --git a/app/stage/service-worker.js b/app/stage/service-worker.jsindex dcb3a70df1..07b748067a 100644--- a/app/stage/service-worker.js+++ b/app/stage/service-worker.js@@ -6,7 +6,7 @@ const INITIAL_CACHED_RESOURCES = ["/stage/icon-large.png","/stage/icon.png","/stage/loading-pure.svg",- "/stage/build/fonts/JetBrainsMono-Regular.woff2",+ "/appearance/fonts/JetBrainsMono-2.304/JetBrainsMono-Regular.woff2","/stage/protyle/js/lute/lute.min.js","/stage/protyle/js/protyle-html.js"];@@ -15,7 +15,14 @@ self.addEventListener("install", event => {self.skipWaiting();event.waitUntil((async () => {const cache = await caches.open(CACHE_NAME);- cache.addAll(INITIAL_CACHED_RESOURCES);+ // 分别缓存启动资源,单个请求失败时保留其他资源,并记录失败地址。+ await Promise.all(INITIAL_CACHED_RESOURCES.map(async resource => {+ try {+ await cache.add(resource);+ } catch (error) {+ console.warn("预缓存资源失败:", resource, error);+ }+ }));})());});@@ -62,6 +69,7 @@ self.addEventListener("fetch", event => {// Don't care about other requests.if (!url.pathname.startsWith("/stage/") &&!url.pathname.startsWith("/appearance/boot/") &&+ !url.pathname.startsWith("/appearance/fonts/") &&!url.pathname.startsWith("/appearance/emojis/") &&!url.pathname.startsWith("/appearance/langs/") &&!url.href.startsWith("https://assets.b3logfile.com/") &&