hookrouter
hookrouter copied to clipboard
URL redirection does not keep URL parameters
Given this first-level routes:
const routes = {
'/models': () => <ManageModelsPage />,
'/company/:companyId*': ({ companyId }) => <CompanyMainPage companyId={companyId} />
};
In the CompanyMainPage component I have the following child routes:
const routes = {
'/inbound': () => <InboundDevicesPage />
};
Inside the CompanyMainPage component I've setup a redirect from \ to \inbound like so:
function CompanyMainPage({ companyId }) {
//...
useRedirect('/', '/inbound');
const routeResult = useRoutes(routes);
return routeResult || null;
}
I tought that the URL would be /company/<SOME-GUID>/inbound after redirection but the URL becomes /company/inbound, losing the URL parameter :companyId.
Is there a way to keep the URL parameters after redirection ?
Edit:
By looking more into it I've noticed that the second part of the url (in this example: .../:companyId) gets removed wheter it is a parameter or not.
This happens in router.js:37 when the URL object constructs the new URL, the issue can be avoided if the parameter current ends with a backslash.
See this example (open the Console): https://jsfiddle.net/0bqve6w9/
This seems to be a bug, indeed. As a workaround, can you try to redirect to ./inbound instead of /inbound? Maybe this helps.
Running into the same issue here. It isn't even specific to redirects, it happens even with normal navigation when your path doesn't end with a slash. Heck, even navigating to ./ blows away the url parameter at the end. It's like it considers non slash-ended paths as "files" rather than "directories", and it considers "./" a level up.
My router chain looks something like /game* -> /:gameId* -> (/, /content/:contentId), with the gameId param specifying a prop to be passed to this detail page, and then the detail page being responsible for showing some specific content as an overlay by navigating relatively. Within that page, when you navigate to content/... or ./content/... or even ./, it destroys that last segment specifying the gameId unless the URL ends with a slash, totally breaking the routing.