Skip to content

Commit 9289200

Browse files
authored
fix(nuxt): Detect pageload by adding flag in Vue router (#13171)
Nuxt is using the Vue router under the hood, but the previous condition to detect a page load (`from.name == null && from.matched.length === 0`) does not work with Nuxt, as `from.matched` is never empty.
1 parent 5f4a71c commit 9289200

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

dev-packages/e2e-tests/test-applications/nuxt-3/playwright.config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const nuxtConfigOptions: ConfigOptions = {
88
},
99
};
1010

11+
/* Make sure to import from '@nuxt/test-utils/playwright' in the tests
12+
* Like this: import { expect, test } from '@nuxt/test-utils/playwright' */
13+
1114
const config = getPlaywrightConfig({
1215
startCommand: `pnpm preview`,
1316
use: { ...nuxtConfigOptions },
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { expect, test } from '@nuxt/test-utils/playwright';
2+
import { waitForTransaction } from '@sentry-internal/test-utils';
3+
4+
test('sends a pageload root span with a parameterized URL', async ({ page }) => {
5+
const transactionPromise = waitForTransaction('nuxt-3', async transactionEvent => {
6+
return transactionEvent.transaction === '/test-param/:param()';
7+
});
8+
9+
await page.goto(`/test-param/1234`);
10+
11+
const rootSpan = await transactionPromise;
12+
13+
expect(rootSpan).toMatchObject({
14+
contexts: {
15+
trace: {
16+
data: {
17+
'sentry.source': 'route',
18+
'sentry.origin': 'auto.pageload.vue',
19+
'sentry.op': 'pageload',
20+
'params.param': '1234',
21+
},
22+
op: 'pageload',
23+
origin: 'auto.pageload.vue',
24+
},
25+
},
26+
transaction: '/test-param/:param()',
27+
transaction_info: {
28+
source: 'route',
29+
},
30+
});
31+
});

packages/nuxt/src/server/sdk.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { applySdkMetadata, getGlobalScope } from '@sentry/core';
22
import { init as initNode } from '@sentry/node';
33
import type { Client, EventProcessor } from '@sentry/types';
4+
import { logger } from '@sentry/utils';
5+
import { DEBUG_BUILD } from '../common/debug-build';
46
import type { SentryNuxtOptions } from '../common/types';
57

68
/**
@@ -26,8 +28,8 @@ export function init(options: SentryNuxtOptions): Client | undefined {
2628
// todo: the buildAssetDir could be changed in the nuxt config - change this to a more generic solution
2729
if (event.transaction?.match(/^GET \/_nuxt\//)) {
2830
options.debug &&
29-
// eslint-disable-next-line no-console
30-
console.log('[Sentry] NuxtLowQualityTransactionsFilter filtered transaction: ', event.transaction);
31+
DEBUG_BUILD &&
32+
logger.log('NuxtLowQualityTransactionsFilter filtered transaction: ', event.transaction);
3133
return null;
3234
}
3335

packages/vue/src/router.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,28 @@ export function instrumentVueRouter(
5050
},
5151
startNavigationSpanFn: (context: StartSpanOptions) => void,
5252
): void {
53+
let isFirstPageLoad = true;
54+
5355
router.onError(error => captureException(error, { mechanism: { handled: false } }));
5456

5557
router.beforeEach((to, from, next) => {
56-
// According to docs we could use `from === VueRouter.START_LOCATION` but I couldnt get it working for Vue 2
58+
// According to docs we could use `from === VueRouter.START_LOCATION` but I couldn't get it working for Vue 2
5759
// https://router.vuejs.org/api/#router-start-location
5860
// https://next.router.vuejs.org/api/#start-location
61+
// Additionally, Nuxt does not provide the possibility to check for `from.matched.length === 0` (this is never 0).
62+
// Therefore, a flag was added to track the page-load: isFirstPageLoad
5963

6064
// from.name:
6165
// - Vue 2: null
6266
// - Vue 3: undefined
67+
// - Nuxt: undefined
6368
// hence only '==' instead of '===', because `undefined == null` evaluates to `true`
64-
const isPageLoadNavigation = from.name == null && from.matched.length === 0;
69+
const isPageLoadNavigation =
70+
(from.name == null && from.matched.length === 0) || (from.name === undefined && isFirstPageLoad);
71+
72+
if (isFirstPageLoad) {
73+
isFirstPageLoad = false;
74+
}
6575

6676
const attributes: SpanAttributes = {
6777
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.vue',

packages/vue/test/router.test.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ describe('instrumentVueRouter()', () => {
114114

115115
const from = testRoutes[fromKey]!;
116116
const to = testRoutes[toKey]!;
117+
beforeEachCallback(to, testRoutes['initialPageloadRoute']!, mockNext); // fake initial pageload
117118
beforeEachCallback(to, from, mockNext);
118119

119120
expect(mockStartSpan).toHaveBeenCalledTimes(1);
@@ -127,7 +128,7 @@ describe('instrumentVueRouter()', () => {
127128
op: 'navigation',
128129
});
129130

130-
expect(mockNext).toHaveBeenCalledTimes(1);
131+
expect(mockNext).toHaveBeenCalledTimes(2);
131132
},
132133
);
133134

@@ -192,6 +193,7 @@ describe('instrumentVueRouter()', () => {
192193

193194
const from = testRoutes.normalRoute1!;
194195
const to = testRoutes.namedRoute!;
196+
beforeEachCallback(to, testRoutes['initialPageloadRoute']!, mockNext); // fake initial pageload
195197
beforeEachCallback(to, from, mockNext);
196198

197199
// first startTx call happens when the instrumentation is initialized (for pageloads)
@@ -219,6 +221,7 @@ describe('instrumentVueRouter()', () => {
219221

220222
const from = testRoutes.normalRoute1!;
221223
const to = testRoutes.namedRoute!;
224+
beforeEachCallback(to, testRoutes['initialPageloadRoute']!, mockNext); // fake initial pageload
222225
beforeEachCallback(to, from, mockNext);
223226

224227
// first startTx call happens when the instrumentation is initialized (for pageloads)
@@ -373,6 +376,7 @@ describe('instrumentVueRouter()', () => {
373376
expect(mockVueRouter.beforeEach).toHaveBeenCalledTimes(1);
374377

375378
const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0]![0]!;
379+
beforeEachCallback(testRoutes['normalRoute1']!, testRoutes['initialPageloadRoute']!, mockNext); // fake initial pageload
376380
beforeEachCallback(testRoutes['normalRoute2']!, testRoutes['normalRoute1']!, mockNext);
377381

378382
expect(mockStartSpan).toHaveBeenCalledTimes(expectedCallsAmount);
@@ -391,6 +395,7 @@ describe('instrumentVueRouter()', () => {
391395

392396
const from = testRoutes.normalRoute1!;
393397
const to = testRoutes.namedRoute!;
398+
beforeEachCallback(to, testRoutes['initialPageloadRoute']!, mockNext); // fake initial pageload
394399
beforeEachCallback(to, from, undefined);
395400

396401
// first startTx call happens when the instrumentation is initialized (for pageloads)

0 commit comments

Comments
 (0)