Skip to content

Commit

Permalink
fix(query): isSameRouteLocation compares queries by string
Browse files Browse the repository at this point in the history
Fix #328
  • Loading branch information
posva committed Jun 24, 2020
1 parent 2bb0db9 commit 6e1f0ea
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
25 changes: 25 additions & 0 deletions __tests__/location.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
})
})
29 changes: 3 additions & 26 deletions src/location.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LocationQuery, LocationQueryRaw, LocationQueryValue } from './query'
import { LocationQuery, LocationQueryRaw } from './query'
import {
RouteLocation,
RouteLocationNormalized,
Expand Down Expand Up @@ -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 {
Expand All @@ -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
)
}
Expand All @@ -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

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 6e1f0ea

Please sign in to comment.