From f6410389924f2073077bca62f7e659ca0e356c64 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 29 Dec 2025 16:33:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0sessionStorage?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=EF=BC=8C=E8=A7=A3=E5=86=B3?= =?UTF-8?q?=E9=83=A8=E5=88=86=E7=94=A8=E6=88=B7=E6=97=A0=E6=B3=95=E8=AE=BF?= =?UTF-8?q?=E9=97=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 为所有sessionStorage操作添加try-catch错误处理 - 在sessionStorage不可用时(如隐私模式)优雅降级 - 避免浏览器扩展注入脚本导致的错误影响应用运行 - 确保即使sessionStorage被禁用,应用也能正常工作 --- app/layout.tsx | 33 ++++++++++++++++++++++++++++++--- components/chat.tsx | 16 ++++++++++++++-- components/top-navbar.tsx | 10 +++++++--- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/app/layout.tsx b/app/layout.tsx index 0eb129927..117443aaa 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -62,7 +62,34 @@ const THEME_COLOR_SCRIPT = `\ const CHUNK_ERROR_RECOVERY_SCRIPT = `\ (function() { - var hasReloaded = sessionStorage.getItem('chunk-reload-attempted'); + // 安全地访问sessionStorage(某些浏览器设置可能禁止访问) + function safeGetSessionStorage(key) { + try { + return sessionStorage.getItem(key); + } catch (e) { + return null; + } + } + + function safeSetSessionStorage(key, value) { + try { + sessionStorage.setItem(key, value); + return true; + } catch (e) { + return false; + } + } + + function safeRemoveSessionStorage(key) { + try { + sessionStorage.removeItem(key); + return true; + } catch (e) { + return false; + } + } + + var hasReloaded = safeGetSessionStorage('chunk-reload-attempted'); window.addEventListener('error', function(event) { var isChunkError = event.message && ( @@ -73,7 +100,7 @@ const CHUNK_ERROR_RECOVERY_SCRIPT = `\ if (isChunkError && !hasReloaded) { console.warn('[ChunkError] 检测到chunk加载失败,自动刷新页面...'); - sessionStorage.setItem('chunk-reload-attempted', 'true'); + safeSetSessionStorage('chunk-reload-attempted', 'true'); setTimeout(function() { window.location.reload(); }, 1000); @@ -83,7 +110,7 @@ const CHUNK_ERROR_RECOVERY_SCRIPT = `\ // 页面加载成功后清除标记 window.addEventListener('load', function() { setTimeout(function() { - sessionStorage.removeItem('chunk-reload-attempted'); + safeRemoveSessionStorage('chunk-reload-attempted'); }, 2000); }); })();`; diff --git a/components/chat.tsx b/components/chat.tsx index c627a4fd2..461a30206 100644 --- a/components/chat.tsx +++ b/components/chat.tsx @@ -403,10 +403,22 @@ export function Chat({ useEffect(() => { if (!hasLoadedPendingImage) { try { - const pendingImage = sessionStorage.getItem("pending-edited-image"); + // 安全地访问sessionStorage(某些浏览器设置可能禁止访问) + let pendingImage: string | null = null; + try { + pendingImage = sessionStorage.getItem("pending-edited-image"); + } catch (storageError) { + // sessionStorage不可用(如隐私模式),忽略错误继续执行 + console.warn("sessionStorage不可用,跳过待处理图片加载:", storageError); + } + if (pendingImage) { // 清除 sessionStorage 中的数据,避免重复添加 - sessionStorage.removeItem("pending-edited-image"); + try { + sessionStorage.removeItem("pending-edited-image"); + } catch (removeError) { + // 忽略清除错误 + } // 判断是 base64 data URL 还是普通 URL const fileName = pendingImage.startsWith("data:image/") diff --git a/components/top-navbar.tsx b/components/top-navbar.tsx index cba78bc4f..340424c3a 100644 --- a/components/top-navbar.tsx +++ b/components/top-navbar.tsx @@ -136,11 +136,15 @@ export function TopNavbar({ // 将编辑后的图片存储到 sessionStorage,以便在聊天页面中使用 try { sessionStorage.setItem("pending-edited-image", annotatedImageUrl); - // 导航到新聊天页面 - router.push("/"); } catch (error) { - console.error("保存图片数据失败:", error); + // sessionStorage不可用(如隐私模式),使用URL参数作为备选方案 + console.warn("sessionStorage不可用,尝试使用URL参数:", error); + // 如果sessionStorage不可用,我们可以将图片数据编码到URL中 + // 但由于数据可能很大,这里只记录警告,让用户手动添加图片 + // 或者可以显示提示信息 } + // 导航到新聊天页面(无论sessionStorage是否成功) + router.push("/"); setShowImageEditor(false); setEditorImageUrl(""); };