question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there anyway to handle an optional trailing`/` in route path without using `*`?

See original GitHub issue

I have this routes definition

const routes = {
    '/foo': () => <Foo />,
    '/bar': () => <Bar />,
    '/qux': () => <Qux />,
    '/qux/:quxId': ({ quxId }) => <Bar quxId={quxId}/>,
};

As you can see there is a special case when 'qux/:quxId' routes to a different view than '/qux' (In fact it routes to the view for '/bar').

I want to be able to handle any of the routes also being visited with a trailing / (for example '/foo/').

I can get it to work with * but it has a couple of downsides.

  1. Because * can match anything, not just the trailing /, routes are able to be activated with an invalid path (/foot for instance)
  2. It depends on ordering '/qux/:quxId' before '/qux' so that the * doesn’t clobber the more specific path. This feels pretty fragile, especially because routes is an object, not an array.

The other alternative I can see is to define 2 paths for each route, one with the slash and one without.

const routes = {
    '/foo': () => <Foo />,
    '/foo/': () => <Foo />,
    '/bar': () => <Bar />,
    '/bar/': () => <Bar />,
    '/qux': () => <Qux />,
    '/qux/': () => <Qux />,
    '/qux/:quxId': ({ quxId }) => <Bar quxId={quxId}/>,
};

Obviously that is not very appealing

Is there any other way to address this special case of optionally allowing a trailing slash, or is it something you would consider adding?

Thanks as always 👍

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
micmcgcommented, Sep 24, 2019

Yeah I think if the solution was to do '/about/?' you would need to expose the fact that the path matcher is a regex in your API which probably creates maintenance headaches for you.

I think it’s the kind of thing you would want to apply to all your paths if you want it (I know that would be the case for us), so an option for useRoutes would be easier than anything that has to be done per route definition IMO.

So that’s a vote for a matchTrailingSlashes option

1reaction
kyeoticcommented, Sep 24, 2019

@micmcg added option matchTrailingSlash published under 0.5.5

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Router with optional path parameter - Stack Overflow
My question is, can we declare it in one route? If I add only the second row then the route without the parameter...
Read more >
Make trailing slash optional · Issue #820 · remix-run/react-router
Desired behavior: Always allow an optional trailing slash; Never add an optional trailing slash when generating links via makePath.
Read more >
Trailing / in route patterns - Slim Framework
Slim treats a URL pattern with a trailing slash as different to one without. That is, /user and /user/ are different and so...
Read more >
hapi — Ignore Trailing Slashes on Route Paths - Future Studio
The „bad solution“ is to just use a single route with an optional path parameter instead of two separate routes. Within the route...
Read more >
Router options - router5
By default, the router is not in "strict match" mode. If you want trailing slashes to not be optional, you can set strictTrailingSlash...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found