Skip to content

Commit ded2d57

Browse files
committed
feat(experimental): basic alias
1 parent 7bb7500 commit ded2d57

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

packages/router/src/experimental/route-resolver/resolver-fixed.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ export interface EXPERIMENTAL_ResolverRecord_Base {
5555
* It will be included in the `matched` array of a resolved location.
5656
*/
5757
parent?: EXPERIMENTAL_ResolverRecord | null // the parent can be matchable or not
58-
59-
// TODO: implement aliases
60-
// aliasOf?: this
6158
}
6259

6360
/**

packages/router/src/experimental/router.spec.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ const childDefaultRawRecord: EXPERIMENTAL_RouteRecord_Matchable = {
8787
parent: parentWithRedirectRecord,
8888
}
8989

90+
const aliasRecordOriginal = normalizeRouteRecord({
91+
// path: '/basic',
92+
// alias: '/basic-alias',
93+
name: Symbol('basic-alias'),
94+
path: new MatcherPatternPathStatic('/basic'),
95+
components: { default: components.Foo },
96+
})
97+
const aliasRecord = normalizeRouteRecord({
98+
...aliasRecordOriginal,
99+
path: new MatcherPatternPathStatic('/basic-alias'),
100+
aliasOf: aliasRecordOriginal,
101+
})
102+
90103
const aliasParentRecord = normalizeRouteRecord({
91104
name: Symbol('aliases'),
92105
path: new MatcherPatternPathStatic('/aliases'),
@@ -258,13 +271,8 @@ const routeRecords: EXPERIMENTAL_RouteRecord_Matchable[] = [
258271
},
259272

260273
// aliases
261-
{
262-
// path: '/basic',
263-
// alias: '/basic-alias',
264-
name: Symbol('basic-alias'),
265-
path: new MatcherPatternPathStatic('/basic-alias'),
266-
components: { default: components.Foo },
267-
},
274+
aliasRecordOriginal,
275+
aliasRecord,
268276

269277
aliasChildOneRecord,
270278
aliasChildTwoRawRecord,
@@ -758,7 +766,26 @@ describe('Experimental Router', () => {
758766
})
759767

760768
describe('alias', () => {
761-
it.skip('does not navigate to alias if already on original record', async () => {})
769+
it('navigates to alias', async () => {
770+
const { router } = await newRouter()
771+
await router.push('/basic-alias')
772+
expect(router.currentRoute.value.path).toBe('/basic-alias')
773+
expect(router.currentRoute.value.matched.at(0)).toBe(aliasRecord)
774+
expect(router.currentRoute.value.matched.at(0)?.aliasOf).toBe(
775+
aliasRecordOriginal
776+
)
777+
})
778+
779+
it('does not navigate to alias if already on original record', async () => {
780+
const { router } = await newRouter()
781+
const spy = vi.fn()
782+
await router.push('/basic')
783+
expect(router.currentRoute.value.path).toBe('/basic')
784+
router.beforeEach(spy)
785+
await router.push('/basic-alias')
786+
expect(spy).not.toHaveBeenCalled()
787+
expect(router.currentRoute.value.path).toBe('/basic')
788+
})
762789

763790
it.skip('does not navigate to alias with children if already on original record', async () => {})
764791

packages/router/src/experimental/router.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ export interface EXPERIMENTAL_RouterOptions_Base extends PathParserOptions {
186186
*/
187187
export interface EXPERIMENTAL_RouteRecord_Base
188188
extends EXPERIMENTAL_ResolverRecord_Base {
189-
// TODO:
190189
/**
191190
* Where to redirect if the route is directly matched. The redirection happens
192191
* before any navigation guard and triggers a new navigation with the new
@@ -196,11 +195,9 @@ export interface EXPERIMENTAL_RouteRecord_Base
196195

197196
// TODO:
198197
/**
199-
* Aliases for the record. Allows defining extra paths that will behave like a
200-
* copy of the record. Allows having paths shorthands like `/users/:id` and
201-
* `/u/:id`. All `alias` and `path` values must share the same params.
198+
* References another record if this record is an alias of it.
202199
*/
203-
// alias?: string | string[]
200+
aliasOf?: unknown
204201

205202
// TODO: deprecate, expose utils to compare resolved routes, and document
206203
// how to create a meta field that does the same
@@ -357,7 +354,10 @@ export function normalizeRouteRecord(
357354
// must be defined as non enumerable because it contains modules
358355
// mods: {},
359356
props: {},
357+
// TODO :make it optional as it changes nothing
360358
parent: null,
359+
// not having the property changes nothing
360+
// aliasOf: null,
361361
...record,
362362
// FIXME: to be removed
363363
instances: {},

0 commit comments

Comments
 (0)