I need to use a hash router with a basename. I did this with the createHashRouter (React Router 6.8), with the basename property to render the app in the sub-route. When I open /sub/route in the browser I get the empty page though, only css background is visible.
All of the files and assets are shipped to the app, but the JS is not executed and the root div stays empty.
The code of the Router:
const router = createHashRouter([
{
id: 'root',
path: '/',
element: <App />,
errorElement: <ErrorPage />,
loader: filtersLoader(queryClient),
children: [
{ path: '/', element: <Navigate to="/dashboard/performance" /> },
{
path: '/dashboard',
id: 'dashboard',
errorElement: <ErrorPage />,
loader: dataLoader(queryClient),
element: (
<>
<Header />
<Outlet />
</>
),
children: [
{ index: true, element: <Index /> }
]
},
{
path: 'info',
element: <Info />
}
]
}
], {
basename: '/sub/route'
});
Update/solution
the issue was caused by mixing HashRouter with basename. Getting rid of the basename in createHashRouter solved the issue.
The
HashRouterwill basically already just work from whatever directory you deploy/serve the React app from, specifying abasenamedoesn't make as much since here as it does for theBrowserRouterwhere the routes/links need it as part of their resolved paths.The
HashRouteruses the URL hash for routing, so for example if you add that one has abasename="/sub/route"then you are specifying that all routes and links operate relative to it where it's rendered. If the app is hosted/served from"https://www.example.com/subA/subB"then you are effectively saying"https://www.example.com/subA/subB/#/sub/route"is where the app is running and the browser will need to be navigated to this URL to start the proper routing.If
"/sub/route"is already where the app is hosted/served from then there's nothing to set as abasenameif you want to keep the default behavior of routing/linking relative to a"/"path on"https://www.example.com/sub/route/#/".Remove the
basenameproperty from the routing configuration.