{ path: '/to-foo', redirect: '/foo' },
{ path: '/to-foo-named', redirect: { name: 'Foo' } },
{ path: '/to-foo2', redirect: '/to-foo' },
+ { path: '/to-foo-query', redirect: '/foo?a=2#b' },
{ path: '/to-p/:p', redirect: { name: 'Param' } },
{ path: '/p/:p', name: 'Param', component: components.Bar },
{ path: '/repeat/:r+', name: 'repeat', component: components.Bar },
expect(history.replace).toHaveBeenCalledWith('/foo', expect.anything())
})
+ it('parses query and hash with router.replace', async () => {
+ const history = createMemoryHistory()
+ const { router } = await newRouter({ history })
+ jest.spyOn(history, 'replace')
+ await router.replace('/foo?q=2#a')
+ expect(history.replace).toHaveBeenCalledTimes(1)
+ expect(history.replace).toHaveBeenCalledWith(
+ '/foo?q=2#a',
+ expect.anything()
+ )
+ })
+
it('replaces if a guard redirects', async () => {
const history = createMemoryHistory()
const { router } = await newRouter({ history })
})
})
+ it('handles query and hash passed in redirect string', async () => {
+ const history = createMemoryHistory()
+ const router = createRouter({ history, routes })
+ await expect(router.push('/to-foo-query')).resolves.toEqual(undefined)
+ expect(router.currentRoute.value).toMatchObject({
+ name: 'Foo',
+ path: '/foo',
+ params: {},
+ query: { a: '2' },
+ hash: '#b',
+ redirectedFrom: expect.objectContaining({
+ fullPath: '/to-foo-query',
+ }),
+ })
+ })
+
it('keeps query and hash when redirect is a string', async () => {
const history = createMemoryHistory()
const router = createRouter({ history, routes })
function locationAsObject(
to: RouteLocationRaw | RouteLocationNormalized
): Exclude<RouteLocationRaw, string> | RouteLocationNormalized {
- // FIXME: does not take into account query params
- return typeof to === 'string' ? { path: to } : assign({}, to)
+ return typeof to === 'string'
+ ? parseURL(parseQuery, to, currentRoute.value.path)
+ : assign({}, to)
}
function checkCanceledNavigation(
const lastMatched = to.matched[to.matched.length - 1]
if (lastMatched && lastMatched.redirect) {
const { redirect } = lastMatched
- // transform it into an object to pass the original RouteLocaleOptions
- let newTargetLocation = locationAsObject(
+ let newTargetLocation =
typeof redirect === 'function' ? redirect(to) : redirect
- )
+
+ if (typeof newTargetLocation === 'string') {
+ newTargetLocation =
+ newTargetLocation.indexOf('?') > -1 ||
+ newTargetLocation.indexOf('#') > -1
+ ? (newTargetLocation = locationAsObject(newTargetLocation))
+ : { path: newTargetLocation }
+ }
if (
__DEV__ &&
}
function pushWithRedirect(
- // TODO: should only be RouteLocation?
to: RouteLocationRaw | RouteLocation,
- // TODO: add replace here
redirectedFrom?: RouteLocation
): Promise<NavigationFailure | void | undefined> {
const targetLocation: RouteLocation = (pendingLocation = resolve(to))
if (shouldRedirect)
return pushWithRedirect(
- assign(shouldRedirect, { state: data, force, replace }),
+ assign(locationAsObject(shouldRedirect), {
+ state: data,
+ force,
+ replace,
+ }),
// keep original redirectedFrom if it exists
redirectedFrom || targetLocation
)