From: Eduardo San Martin Morote Date: Mon, 17 Mar 2025 13:42:46 +0000 (+0100) Subject: docs: note about slow routes X-Git-Tag: v4.5.1~17 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ce1eb3e0aa36613488a892e4840b849723f76c04;p=thirdparty%2Fvuejs%2Frouter.git docs: note about slow routes --- diff --git a/packages/docs/guide/essentials/route-matching-syntax.md b/packages/docs/guide/essentials/route-matching-syntax.md index 23a53179..febc76c3 100644 --- a/packages/docs/guide/essentials/route-matching-syntax.md +++ b/packages/docs/guide/essentials/route-matching-syntax.md @@ -124,3 +124,25 @@ You can play around with the matching syntax [in the playground](https://paths.e ## 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. + +## Avoiding slow regex + +When using custom regex, make sure to avoid using slow regex patterns. For example, using `.*` will match any character and can lead to **serious performance issues** if it's combined with a repeatable modifier `*` or `+` and anything after it: + +```ts +const routes = [ + // This creates a very slow regex because of the greedy `.*` followed by `*` and a static string + { path: '/:pathMatch(.*)*/something-at-the-end' }, +] +``` + +In practice, use these _match everything_ params only **in the very end of the URL**. If you need them in the middle of the path, **do not make them repeatable**: + +```ts +const routes = [ + // This is fine because the `.*` is at the end + { path: '/:pathMatch(.*)/something-at-the-end' }, +] +``` + +This matches the same routes but without an array of params and it's much faster.