Skip to content

Commit

Permalink
fix(link): navigate to the alias path
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Mar 12, 2020
1 parent 392c295 commit 3284110
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
29 changes: 28 additions & 1 deletion __tests__/matcher/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('Router Matcher', () => {
)
})

it('resolves an alias with children', () => {
it('resolves an alias with children to the alias when using the path', () => {
const children = [{ path: 'one', component, name: 'nested' }]
assertRecordMatch(
{
Expand All @@ -182,6 +182,33 @@ describe('Router Matcher', () => {
}
)
})

it('resolves the original path of the named children of a route with an alias', () => {
const children = [{ path: 'one', component, name: 'nested' }]
assertRecordMatch(
{
path: '/parent',
alias: '/p',
component,
children,
},
{ name: 'nested' },
{
path: '/parent/one',
name: 'nested',
params: {},
matched: [
{
path: '/parent',
children,
components,
aliasOf: undefined,
},
{ path: '/parent/one', name: 'nested', components },
],
}
)
})
})

describe('LocationAsPath', () => {
Expand Down
15 changes: 14 additions & 1 deletion src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export function createRouterMatcher(
// while (i < matchers.length && matcher.score <= matchers[i].score) i++
matchers.splice(i, 0, matcher)
// only add the original record to the name map
if (matcher.record.name && !matcher.record.aliasOf)
if (matcher.record.name && !isAliasRecord(matcher))
matcherMap.set(matcher.record.name, matcher)
}

Expand Down Expand Up @@ -245,4 +245,17 @@ export function normalizeRouteRecord(
}
}

/**
* Checks if a record or any of its parent is an alias
* @param record
*/
function isAliasRecord(record: RouteRecordMatcher | undefined): boolean {
while (record) {
if (record.record.aliasOf) return true
record = record.parent
}

return false
}

export { PathParserOptions }
20 changes: 15 additions & 5 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,19 @@ export function createRouter({
}
}

function push(to: RouteLocation): Promise<RouteLocationNormalized> {
function push(
to: RouteLocation | RouteLocationNormalized
): Promise<RouteLocationNormalized> {
return pushWithRedirect(to, undefined)
}

async function pushWithRedirect(
to: RouteLocation,
to: RouteLocation | RouteLocationNormalized,
redirectedFrom: RouteLocationNormalized | undefined
): Promise<RouteLocationNormalized> {
const toLocation: RouteLocationNormalized = (pendingLocation = resolve(to))
const toLocation: RouteLocationNormalized = (pendingLocation =
// Some functions will pass a normalized location and we don't need to resolve it again
typeof to === 'object' && 'matched' in to ? to : resolve(to))
const from: RouteLocationNormalized = currentRoute.value
// @ts-ignore: no need to check the string as force do not exist on a string
const force: boolean | undefined = to.force
Expand Down Expand Up @@ -222,12 +226,18 @@ export function createRouter({
triggerError(error)
}

finalizeNavigation(toLocation, from, true, to.replace === true)
finalizeNavigation(
toLocation,
from,
true,
// RouteLocationNormalized will give undefined
(to as RouteLocation).replace === true
)

return currentRoute.value
}

function replace(to: RouteLocation) {
function replace(to: RouteLocation | RouteLocationNormalized) {
const location = typeof to === 'string' ? { path: to } : to
return push({ ...location, replace: true })
}
Expand Down

0 comments on commit 3284110

Please sign in to comment.