@@ -51,32 +51,40 @@ function getLangPath(): string {
5151}
5252
5353// 执行语言重定向
54- function redirectToLang ( router : { go : ( path : string ) => void } ) {
54+ function redirectToLang ( router : { go : ( path : string ) => void } , base : string ) {
5555 // 只在客户端执行
5656 if ( typeof window === 'undefined' ) return
5757
5858 const path = window . location . pathname
59- const base = '/'
59+ const normalizedBase = base . endsWith ( '/' ) ? base : `${ base } /`
60+ const rootPath = normalizedBase
61+ const rootIndexPath = `${ normalizedBase } index.html`
6062
6163 // 检查当前路径是否是根路径或只有 base
62- if ( path === '/' || path === base || path === ` ${ base } /` || path === ` ${ base } /index.html` ) {
64+ if ( path === '/' || path === rootPath || path === rootIndexPath ) {
6365 const langPath = getLangPath ( )
64- router . go ( `${ base } ${ langPath } ` )
66+
67+ // langPath 已经是绝对路径(如 /zh/),避免与 base 再拼接导致 //zh/
68+ if ( path !== langPath ) {
69+ router . go ( langPath )
70+ }
6571 }
6672}
6773
6874export default {
6975 extends : DefaultTheme ,
70- enhanceApp ( { app : _app , router, siteData : _siteData } ) {
76+ enhanceApp ( { app : _app , router, siteData } ) {
7177 // 在路由就绪后进行语言重定向
7278 if ( typeof window !== 'undefined' ) {
79+ const base = siteData . value . base || '/'
80+
7381 // 初始加载时检查
74- redirectToLang ( router )
82+ redirectToLang ( router , base )
7583
7684 // 监听路由变化
7785 router . onAfterRouteChange = ( to : string ) => {
7886 if ( to === '/' || to === '/index.html' ) {
79- redirectToLang ( router )
87+ redirectToLang ( router , base )
8088 }
8189 }
8290 }
0 commit comments