更新 basename 模块,添加动态计算 basename 的功能,并在 main.tsx 中使用该功能

This commit is contained in:
2026-02-18 21:53:06 +08:00
parent b2a7b66f21
commit 2cb404846e
2 changed files with 13 additions and 2 deletions

View File

@@ -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,
})

View File

@@ -9,3 +9,14 @@ export const wrapBasename = (path: string) => {
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
}