From: Eduardo San Martin Morote Date: Sat, 18 Oct 2025 10:20:25 +0000 (+0200) Subject: feat(experimental): basic alias X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ded2d578954752e5d1fb3f0ed0fc028bb7bd0786;p=thirdparty%2Fvuejs%2Frouter.git feat(experimental): basic alias --- diff --git a/packages/router/src/experimental/route-resolver/resolver-fixed.ts b/packages/router/src/experimental/route-resolver/resolver-fixed.ts index 8533b25d..92b06b88 100644 --- a/packages/router/src/experimental/route-resolver/resolver-fixed.ts +++ b/packages/router/src/experimental/route-resolver/resolver-fixed.ts @@ -55,9 +55,6 @@ export interface EXPERIMENTAL_ResolverRecord_Base { * It will be included in the `matched` array of a resolved location. */ parent?: EXPERIMENTAL_ResolverRecord | null // the parent can be matchable or not - - // TODO: implement aliases - // aliasOf?: this } /** diff --git a/packages/router/src/experimental/router.spec.ts b/packages/router/src/experimental/router.spec.ts index bc148288..de83956f 100644 --- a/packages/router/src/experimental/router.spec.ts +++ b/packages/router/src/experimental/router.spec.ts @@ -87,6 +87,19 @@ const childDefaultRawRecord: EXPERIMENTAL_RouteRecord_Matchable = { parent: parentWithRedirectRecord, } +const aliasRecordOriginal = normalizeRouteRecord({ + // path: '/basic', + // alias: '/basic-alias', + name: Symbol('basic-alias'), + path: new MatcherPatternPathStatic('/basic'), + components: { default: components.Foo }, +}) +const aliasRecord = normalizeRouteRecord({ + ...aliasRecordOriginal, + path: new MatcherPatternPathStatic('/basic-alias'), + aliasOf: aliasRecordOriginal, +}) + const aliasParentRecord = normalizeRouteRecord({ name: Symbol('aliases'), path: new MatcherPatternPathStatic('/aliases'), @@ -258,13 +271,8 @@ const routeRecords: EXPERIMENTAL_RouteRecord_Matchable[] = [ }, // aliases - { - // path: '/basic', - // alias: '/basic-alias', - name: Symbol('basic-alias'), - path: new MatcherPatternPathStatic('/basic-alias'), - components: { default: components.Foo }, - }, + aliasRecordOriginal, + aliasRecord, aliasChildOneRecord, aliasChildTwoRawRecord, @@ -758,7 +766,26 @@ describe('Experimental Router', () => { }) describe('alias', () => { - it.skip('does not navigate to alias if already on original record', async () => {}) + it('navigates to alias', async () => { + const { router } = await newRouter() + await router.push('/basic-alias') + expect(router.currentRoute.value.path).toBe('/basic-alias') + expect(router.currentRoute.value.matched.at(0)).toBe(aliasRecord) + expect(router.currentRoute.value.matched.at(0)?.aliasOf).toBe( + aliasRecordOriginal + ) + }) + + it('does not navigate to alias if already on original record', async () => { + const { router } = await newRouter() + const spy = vi.fn() + await router.push('/basic') + expect(router.currentRoute.value.path).toBe('/basic') + router.beforeEach(spy) + await router.push('/basic-alias') + expect(spy).not.toHaveBeenCalled() + expect(router.currentRoute.value.path).toBe('/basic') + }) it.skip('does not navigate to alias with children if already on original record', async () => {}) diff --git a/packages/router/src/experimental/router.ts b/packages/router/src/experimental/router.ts index dfa617bd..919835bc 100644 --- a/packages/router/src/experimental/router.ts +++ b/packages/router/src/experimental/router.ts @@ -186,7 +186,6 @@ export interface EXPERIMENTAL_RouterOptions_Base extends PathParserOptions { */ export interface EXPERIMENTAL_RouteRecord_Base extends EXPERIMENTAL_ResolverRecord_Base { - // TODO: /** * Where to redirect if the route is directly matched. The redirection happens * before any navigation guard and triggers a new navigation with the new @@ -196,11 +195,9 @@ export interface EXPERIMENTAL_RouteRecord_Base // TODO: /** - * Aliases for the record. Allows defining extra paths that will behave like a - * copy of the record. Allows having paths shorthands like `/users/:id` and - * `/u/:id`. All `alias` and `path` values must share the same params. + * References another record if this record is an alias of it. */ - // alias?: string | string[] + aliasOf?: unknown // TODO: deprecate, expose utils to compare resolved routes, and document // how to create a meta field that does the same @@ -357,7 +354,10 @@ export function normalizeRouteRecord( // must be defined as non enumerable because it contains modules // mods: {}, props: {}, + // TODO :make it optional as it changes nothing parent: null, + // not having the property changes nothing + // aliasOf: null, ...record, // FIXME: to be removed instances: {},