]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(experimental): basic alias
authorEduardo San Martin Morote <posva13@gmail.com>
Sat, 18 Oct 2025 10:20:25 +0000 (12:20 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sat, 18 Oct 2025 10:20:25 +0000 (12:20 +0200)
packages/router/src/experimental/route-resolver/resolver-fixed.ts
packages/router/src/experimental/router.spec.ts
packages/router/src/experimental/router.ts

index 8533b25d7d8c3f5ce9beb20095e6786fd457ea30..92b06b88a0c0275abb019b11c06f02db90c60e7f 100644 (file)
@@ -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
 }
 
 /**
index bc14828870bd50726e7f1bea94663a372d3d52b6..de83956f65b3b4937d8ded901d817f4d27255323 100644 (file)
@@ -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 () => {})
 
index dfa617bd5cb284f860f5f98b324057415a8e417a..919835bc71cbf6cf71c3bece33221257a38e76ae 100644 (file)
@@ -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: {},