]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(query): isSameRouteLocation compares queries by string
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 24 Jun 2020 10:02:14 +0000 (12:02 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 24 Jun 2020 10:02:14 +0000 (12:02 +0200)
Fix #328

__tests__/location.spec.ts
src/location.ts
src/router.ts

index 9be6c3a690f076c1ef6837f2cc9aed206467e0e2..5399d549b903a58c34278ab1ebfe8d57f33beccf 100644 (file)
@@ -5,7 +5,9 @@ import {
   stringifyURL as originalStringifyURL,
   stripBase,
   isSameRouteLocationParams,
+  isSameRouteLocation,
 } from '../src/location'
+import { RouteLocationNormalizedLoaded } from 'src'
 
 describe('parseURL', () => {
   let parseURL = originalParseURL.bind(null, parseQuery)
@@ -273,3 +275,26 @@ describe('isSameRouteLocationParams', () => {
     expect(isSameRouteLocationParams({ a: ['3'] }, { a: '2' })).toBe(false)
   })
 })
+
+describe('isSameRouteLocation', () => {
+  it('compares queries as strings', () => {
+    const location: RouteLocationNormalizedLoaded = {
+      path: '/',
+      params: {},
+      name: 'home',
+      matched: [{} as any],
+      fullPath: '/',
+      hash: '',
+      meta: {},
+      query: {},
+      redirectedFrom: undefined,
+    }
+    expect(
+      isSameRouteLocation(
+        () => 'fake query',
+        { ...location, query: { a: 'a' } },
+        { ...location, query: { b: 'b' } }
+      )
+    ).toBe(true)
+  })
+})
index 2bccbd51d5911d9b979339fc5e8e384bd7d3635a..3d7155b49b8a28a94a5f07d8897666189346d250 100644 (file)
@@ -1,4 +1,4 @@
-import { LocationQuery, LocationQueryRaw, LocationQueryValue } from './query'
+import { LocationQuery, LocationQueryRaw } from './query'
 import {
   RouteLocation,
   RouteLocationNormalized,
@@ -127,6 +127,7 @@ export function stripBase(pathname: string, base: string): string {
  * @param b second {@link RouteLocation}
  */
 export function isSameRouteLocation(
+  stringifyQuery: (query: LocationQueryRaw) => string,
   a: RouteLocation,
   b: RouteLocation
 ): boolean {
@@ -138,7 +139,7 @@ export function isSameRouteLocation(
     aLastIndex === bLastIndex &&
     isSameRouteRecord(a.matched[aLastIndex], b.matched[bLastIndex]) &&
     isSameRouteLocationParams(a.params, b.params) &&
-    isSameRouteLocationParams(a.query, b.query) &&
+    stringifyQuery(a.query) === stringifyQuery(b.query) &&
     a.hash === b.hash
   )
 }
@@ -157,17 +158,9 @@ export function isSameRouteRecord(a: RouteRecord, b: RouteRecord): boolean {
   return (a.aliasOf || a) === (b.aliasOf || b)
 }
 
-export function isSameRouteLocationParams(
-  a: RouteLocationNormalized['query'],
-  b: RouteLocationNormalized['query']
-): boolean
 export function isSameRouteLocationParams(
   a: RouteLocationNormalized['params'],
   b: RouteLocationNormalized['params']
-): boolean
-export function isSameRouteLocationParams(
-  a: RouteLocationNormalized['query' | 'params'],
-  b: RouteLocationNormalized['query' | 'params']
 ): boolean {
   if (Object.keys(a).length !== Object.keys(b).length) return false
 
@@ -178,25 +171,9 @@ export function isSameRouteLocationParams(
   return true
 }
 
-function isSameRouteLocationParamsValue(
-  a: LocationQueryValue | LocationQueryValue[],
-  b: LocationQueryValue | LocationQueryValue[]
-): boolean
 function isSameRouteLocationParamsValue(
   a: RouteParamValue | RouteParamValue[],
   b: RouteParamValue | RouteParamValue[]
-): boolean
-function isSameRouteLocationParamsValue(
-  a:
-    | LocationQueryValue
-    | LocationQueryValue[]
-    | RouteParamValue
-    | RouteParamValue[],
-  b:
-    | LocationQueryValue
-    | LocationQueryValue[]
-    | RouteParamValue
-    | RouteParamValue[]
 ): boolean {
   return Array.isArray(a)
     ? isEquivalentArray(a, b)
index 392872ea319cb960461096e7d9d24c2602b7b2d9..f9a4217d5b4648598333797386dfd151eb361e2a 100644 (file)
@@ -427,7 +427,7 @@ export function createRouter(options: RouterOptions): Router {
     toLocation.redirectedFrom = redirectedFrom
     let failure: NavigationFailure | void | undefined
 
-    if (!force && isSameRouteLocation(from, targetLocation)) {
+    if (!force && isSameRouteLocation(stringifyQuery, from, targetLocation)) {
       failure = createRouterError<NavigationFailure>(
         ErrorTypes.NAVIGATION_DUPLICATED,
         { to: toLocation, from }