Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Absolute paths in react router v6 #8848

Closed
ryanryanorient opened this issue May 6, 2022 · 2 comments
Closed

[Feature]: Absolute paths in react router v6 #8848

ryanryanorient opened this issue May 6, 2022 · 2 comments
Labels

Comments

@ryanryanorient
Copy link

What is the new or updated feature that you are suggesting?

I want to use absolute path navigation in nested routes but I can't find the relevant api

The original path is /a/b/c
I wish I could render /a/b/c from url /d

Why should this feature be included?

It is risky to expose the original url directly
All components can be easily guessed by a hacker

@JesusTheHun
Copy link
Contributor

I agree with OP, but I think a bit of context is required here.

I put my routes in contants, so I can use generatePath() safely.
Since I don't want define all my routes in the same place, I let each component that host a <Routes> define it's sub routes.

// App.tsx
export const rootRoutes = {
  settings: `settings/*`
}; 
return <App>
  <Routes>
    ...
    <Route path={rootRoutes['settings']} element={<SettingsPage />} />
  </Routes>
</App>

/// SettingsPage.tsx
export const settingsRoutes = {...};
return <SettingsPage>
  <Routes>
    <Route path={settingsRoutes['user-settings']} element={<UserSettingsPage>} />
  </Routes>
<SettingsPage/>

The problem is that when I want to generate the path to a setting page, I want to use

generatePath(settingsRoutes['user-settings'])

But this path contains only the relative path. So I need to work things around :

export const settingsRoute = joinRoutes(rootRoutes['settings'], { ... });

Now I have another problem. Because react-router does not support absolute path in sub-routes, I have to find my way around, again :

/// SettingsPage.tsx
const subRoutes = { ... };
export const settingsRoutes = joinRoutes(rootRoutes['settings'], subRoutes);
return <SettingsPage>
  <Routes>
    <Route path={subRoutes['user-settings']} element={<UserSettingsPage>} />
  </Routes>
<SettingsPage/>
Click to see `joinRoutes` definition
export const joinRoutes = (rootRoute: string, routes: Record<string, string>): Record<string, string> => {
  const wildcardlessRootRoute = rootRoute.replace('/*', '');
  const joinedRoutes: Record<string, string> = {};

  Object.entries(routes).forEach(([key, value]) => {
    joinedRoutes[key] = wildcardlessRootRoute + '/' + value;
  })

  return joinedRoutes;
}

Clearly this is a flawed way to go, as this would not support several scenarios but it works for now.
I believe my issue is faced by many as I don't believe anyone uses plain text links in their code.

@timdorr
Copy link
Member

timdorr commented May 7, 2022

This is already implemented. You prefix your path with / to go to an absolute route:

<Link to="something">Go to a nested route</Link>
<Link to="/anything">Go to an absolute route</Link>

@timdorr timdorr closed this as completed May 7, 2022
brophdawg11 pushed a commit that referenced this issue Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants