]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(guards): avoid enter guards between aliases
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 11 Mar 2021 15:24:37 +0000 (16:24 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 11 Mar 2021 15:24:37 +0000 (16:24 +0100)
__tests__/guards/beforeRouteEnter.spec.ts
src/router.ts

index 1a0a3b7e1d54b0c67b026b2ae27c5f17392ae43a..82e05418c05dc83bc0e1bcca0fe5fd478b721fa4 100644 (file)
@@ -29,6 +29,7 @@ const routes: RouteRecordRaw[] = [
   { path: '/foo', component: Foo },
   {
     path: '/guard/:n',
+    alias: '/guard-alias/:n',
     component: {
       ...Foo,
       beforeRouteEnter,
@@ -120,6 +121,20 @@ describe('beforeRouteEnter', () => {
     expect(beforeRouteEnter).toHaveBeenCalledTimes(1)
   })
 
+  it('does not call beforeRouteEnter guards on navigation between aliases', async () => {
+    const router = createRouter({ routes })
+    const spy = jest.fn()
+    beforeRouteEnter.mockImplementation(spy)
+    await router.push('/guard/valid')
+    expect(beforeRouteEnter).toHaveBeenCalledTimes(1)
+    await router.push('/guard-alias/valid')
+    expect(beforeRouteEnter).toHaveBeenCalledTimes(1)
+    await router.push('/guard-alias/other')
+    expect(beforeRouteEnter).toHaveBeenCalledTimes(1)
+    await router.push('/guard/other')
+    expect(beforeRouteEnter).toHaveBeenCalledTimes(1)
+  })
+
   it('calls beforeRouteEnter guards on navigation for nested views', async () => {
     const router = createRouter({ routes })
     await router.push('/nested/nested/foo')
index d445c384924fef730c972ffc3d22794c18d07303..d2c421a7f48ee6db1b1b4160528573f203cb98d4 100644 (file)
@@ -1174,8 +1174,9 @@ function extractChangingRecords(
     const recordTo = to.matched[i]
     if (recordTo) {
       // the type doesn't matter because we are comparing per reference
-      if (from.matched.indexOf(recordTo as any) < 0)
+      if (!from.matched.find(isSameRouteRecord.bind(null, recordTo))) {
         enteringRecords.push(recordTo)
+      }
     }
   }