更新 basename 模块,添加动态计算 basename 的功能,并在 main.tsx 中使用该功能
This commit is contained in:
@@ -2,12 +2,12 @@ import ReactDOM from 'react-dom/client'
|
||||
import { RouterProvider, createRouter } from '@tanstack/react-router'
|
||||
import { routeTree } from './routeTree.gen'
|
||||
import './index.css'
|
||||
import { basename } from './modules/basename'
|
||||
import { getDynamicBasename } from './modules/basename'
|
||||
|
||||
// Set up a Router instance
|
||||
const router = createRouter({
|
||||
routeTree,
|
||||
basepath: basename,
|
||||
basepath: getDynamicBasename(),
|
||||
defaultPreload: 'intent',
|
||||
scrollRestoration: true,
|
||||
})
|
||||
|
||||
@@ -8,4 +8,15 @@ export const wrapBasename = (path: string) => {
|
||||
} else {
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
// 动态计算 basename,根据当前 URL 路径
|
||||
export const getDynamicBasename = (): string => {
|
||||
const path = window.location.pathname
|
||||
const [user, key, id] = path.split('/').filter(Boolean)
|
||||
if (key === 'v1' && id) {
|
||||
return `/${user}/v1/${id}`
|
||||
}
|
||||
// 默认使用构建时的 basename
|
||||
return basename
|
||||
}
|
||||
Reference in New Issue
Block a user