stencil-community/stencil-router

Incorrect match when multiple urlMatch attributes are defined as regular expressions

ltm opened this issue · 0 comments

ltm commented

Stencil version:

ionic-docs@1.0.0
└── @stencil/router@1.0.1 

I'm submitting a ... (check one with "x")

Current behavior:
If multiple <stencil-route-link> elements define urlMatch using regular expressions, only one of the elements will correctly match the URL.

Expected behavior:
Multiple <stencil-route-link> elements should be able to define urlMatch using regular expressions.

Steps to reproduce:
Define multiple <stencil-route-link> elements with urlMatch using regular expressions:

<stencil-route-link url="/docs/" urlMatch={[/^\/docs(?!\/(api|components|cli|native)).*$/]}>Guide</stencil-route-link>
<stencil-route-link url="/docs/appflow" urlMatch={[/^\/docs\/appflow(?!\/(tutorial)).*$/]}>Guide</stencil-route-link>

Notice that only one of the two elements will match its URL.

Related code:
The compilePath() function uses JSON.stringify() to generate a cache pattern, cachePattern. If pattern is a regular expression or an array of regular expressions, the cache pattern will contain a non-unique value such as "{}" or "[{}]":

https://github.com/ionic-team/stencil-router/blob/228e1f3888759d651aa436ea3bfe5201e5872803/packages/router/src/utils/match-path.ts#L15-L18

Other information:
Given the pattern [/^\/docs(?!\/(api|components|cli|native)).*$/] the compilePath() function generates the cache pattern "[{}]":

Screen Shot 2020-11-11 at 12 55 16 PM