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
This commit is contained in:
@@ -27,6 +27,11 @@ initFont();
|
|||||||
|
|
||||||
const app = createApp(App);
|
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(TDesign);
|
||||||
app.use(createPinia());
|
app.use(createPinia());
|
||||||
app.use(router);
|
app.use(router);
|
||||||
|
|||||||
@@ -112,15 +112,25 @@ const defaultSettings: Settings = {
|
|||||||
|
|
||||||
/** Keep builtin agent id and isAgentEnabled in sync after localStorage reload. */
|
/** Keep builtin agent id and isAgentEnabled in sync after localStorage reload. */
|
||||||
function loadAndReconcileSettings(): Settings {
|
function loadAndReconcileSettings(): Settings {
|
||||||
const loaded = JSON.parse(
|
let loaded: Settings;
|
||||||
localStorage.getItem("WeKnora_settings") || JSON.stringify(defaultSettings),
|
try {
|
||||||
) as Settings;
|
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.selectedTags ||= [];
|
||||||
loaded.selectedMCPServices ||= [];
|
loaded.selectedMCPServices ||= [];
|
||||||
loaded.selectedSkills ||= loaded.selectedTools || [];
|
loaded.selectedSkills ||= loaded.selectedTools || [];
|
||||||
loaded.selectedFileKbMap ||= {};
|
loaded.selectedFileKbMap ||= {};
|
||||||
if (reconcileBuiltinAgentMode(loaded)) {
|
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;
|
return loaded;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user