From 85f5cd760b931dda21e80b88bbd1184215b74977 Mon Sep 17 00:00:00 2001 From: Binks Riverton Date: Sun, 5 Jul 2026 17:02:54 +0800 Subject: [PATCH] fix(frontend): prevent white screen from corrupted localStorage - Wrap JSON.parse in loadAndReconcileSettings() with try/catch, fall back to defaultSettings and remove corrupted WeKnora_settings on parse failure instead of crashing the app - Add try/catch around localStorage.setItem write-back for quota exceeded protection - Register app.config.errorHandler to catch unhandled Vue errors and prevent silent component tree crashes --- frontend/src/main.ts | 5 +++++ frontend/src/stores/settings.ts | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/frontend/src/main.ts b/frontend/src/main.ts index 9b87ece0..522ded43 100644 --- a/frontend/src/main.ts +++ b/frontend/src/main.ts @@ -27,6 +27,11 @@ initFont(); const app = createApp(App); +// 全局错误处理:捕获未处理的组件错误,防止白屏 +app.config.errorHandler = (err, instance, info) => { + console.error("[WeKnora] Unhandled Vue error:", err, "\nComponent:", instance, "\nInfo:", info); +}; + app.use(TDesign); app.use(createPinia()); app.use(router); diff --git a/frontend/src/stores/settings.ts b/frontend/src/stores/settings.ts index 9af40c66..c56c0faa 100644 --- a/frontend/src/stores/settings.ts +++ b/frontend/src/stores/settings.ts @@ -112,15 +112,25 @@ const defaultSettings: Settings = { /** Keep builtin agent id and isAgentEnabled in sync after localStorage reload. */ function loadAndReconcileSettings(): Settings { - const loaded = JSON.parse( - localStorage.getItem("WeKnora_settings") || JSON.stringify(defaultSettings), - ) as Settings; + let loaded: Settings; + try { + const raw = localStorage.getItem("WeKnora_settings"); + loaded = raw ? (JSON.parse(raw) as Settings) : { ...defaultSettings }; + } catch (e) { + console.error("[settings] Failed to parse WeKnora_settings from localStorage, resetting to defaults:", e); + localStorage.removeItem("WeKnora_settings"); + return { ...defaultSettings }; + } loaded.selectedTags ||= []; loaded.selectedMCPServices ||= []; loaded.selectedSkills ||= loaded.selectedTools || []; loaded.selectedFileKbMap ||= {}; if (reconcileBuiltinAgentMode(loaded)) { - localStorage.setItem("WeKnora_settings", JSON.stringify(loaded)); + try { + localStorage.setItem("WeKnora_settings", JSON.stringify(loaded)); + } catch { + /* quota exceeded — non-critical */ + } } return loaded; }