Skip to content

Commit 5921aa2

Browse files
authored
feat(experimental): warn on relative object locations before the initial navigation (#2895)
* feat(experimental): warn on relative object locations before the initial navigation * test: assert only the diagnostic code * refactor(experimental): display location on its own line in VUE_ROUTER_R0130
1 parent dacd36d commit 5921aa2

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎packages/router/src/diagnostics.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ export const diagnostics = /*#__PURE__*/ defineDiagnostics({
6565
`Route "${p.name}" uses beforeEnter, which is deprecated in the experimental router.`,
6666
fix: 'Move the condition to the route meta field and check to.meta in router.beforeEach().',
6767
},
68+
VUE_ROUTER_R0130: {
69+
why: (p: { location: Parameters<typeof stringifyRoute>[0] }) =>
70+
`Cannot resolve a relative location before the initial navigation: there is no current route yet. This crashes in production.\nLocation: ${stringifyRoute(p.location)}`,
71+
fix: 'Use a location with a "name" or an absolute "path", await router.isReady() before mounting the app, or render the <RouterLink> inside a page component so it only renders after the initial navigation.',
72+
},
6873
// --- experimental/route-resolver/resolver-fixed.ts ---
6974
VUE_ROUTER_D0002: {
7075
why: (p: { key: string; value: string }) =>

‎packages/router/src/experimental/router.spec.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,12 +989,23 @@ describe('Experimental Router', () => {
989989
resolver: createFixedResolver(experimentalRoutes),
990990
})
991991
expect(router.resolve({ query: { page: ['2'] } }).matched).toEqual([])
992+
expect('VUE_ROUTER_R0130').toHaveBeenWarnedTimes(1)
992993
expect('VUE_ROUTER_R0129').toHaveBeenWarnedTimes(1)
993994
expect('Cannot resolve a relative location').toHaveBeenWarned()
994995
expect('\nLocation: {\n "query"').toHaveBeenWarned()
995996
expect('No match found').toHaveBeenWarned()
996997
})
997998

999+
it('does not warn before the initial navigation for absolute locations', () => {
1000+
const router = experimental_createRouter({
1001+
history: createMemoryHistory(),
1002+
resolver: createFixedResolver(experimentalRoutes),
1003+
})
1004+
router.resolve({ path: '/search' })
1005+
router.resolve({ name: 'search' })
1006+
expect('VUE_ROUTER_R0130').not.toHaveBeenWarned()
1007+
})
1008+
9981009
it('warns when the current route does not match', async () => {
9991010
const { router } = await newRouter({
10001011
resolver: createFixedResolver(
@@ -1005,6 +1016,7 @@ describe('Experimental Router', () => {
10051016
expect(router.currentRoute.value.matched).toEqual([])
10061017
expect(router.resolve({ query: { page: ['2'] } }).matched).toEqual([])
10071018
expect('VUE_ROUTER_R0129').toHaveBeenWarnedTimes(1)
1019+
expect('VUE_ROUTER_R0130').not.toHaveBeenWarned()
10081020
expect('No match found').toHaveBeenWarned()
10091021
})
10101022
})

‎packages/router/src/experimental/router.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,17 @@ export function experimental_createRouter(
731731
function resolve(
732732
...[to, currentLocation]: _resolveArgs
733733
): RouteLocationResolved {
734+
if (
735+
__DEV__ &&
736+
!currentLocation &&
737+
typeof to === 'object' &&
738+
to.name == null &&
739+
to.path == null &&
740+
currentRoute.value === START_LOCATION_NORMALIZED
741+
) {
742+
diagnostics.VUE_ROUTER_R0130({ location: to })
743+
}
744+
734745
const matchedRoute = resolver.resolve(
735746
// @ts-expect-error FIXME: incompatible types
736747
to,

0 commit comments

Comments
 (0)