From: Eduardo San Martin Morote Date: Sun, 24 May 2020 13:59:54 +0000 (+0200) Subject: fix(matcher): override records by name when adding X-Git-Tag: v4.0.0-alpha.13~45 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=07100fc1386fb636da3eb1c8196a36f6538eb91f;p=thirdparty%2Fvuejs%2Frouter.git fix(matcher): override records by name when adding --- diff --git a/__tests__/matcher/addingRemoving.spec.ts b/__tests__/matcher/addingRemoving.spec.ts index 5fe3d841..a3ebabb3 100644 --- a/__tests__/matcher/addingRemoving.spec.ts +++ b/__tests__/matcher/addingRemoving.spec.ts @@ -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() diff --git a/src/matcher/index.ts b/src/matcher/index.ts index d4eeb1eb..ab02038d 100644 --- a/src/matcher/index.ts +++ b/src/matcher/index.ts @@ -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) } diff --git a/src/matcher/pathTokenizer.ts b/src/matcher/pathTokenizer.ts index 2e9c43d6..c1d5d5af 100644 --- a/src/matcher/pathTokenizer.ts +++ b/src/matcher/pathTokenizer.ts @@ -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() export function tokenizePath(path: string): Array { if (!path) return [[]] @@ -45,6 +49,8 @@ export function tokenizePath(path: string): Array { // 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 { consumeBuffer() finalizeSegment() + // tokenCache.set(path, tokens) + return tokens }