Fix locale path for Navbar.#1034
Merged
Merged
Conversation
Generated by Endless task #22. Co-authored-by: Huacnlee Li Huashun <huacnlee@longbridge-inc.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 Auto-generated by Endless task #22.
Initiated by: Huacnlee Li Huashun
背景
开发者文档站(open.longbridge.com)支持三种语言:英文(根路径)、简体中文(
/zh-CN/...)和繁体中文(/zh-HK/...)。用户切换语言后,页面 URL 会携带对应的语言前缀,但 AppNav 顶部导航栏中的链接(Docs、Pricing、CLI、MCP、Skill、品牌 Logo 等)及 FeaturesMenu 功能下拉菜单中的链接,始终指向英文根路径,而非当前语言路径。这意味着在中文页面点击导航链接会跳回英文版本,用户体验不连贯。根因
utils/i18n.ts中的localePath()函数通过读取window.location.pathname来判断当前语言前缀。这个读取行为不是 Vue 响应式依赖——Vue 的响应式系统无法追踪它,因此模板中调用localePath(href)的绑定不会在语言变化时自动重新求值。更关键的是,VitePress 在静态生成阶段(SSR)渲染各语言页面时,
isServer()返回true,localePath()直接原样返回路径,不添加任何语言前缀。预生成的 zh-CN / zh-HK 页面 HTML 中,导航链接因此全部是无前缀的英文路径。客户端水合时,若没有响应式依赖触发重新渲染,这些链接便保持错误状态。摘要
utils/i18n.ts:新增useLocalePath()composable,内部使用 VitePressuseData().localeIndex(响应式 ref)计算语言前缀,取代直接读取window.location.pathnameAppNav.vue:将localePath的静态导入替换为useLocalePathcomposable,在setup中调用获得响应式路径函数FeaturesMenu.vue:同上,替换为useLocalePathcomposablelocaleIndex是 VitePress 内置的响应式 ref,SSR 阶段也能正确返回当前语言键('root'/'zh-CN'/'zh-HK'),从根本上消除 SSR 预生成时的无前缀问题localeIndex.value作为依赖,无需额外包裹localePath导出不变,其他调用方(如NewHomePage/index.vue)无需修改bun run dev后访问/zh-CN/docs,检查 AppNav 所有链接 href 是否包含/zh-CN/前缀;访问/zh-HK/docs重复检查;开发者工具控制台无 Vue hydration 警告修改
docs/.vitepress/theme/utils/i18n.tsuseLocalePath()composable,使用useData().localeIndex响应式计算语言前缀docs/.vitepress/theme/components/AppNav.vuelocalePath静态导入替换为useLocalePath,在 setup 中调用docs/.vitepress/theme/components/FeaturesMenu.vuelocalePath静态导入替换为useLocalePath,在 setup 中调用关键决策
使用
localeIndex(值为'root'/'zh-CN'/'zh-HK')而非lang(值为'en-US'/'zh-CN'/'zh-HK')计算 URL 前缀,因为localeIndex的值与 VitePress locales 配置键完全对应,英文根路径用'root'表示而非'en',映射逻辑更直接,不需要额外的字符串转换。