]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(matcher): remove unused params
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 16 Aug 2022 13:56:04 +0000 (15:56 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 16 Aug 2022 13:56:08 +0000 (15:56 +0200)
Fix #1497

NOTES: if you were relying on passing `params` that were not defined as
part of the `path`, eg: having a route defined as follows:

```js
{
  path: '/somewhere',
  name: 'somewhere'
}
```

And pushing with an _artificial_ param:

```js
router.push({ name: 'somewhere', params: { oops: 'gets removed' }})
```

This change will break your app. This behavior has worked in some
scenarios but has been **advised against** for years as it's an
antipattern in routing. You can still put the data in `query` or as an
actual param.

packages/router/__tests__/matcher/resolve.spec.ts
packages/router/src/matcher/index.ts

index 8e9171c15e9e75084fb975ef0959f6218611ac50..c3df1f6acb5b6c1af95e2019bcbc4fe0cb17fd7d 100644 (file)
@@ -775,6 +775,19 @@ describe('RouterMatcher.resolve', () => {
       )
     })
 
+    it('drops non existent params', () => {
+      assertRecordMatch(
+        { path: '/', name: 'home', components },
+        { name: 'home', params: { a: 'b' } },
+        { name: 'home', path: '/', params: {} }
+      )
+      assertRecordMatch(
+        { path: '/:b', name: 'a', components },
+        { name: 'a', params: { a: 'a', b: 'b' } },
+        { name: 'a', path: '/b', params: { b: 'b' } }
+      )
+    })
+
     it('drops optional params', () => {
       assertRecordMatch(
         { path: '/:a/:b?', name: 'p', components },
index a1f584e868b0053c1fb4bbbec5c6206d8632fb8c..bd922eb54b41485df1dde2599f03f7d974c5d94f 100644 (file)
@@ -256,7 +256,13 @@ export function createRouterMatcher(
           // TODO: only keep optional params coming from a parent record
           matcher.keys.filter(k => !k.optional).map(k => k.name)
         ),
-        location.params
+        // discard any existing params in the current location that do not exist here
+        // #1497 this ensures better active/exact matching
+        location.params &&
+          paramsFromLocation(
+            location.params,
+            matcher.keys.map(k => k.name)
+          )
       )
       // throws if cannot be stringified
       path = matcher.stringify(params)
@@ -275,7 +281,6 @@ export function createRouterMatcher(
       // matcher should have a value after the loop
 
       if (matcher) {
-        // TODO: dev warning of unused params if provided
         // we know the matcher works because we tested the regexp
         params = matcher.parse(path)!
         name = matcher.record.name