]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(matcher): override records by name when adding
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 24 May 2020 13:59:54 +0000 (15:59 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sun, 24 May 2020 13:59:54 +0000 (15:59 +0200)
__tests__/matcher/addingRemoving.spec.ts
src/matcher/index.ts
src/matcher/pathTokenizer.ts

index 5fe3d8414c9fa29314964d2ed17f9f008959242b..a3ebabb3160b566320df16dd90e1631b429960ee 100644 (file)
@@ -360,6 +360,16 @@ describe('Matcher: adding and removing records', () => {
     expect(matcher.getRecordMatcher('child')).toBe(undefined)
   })
 
+  it('removes existing record when adding with the same name', () => {
+    const matcher = createRouterMatcher([], {})
+    matcher.addRoute({ path: '/', component, name: 'home' })
+    matcher.addRoute({ path: '/home', component, name: 'home' })
+    expect(matcher.getRoutes()).toHaveLength(1)
+    expect(matcher.resolve({ path: '/home' }, currentLocation)).toMatchObject({
+      name: 'home',
+    })
+  })
+
   describe('warnings', () => {
     mockWarn()
 
index d4eeb1eb88e823cba666c535226a8f8acbedf45e..ab02038d7856754d596f12a66c7b6e730ebdccc3 100644 (file)
@@ -53,6 +53,8 @@ export function createRouterMatcher(
     parent?: RouteRecordMatcher,
     originalRecord?: RouteRecordMatcher
   ) {
+    // used later on to remove by name
+    let isRootAdd = !originalRecord
     let mainNormalizedRecord = normalizeRouteRecord(record)
     // we might be the child of an alias
     mainNormalizedRecord.aliasOf = originalRecord && originalRecord.record
@@ -134,6 +136,9 @@ export function createRouterMatcher(
       // other alias (if any) need to reference this record when adding children
       originalRecord = originalRecord || matcher
 
+      // remove the route if named and only for the top record (avoid in nested calls)
+      if (isRootAdd && record.name) removeRoute(record.name)
+
       insertMatcher(matcher)
     }
 
index 2e9c43d684f2f6cf9d925b9a13eb733ca198ac0e..c1d5d5af12b79291e3ee97aea984ad6fdba4cd2b 100644 (file)
@@ -38,6 +38,10 @@ const ROOT_TOKEN: Token = {
 }
 
 const VALID_PARAM_RE = /[a-zA-Z0-9_]/
+// After some profiling, the cache seems to be unnecessary because tokenizePath
+// (the slowest part of adding a route) is very fast
+
+// const tokenCache = new Map<string, Token[][]>()
 
 export function tokenizePath(path: string): Array<Token[]> {
   if (!path) return [[]]
@@ -45,6 +49,8 @@ export function tokenizePath(path: string): Array<Token[]> {
   // remove the leading slash
   if (path[0] !== '/') throw new Error('A non-empty path must start with "/"')
 
+  // if (tokenCache.has(path)) return tokenCache.get(path)!
+
   function crash(message: string) {
     throw new Error(`ERR (${state})/"${buffer}": ${message}`)
   }
@@ -178,5 +184,7 @@ export function tokenizePath(path: string): Array<Token[]> {
   consumeBuffer()
   finalizeSegment()
 
+  // tokenCache.set(path, tokens)
+
   return tokens
 }