react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Make trailing slash optional

Open aldendaniels opened this issue 11 years ago • 32 comments

Desired behavior:

  1. Always allow an optional trailing slash
  2. Never add an optional trailing slash when generating links via makePath

I think this behavior is pretty standard.

Today (as of v0.12.9) makePath appends a trailing slash if a trailing slash is optionally allowed.

Food for thought (tangential): It would be nice to support optional trailing slashes automatically without the need to add a /? to all routes.

aldendaniels avatar Feb 11 '15 16:02 aldendaniels

For me, the desired behavior would be

  1. Force trailing slash for routes (via redirect), but only for routes with children/DefaultRoutes ("directories")
  2. Always add trailing slash to links to those routes

So I don't think changing the default behavior is the ideal solution. But it would be nice to have some more control over this.

agundermann avatar Feb 11 '15 19:02 agundermann

+1

jkimbo avatar Mar 22 '15 17:03 jkimbo

+1

romeovs avatar Apr 09 '15 13:04 romeovs

+1

mikepro4 avatar Apr 24 '15 17:04 mikepro4

This is really an important issue. However, since the issue hasn’t been resolved, what is the workaround?

Frikki avatar Apr 24 '15 20:04 Frikki

+1

aronwoost avatar Apr 27 '15 17:04 aronwoost

+1

super-cache-money avatar May 04 '15 07:05 super-cache-money

Yep, very annoying issue. Path must be handled with and without trailing slash by default as it is not classic REST router.

mbektimirov avatar May 09 '15 17:05 mbektimirov

@ryanflorence @mjackson will this be addressed/changed in #1158? Does it make sense to implement it on the current code base, or should I rather start based on the code in #1158 ?

dignifiedquire avatar May 11 '15 16:05 dignifiedquire

@Dignifiedquire This is already fixed in #1158

mjackson avatar May 17 '15 00:05 mjackson

@mjackson how to solve this issue now?

fxxkscript avatar Jun 11 '15 08:06 fxxkscript

its now optional in 1.0

ryanflorence avatar Jun 16 '15 16:06 ryanflorence

workaround for this in 0.13.2 ?

kevzettler avatar Jul 01 '15 19:07 kevzettler

@kevzettler add /? to the url

fxxkscript avatar Jul 02 '15 13:07 fxxkscript

workaround for this in 0.13.2 ?

+1

TimDaub avatar Jul 21 '15 14:07 TimDaub

/? is not work in 1.0.0-beta3?

rtm7777 avatar Jul 23 '15 19:07 rtm7777

It is not working in Link component. Class "active" is not added in case of trailing slash.

In the method Router#isActive are compared wrong strings:

to === state.path

which in my case of using /? in route's path looks like this:

"/about" === "/about/"

tommmyy avatar Aug 28 '15 07:08 tommmyy

Hi guys! Can't you just redirect every path with trailing slash to the path without it on server (using express for example)? It completely solved this issue for us.

th0r avatar Aug 28 '15 09:08 th0r

@th0r yes I ended up with unification of all routes

tommmyy avatar Aug 28 '15 10:08 tommmyy

(On 1.0.0-rc3) This is strange, but when using IndexRedirect, having a trailing slash on the root path caused a problem for me.

If I do this (a slash after admin):

  <Route path='/admin/' component={App}>
    <IndexRedirect to='posts' />

Then if I open a new window and go to localhost:.../admin/ it gets redirected to /admin/posts correctly, BUT if I open a new window and go to localhost:.../admin (without a slash at the end) it gives me an error:

Warning: Location "/admin" did not match any routes

To fix this, I had to remove the trailing slash from the root component:

  <Route path='/admin' component={App}>
    <IndexRedirect to='posts' />

I wanted to write a failing test but couldn't figure out how: https://github.com/rackt/react-router/blob/c025177a10785f96f7683d4f033f6acdd46c1f2e/modules/tests/IndexRedirect-test.js#L20-L31

If someone could help me write a failing test, I'd create a new issue.

chibicode avatar Oct 27 '15 13:10 chibicode