Skip to content

Commit c346948

Browse files
soch4nbillyvg
authored andcommitted
fix(vue): Handle span name assignment for nested routes in VueRouter (#12398)
Fix an issue where the correct transactionName could not be obtained when using nested routes in VueRouter.
1 parent cc3df2c commit c346948

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

dev-packages/e2e-tests/test-applications/vue-3/src/router/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ const router = createRouter({
2121
path: '/users-error/:id',
2222
component: () => import('../views/UserIdErrorView.vue'),
2323
},
24+
{
25+
path: '/categories',
26+
children: [
27+
{
28+
path: ':id',
29+
component: () => import('../views/CategoryIdView.vue'),
30+
},
31+
],
32+
},
2433
],
2534
});
2635

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<h1>Category ID: {{ $route.params.id }}</h1>
3+
</template>

dev-packages/e2e-tests/test-applications/vue-3/tests/performance.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,35 @@ test('sends a navigation transaction with a parameterized URL', async ({ page })
6666
});
6767
});
6868

69+
test('sends a pageload transaction with a nested route URL', async ({ page }) => {
70+
const transactionPromise = waitForTransaction('vue-3', async transactionEvent => {
71+
return !!transactionEvent?.transaction && transactionEvent.contexts?.trace?.op === 'pageload';
72+
});
73+
74+
await page.goto(`/categories/123`);
75+
76+
const rootSpan = await transactionPromise;
77+
78+
expect(rootSpan).toMatchObject({
79+
contexts: {
80+
trace: {
81+
data: {
82+
'sentry.source': 'route',
83+
'sentry.origin': 'auto.pageload.vue',
84+
'sentry.op': 'pageload',
85+
'params.id': '123',
86+
},
87+
op: 'pageload',
88+
origin: 'auto.pageload.vue',
89+
},
90+
},
91+
transaction: '/categories/:id',
92+
transaction_info: {
93+
source: 'route',
94+
},
95+
});
96+
});
97+
6998
test('sends a pageload transaction with a route name as transaction name if available', async ({ page }) => {
7099
const transactionPromise = waitForTransaction('vue-3', async transactionEvent => {
71100
return !!transactionEvent?.transaction && transactionEvent.contexts?.trace?.op === 'pageload';

packages/vue/src/router.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ export function instrumentVueRouter(
8383
if (to.name && options.routeLabel !== 'path') {
8484
spanName = to.name.toString();
8585
transactionSource = 'custom';
86-
} else if (to.matched[0] && to.matched[0].path) {
87-
spanName = to.matched[0].path;
86+
} else if (to.matched.length > 0) {
87+
const lastIndex = to.matched.length - 1;
88+
spanName = to.matched[lastIndex].path;
8889
transactionSource = 'route';
8990
}
9091

packages/vue/test/router.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ const testRoutes: Record<string, Route> = {
4343
path: '/accounts/4',
4444
query: {},
4545
},
46+
nestedRoute: {
47+
matched: [{ path: '/' }, { path: '/categories' }, { path: '/categories/:categoryId' }],
48+
params: {
49+
categoryId: '1',
50+
},
51+
path: '/categories/1',
52+
query: {},
53+
},
4654
namedRoute: {
4755
matched: [{ path: '/login' }],
4856
name: 'login-screen',
@@ -85,6 +93,7 @@ describe('instrumentVueRouter()', () => {
8593

8694
it.each([
8795
['normalRoute1', 'normalRoute2', '/accounts/:accountId', 'route'],
96+
['normalRoute1', 'nestedRoute', '/categories/:categoryId', 'route'],
8897
['normalRoute2', 'namedRoute', 'login-screen', 'custom'],
8998
['normalRoute2', 'unmatchedRoute', '/e8733846-20ac-488c-9871-a5cbcb647294', 'url'],
9099
])(
@@ -122,6 +131,7 @@ describe('instrumentVueRouter()', () => {
122131

123132
it.each([
124133
['initialPageloadRoute', 'normalRoute1', '/books/:bookId/chapter/:chapterId', 'route'],
134+
['initialPageloadRoute', 'nestedRoute', '/categories/:categoryId', 'route'],
125135
['initialPageloadRoute', 'namedRoute', 'login-screen', 'custom'],
126136
['initialPageloadRoute', 'unmatchedRoute', '/e8733846-20ac-488c-9871-a5cbcb647294', 'url'],
127137
])(

0 commit comments

Comments
 (0)