hookrouter icon indicating copy to clipboard operation
hookrouter copied to clipboard

Handle trailing slashes?

Open gilbert opened this issue 4 years ago • 2 comments

Assuming the following routes:

const routes = {
  '/docs': () =>
    <h1>My Docs</h1>
  ,
}

If the user visits /docs/ in their browser, the route does not match.

As a workaround I have to do useRedirect('/docs/', '/docs')

gilbert avatar Dec 17 '19 05:12 gilbert

Hi @Paratron , I am new to Github contribution, I want to kick off with this for the first one, I found that updating regex can solve it.

new RegExp(`${inRoute.substr(0, 1) === '*' ? '' : '^'}${inRoute.replace(/:[a-zA-Z]+/g, '([^/]+)').replace(/\*/g, '')}${inRoute.substr(-1,) === '*' ? '/*' : '/*$'}`)

This can match /docs and /docs/ too. What do you think? If it's ok, I am willing to create a PR.

tonynguyenit18 avatar Jun 25 '20 17:06 tonynguyenit18

I would be interested in as well. Any chance get get this into one of the next releases?

grindsa avatar Jan 09 '21 10:01 grindsa