Replies: 4 comments 1 reply
-
React Router doesn't care about trailing slashes in matching, so I would expect
But if it always returns a trailing slash I'd like to change to one of the above. |
Beta Was this translation helpful? Give feedback.
-
The way it works currently in v5 is {
path: "", // the path we used to match the URL
url: "", // the portion of the URL pathname that was matched
isExact: bool, // whether or not we matched exactly (the entire URL was consumed)
params: {} // the URL params
} If they designed their site to use trailing slashes in their URLs, then The problem here is that people don't/can't redirect URLs that end with / to their non-/ equivalents on the server side, or vice versa. So when React Router kicks in on the client, they don't know if the current URL ends with a / or not. So they have to create a normalization function that adds/removes trailing slashes depending on what they want to do with Aside: using a client-side The best solution to this problem is for users to a) decide which URL structure they want to use throughout their site (either with trailing / or without) and then b) redirect accordingly on the server side. Then on the client side they would always know whether the current URL ends with a trailing / or not, and so they'd know if For people who can't redirect on the server for some reason, they should probably just strip the trailing slash before joining with another string. We could maybe provide a function like: function joinUrls(a: string, b: string): string {
return a.replace(/\/+$/, "") + b.replace(/^\/*/, "/");
}
<Link to={joinUrls(match.url, "some/path")}>some path</Link> What should we do in v5?If we change what My vote would be to provide the We should also add something to the documentation that says they should be stripping/adding the trailing slash in a server-side redirect for the sake of SEO. What should we do in v6?In v6 {
path: "", // the route path that was used to match
pathname: "", // the portion of the URL pathname that was matched
params: {} // the URL params that were matched
} We should probably change <Link to={`${match.url}/some/path`}>some path</Link> The only thing we lose if we make this change is you won't be able to determine if the current URL ends with a trailing slash, but I doubt many people are going to need that. They mostly just want We should also add a note in the v6 docs about configuring your server to add/remove the trailing slash in a redirect for the sake of SEO. This is not router-specific (i.e. they don't need to in order to use React Router), but it's just good practice. |
Beta Was this translation helpful? Give feedback.
-
Quick update: @chaance and I had a chat about this and we decided it's best to use the following shape in v6 for {
params: {}, // the URL params that were matched
pathname: '...', // the portion of the URL pathname that was matched
pattern: { path: '...', caseSensitive: bool, end: bool } // the route path/pattern that was used to match
} This is the same shape as objects returned from The |
Beta Was this translation helpful? Give feedback.
-
Site and app owners cannot fully "take control" of trailing slash patterns without redirecting because anyone linking to our site or just typing in the url can add a trailing slash. The V5 documentation explicitly calls out that the url component returned from useRouteMatch is "useful for building nested Links", but doesn't mention at all that a trailing slash could be present. Unless the route is matched using the "exact" option to a route with a trailing slash it seems unintuitive that the "portion of the URL that was matched" would include it. If the current state must be maintained then the documentation should be updated to mention it with an example of how to mitigate. In its current state it's very unintuitive and a lot of users are likely ruining their SEO with redirects because of it. |
Beta Was this translation helpful? Give feedback.
-
See #4841 for context.
Many users have expressed concern that
useRouteMatch
returns the trailing slash in matching URLs, which makes joining them with path segments unpredictable in some cases.#4841 shows several examples of current workarounds. Opening this discussion to explore various ways we might be able to make this simpler/more predictable and consistent in v6.
Beta Was this translation helpful? Give feedback.
All reactions