From f3b795e8a9c0e1cfcf88b2d6f0bad307d093f5e7 Mon Sep 17 00:00:00 2001 From: kkfluous Date: Wed, 15 Apr 2026 17:13:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B0=8F=E7=A8=8B=E5=BA=8F/webview=20?= =?UTF-8?q?=E5=86=85=E9=9C=80=E7=82=B9=E4=B8=A4=E6=AC=A1=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=89=8D=E8=83=BD=E9=80=80=E5=87=BA=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E7=89=88=E6=9C=AC=E5=8F=B7=201.1.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Shell 首次挂载时用 location.hash= 同步模块到 URL hash,会 push 一条多余 的 history 记录;webview 里第一次返回只是回到 hash 为空的同一页面没有 视觉变化,得再按一次才能真正退出。 改为 history.replaceState 更新 hash,切换 tab 也走 replace,整个应用 只占用一个 history 记录,一次返回即可退出。 Co-Authored-By: Claude Opus 4.6 (1M context) --- package.json | 2 +- src/components/Shell.tsx | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index f44da99..273d366 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "ln-bi", "private": true, - "version": "1.1.2", + "version": "1.1.3", "type": "module", "scripts": { "dev": "concurrently -n server,client -c blue,green \"npm run dev:server\" \"npm run dev:client\"", diff --git a/src/components/Shell.tsx b/src/components/Shell.tsx index b94d355..fd6a4ee 100644 --- a/src/components/Shell.tsx +++ b/src/components/Shell.tsx @@ -1,5 +1,6 @@ import { useState, useEffect, useMemo, type ComponentType } from 'react'; import { useAuth } from '../auth/useAuth'; +import { DemoModeProvider } from './Blur'; export interface ModuleConfig { id: string; @@ -44,14 +45,19 @@ export function Shell({ modules }: { modules: ModuleConfig[] }) { }, [modules]); useEffect(() => { - // 同步 hash 到当前模块 + // 同步 hash 到当前模块:使用 replaceState 避免产生多余的 history 记录, + // 否则在小程序/webview 环境下首次进入需要点两次返回才能退出 if (window.location.hash.slice(1) !== activeModule) { - window.location.hash = activeModule; + const { pathname, search } = window.location; + window.history.replaceState(null, '', `${pathname}${search}#${activeModule}`); } }, [activeModule]); const switchModule = (id: string) => { - window.location.hash = id; + if (window.location.hash.slice(1) === id) return; + const { pathname, search } = window.location; + window.history.replaceState(null, '', `${pathname}${search}#${id}`); + setActiveModule(id); }; const ActiveComponent = modules.find((m) => m.id === activeModule)?.component ?? modules[0]?.component; @@ -64,6 +70,7 @@ export function Shell({ modules }: { modules: ModuleConfig[] }) { }, [user]); return ( +
{/* 全局水印 */}
@@ -117,5 +124,6 @@ export function Shell({ modules }: { modules: ModuleConfig[] }) { })}
+ ); }