From 97e1df983c058265423c96e1d475c2ce2c43c492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Racek?= Date: Fri, 29 Mar 2024 19:39:03 +0100 Subject: [PATCH 1/2] docs: mention an edge case with mutliple optional params in the docs See #2190 for more info. --- packages/docs/guide/essentials/route-matching-syntax.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/docs/guide/essentials/route-matching-syntax.md b/packages/docs/guide/essentials/route-matching-syntax.md index 52f8812a7..b9338ea05 100644 --- a/packages/docs/guide/essentials/route-matching-syntax.md +++ b/packages/docs/guide/essentials/route-matching-syntax.md @@ -114,6 +114,15 @@ const routes = [ Note that `*` technically also marks a parameter as optional but `?` parameters cannot be repeated. +When using multiple optional parameters in succession, the behavior is slightly different because the parser expects at least one of them to match. + +```js +const routes = [ + // This will match /users/42posva, /users/42, /users/posva but not /users, + { path: '/users/:userId(\\d+)?:name(\\w+)?' }, +] +``` + ## Debugging If you need to dig how your routes are transformed into a regex to understand why a route isn't being matched or, to report a bug, you can use the [path ranker tool](https://paths.esm.dev/?p=AAMeJSyAwR4UbFDAFxAcAGAIJXMAAA..#). It supports sharing your routes through the URL. From ac4e94b83ed8abd42e0f6d6e679e9ccddedb6380 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Tue, 9 Jul 2024 09:59:33 +0200 Subject: [PATCH 2/2] Update packages/docs/guide/essentials/route-matching-syntax.md --- .../docs/guide/essentials/route-matching-syntax.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/docs/guide/essentials/route-matching-syntax.md b/packages/docs/guide/essentials/route-matching-syntax.md index b9338ea05..44a508341 100644 --- a/packages/docs/guide/essentials/route-matching-syntax.md +++ b/packages/docs/guide/essentials/route-matching-syntax.md @@ -114,14 +114,12 @@ const routes = [ Note that `*` technically also marks a parameter as optional but `?` parameters cannot be repeated. -When using multiple optional parameters in succession, the behavior is slightly different because the parser expects at least one of them to match. +If the route segment contains more than **just an optional parameter**, it won't match a path **without the trailing slash**. For example: -```js -const routes = [ - // This will match /users/42posva, /users/42, /users/posva but not /users, - { path: '/users/:userId(\\d+)?:name(\\w+)?' }, -] -``` +- `/users/:uid?-:name?` won't match `/users`, only `/users/-` or even `/users/-/` +- `/users/:uid(\\d+)?:name? won't match `/users`, only `/users/`, `/users/2`, `/users/2/`, etc + +You can play around with the matching syntax [in the playground](https://paths.esm.dev/?p=AAMsIPQg4AoKzidgQFoEXAmw-IEBBRYYOE0SkABTASiz1qgBpgQA1QTsFjAb3h2onsmlAmGIFsCXjXh4AIA.&t=/users/2/#) ## Debugging