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

Link to "/" with basename adds extra trailing slash #6226

Closed
mmartha opened this issue Jun 25, 2018 · 5 comments
Closed

Link to "/" with basename adds extra trailing slash #6226

mmartha opened this issue Jun 25, 2018 · 5 comments

Comments

@mmartha
Copy link

mmartha commented Jun 25, 2018

Version

4.2.0

Test Case

https://codesandbox.io/s/6llwm7m8zz

Steps to reproduce

<BrowserRouter basename="/foo/bar">
    <Route path="/" component={App}/>
</BrowserRouter>
<Link to="/" />

Expected vs. Actual Behavior

the link takes me to http://host/foo/bar/ (with trailing slash) when I expect http://host/foo/bar (without trailing slash)

@timdorr
Copy link
Member

timdorr commented Jun 26, 2018

When using a basename, this is expected behavior for the root route.

@timdorr timdorr closed this as completed Jun 26, 2018
@mmartha
Copy link
Author

mmartha commented Jun 26, 2018

thanks @timdorr, I've implemented a server side redirect as a workaround, looks like I'll just stick with that!

Do you have any explanation or link to a discussion thread for why this was decided to be the expected behavior? It doesn't seem ideal since the root route is a file rather than a directory.
Additionally, it seems strange that the root route's behavior (wrt trailing slash) would differ when using basename vs. when not.

@ajenkins-kyr
Copy link

ajenkins-kyr commented Aug 17, 2018

Is there any way to get the behavior that @mmartha is looking for using basename without implementing some kind of redirect behavior? This seems like a very normal use case and I can't figure out a working solution other than not using basename altogether.

@mstarrrsui
Copy link

mstarrrsui commented Sep 29, 2018

When using a basename, this is expected behavior for the root route.

@timdorr Can you pls elaborate on why this is the case?

Given:

  <BrowserRouter basename="/foo/bar">
      <Route path="/" component={App}/>
      <Route path="/baz" component={SomeBaz}/>
  </BrowserRouter>

I'm struggling to understand why 'foo/bar' (root route) adds a trailing slash yet 'foo/bar/baz' does not add one. Other than redirects, is there anyway to stop this happening?

@lock lock bot locked as resolved and limited conversation to collaborators Nov 28, 2018
@brophdawg11
Copy link
Contributor

This use case should be achievable after the changes in #8861. The basename + link should now be able to control trailing slash behavior when routing to the root:

This setup:

<BrowserRouter basename="/app">
  <Routes>
    <Route path="" element={
        <>
          <Link to="" />  {/* No trailing slash */}
          <Link to="/" /> {/* Trailing slash */}
        </>
      }
    />
  </Routes>
</BrowserRouter>

Should render:

<a href="/app">
<a href="/app/">

The same goes with navigations upwards to the root - <Link to="../.."> would not include a trailing slash but <Link to="../../"> would.

The new logic should be such that if either basename or the <Link to> have a trailing slash, the resulting URL should have a trailing slash. If neither have one, the resulting URL will not.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants