mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-06-02 02:15:34 +08:00
fix: harden router basename detection
This commit is contained in:
39
src/App.tsx
39
src/App.tsx
@@ -9,7 +9,44 @@ import { PluginsProvider } from './contexts/PluginsContext';
|
||||
import AppContent from './components/app/AppContent';
|
||||
import i18n from './i18n/config.js';
|
||||
|
||||
function detectRouterBasename() {
|
||||
const explicitBasename = typeof window !== 'undefined' ? window.__ROUTER_BASENAME__ || '' : '';
|
||||
if (explicitBasename) {
|
||||
return explicitBasename.replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
if (typeof window === 'undefined' || typeof document === 'undefined') {
|
||||
return '';
|
||||
}
|
||||
|
||||
const candidatePaths = [
|
||||
document.querySelector('link[rel="manifest"]')?.getAttribute('href'),
|
||||
document.querySelector('script[type="module"][src]')?.getAttribute('src'),
|
||||
document.querySelector('link[rel="icon"][href]')?.getAttribute('href'),
|
||||
].filter((value): value is string => Boolean(value));
|
||||
|
||||
let detectedBasename = '';
|
||||
for (const candidate of candidatePaths) {
|
||||
try {
|
||||
const pathname = new URL(candidate, document.baseURI || window.location.href).pathname;
|
||||
const match = pathname.match(/^(.*)\/(?:assets\/|manifest\.json$|favicon\.(?:svg|png)$)/);
|
||||
if (match) {
|
||||
const normalized = match[1] ? match[1].replace(/\/+$/, '') : '';
|
||||
if (normalized.length > detectedBasename.length) {
|
||||
detectedBasename = normalized;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Ignore invalid candidate URLs and continue checking other hints.
|
||||
}
|
||||
}
|
||||
|
||||
return detectedBasename;
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
const routerBasename = detectRouterBasename();
|
||||
|
||||
return (
|
||||
<I18nextProvider i18n={i18n}>
|
||||
<ThemeProvider>
|
||||
@@ -19,7 +56,7 @@ export default function App() {
|
||||
<TasksSettingsProvider>
|
||||
<TaskMasterProvider>
|
||||
<ProtectedRoute>
|
||||
<Router basename={window.__ROUTER_BASENAME__ || ''}>
|
||||
<Router basename={routerBasename}>
|
||||
<Routes>
|
||||
<Route path="/" element={<AppContent />} />
|
||||
<Route path="/session/:sessionId" element={<AppContent />} />
|
||||
|
||||
Reference in New Issue
Block a user