Trailing slash + `exactly`
See original GitHub issueHi there,
Apologies up front if this is the wrong place for this - wasn’t quite sure what category this issue falls into so I figured I’d start here. Also, sorry if I’ve missed something obvious in my search for a solution or to see if this had already been answered elsewhere.
Basically, I’m starting a new project with version 4.0.0-alpha.5 and have just a few routes right now:
<Match exactly pattern="/" component={Dashboard} />
<Match exactly pattern="/users/:id" component={User} />
<Match exactly pattern="/login" component={LoginForm} />
<Miss component={NotFoundError} />
I’m using exactly pretty liberally because I want to be able to navigate to /users/lol and get the user lol’s page, but I don’t want to navigate to /users/lol/cats and get the user lol’s page (rather, a 404), which is the behavior without exactly. So, this works well for my current use case.
However, I do want to be able to go to /users/lol/ with the trailing slash and have it show me the user lol’s page. As it stands now, the only way I can get it to work is to add another <Match>:
<Match exactly pattern="/" component={Dashboard} />
<Match exactly pattern="/users/:id" component={User} />
<Match exactly pattern="/users/:id/" component={User} />
<Match exactly pattern="/login" component={LoginForm} />
<Miss component={NotFoundError} />
It could be that I’m just missing an obvious way to make this work, whether that be with some regex pattern or otherwise, but this is the only way I could get the behavior I’m looking for.
I have also downloaded the path-to-regexp package separately and been able to reproduce the behavior I want with this code:
const pathToRegexp = require('path-to-regexp');
const keys = []
const re = pathToRegexp('/users/:id', keys, { end: true, strict: false });
From what I can tell, the difference between this code and React Router v4 is the strict option being set to false.
Thanks in advance for any help or feedback!
Issue Analytics
- State:
- Created 7 years ago
- Reactions:16
- Comments:13 (6 by maintainers)
Top Related StackOverflow Question
For those looking for a work around for now, I’m using this:
I had this problem also, it’s a pity, but found a way to work arround it
_url_/seasonand_url_/season/_url_/season/add,_url_/season/add/and_url_/season/add/32_url_/season/32