feat(router): add v2RouteName collision-guard helper

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-16 01:17:05 +02:00
parent d2c91f4e80
commit be245080e1
2 changed files with 43 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
import { describe, expect, it } from 'vitest'
import { v2RouteName } from '@/plugins/1.router/v2RouteName'
describe('v2RouteName', () => {
it('prefixes names for routes under the v2 path', () => {
expect(v2RouteName('dashboard', '/v2/dashboard')).toBe('v2-dashboard')
expect(v2RouteName('events-id', '/v2/events/:id')).toBe('v2-events-id')
})
it('prefixes the bare v2 root', () => {
expect(v2RouteName('v2', '/v2')).toBe('v2-v2')
expect(v2RouteName('index', 'v2/')).toBe('v2-index')
})
it('leaves v1 route names untouched', () => {
expect(v2RouteName('dashboard', '/dashboard')).toBe('dashboard')
expect(v2RouteName('events', '/events')).toBe('events')
})
it('does not match a v1 path that merely starts with the letters v2', () => {
expect(v2RouteName('v2x-thing', '/v2x-thing')).toBe('v2x-thing')
})
})

View File

@@ -0,0 +1,20 @@
/**
* Route-NAME collision guard for the parallel /v2/* tree.
*
* unplugin-vue-router derives the route name from the file path relative
* to its routesFolder, so `src/pages/events/index.vue` and
* `src/pages-v2/events/index.vue` would BOTH yield name `events` — a
* silent runtime collision (router.push({ name: 'events' }) becomes
* ambiguous). The pages-v2 routesFolder carries `path: 'v2/'`, so every
* v2 route's URL path is under `/v2`. Prefix the NAME with `v2-` for
* those, leaving v1 names untouched. Stripped at final cutover.
*
* @param baseName the kebab name already computed by getRouteName
* @param routePath the node's URL path (leading slash optional)
*/
export function v2RouteName(baseName: string, routePath: string): string {
const normalized = routePath.replace(/^\//, '')
const isV2 = normalized === 'v2' || normalized.startsWith('v2/')
return isV2 ? `v2-${baseName}` : baseName
}