diff --git a/src/main.tsx b/src/main.tsx index 9576dda..13867dc 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -4,9 +4,11 @@ import { BrowserRouter } from 'react-router-dom'; import App from './App'; import './index.css'; +const routerBasename = import.meta.env.BASE_URL.replace(/\/+$/, '') || '/'; + createRoot(document.getElementById('root')!).render( - + diff --git a/src/routes.ts b/src/routes.ts index e74dad0..9c70e8e 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -45,13 +45,22 @@ export const navItems: NavItem[] = [ // }; const appRoutes: AppRoute[] = [...navItems.map((item) => item.id), 'profile']; +const publicBasePath = import.meta.env.BASE_URL.replace(/\/+$/, ''); + +const stripPublicBasePath = (pathname: string): string => { + if (!publicBasePath || publicBasePath === '/') return pathname; + if (pathname === publicBasePath) return '/'; + if (pathname.startsWith(`${publicBasePath}/`)) return pathname.slice(publicBasePath.length) || '/'; + return pathname; +}; export const isAppRoute = (value: string): value is AppRoute => appRoutes.includes(value as AppRoute); export const getRoutePath = (route: AppRoute) => routePaths[route]; export const getRouteFromPathname = (pathname: string): AppRoute | null => { - const normalizedPath = pathname === '/' ? routePaths[defaultRoute] : pathname.replace(/\/+$/, ''); + const routePathname = stripPublicBasePath(pathname); + const normalizedPath = routePathname === '/' ? routePaths[defaultRoute] : routePathname.replace(/\/+$/, ''); const entry = Object.entries(routePaths).find(([, path]) => path === normalizedPath); return entry ? (entry[0] as AppRoute) : null; };