Skip to content

Commit 5affac5

Browse files
authored
fix(tanstack-router): Check for fromLocation existence before reporting pageload (#18463)
Bad condition which short circuits the web vitals from being reported, I included a test anyways even though it doesn't reproduce it. closes #18425 Closes #18464 (added automatically)???
1 parent 0591b1b commit 5affac5

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

dev-packages/e2e-tests/test-applications/tanstack-router/tests/routing-instrumentation.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,47 @@ test('sends a pageload transaction with a parameterized URL', async ({ page }) =
3535
});
3636
});
3737

38+
test('sends pageload transaction with web vitals measurements', async ({ page }) => {
39+
const transactionPromise = waitForTransaction('tanstack-router', async transactionEvent => {
40+
return !!transactionEvent?.transaction && transactionEvent.contexts?.trace?.op === 'pageload';
41+
});
42+
43+
await page.goto(`/`);
44+
45+
const transaction = await transactionPromise;
46+
47+
expect(transaction).toMatchObject({
48+
contexts: {
49+
trace: {
50+
op: 'pageload',
51+
origin: 'auto.pageload.react.tanstack_router',
52+
},
53+
},
54+
transaction: '/',
55+
transaction_info: {
56+
source: 'route',
57+
},
58+
measurements: expect.objectContaining({
59+
ttfb: expect.objectContaining({
60+
value: expect.any(Number),
61+
unit: 'millisecond',
62+
}),
63+
lcp: expect.objectContaining({
64+
value: expect.any(Number),
65+
unit: 'millisecond',
66+
}),
67+
fp: expect.objectContaining({
68+
value: expect.any(Number),
69+
unit: 'millisecond',
70+
}),
71+
fcp: expect.objectContaining({
72+
value: expect.any(Number),
73+
unit: 'millisecond',
74+
}),
75+
}),
76+
});
77+
});
78+
3879
test('sends a navigation transaction with a parameterized URL', async ({ page }) => {
3980
const pageloadTxnPromise = waitForTransaction('tanstack-router', async transactionEvent => {
4081
return !!transactionEvent?.transaction && transactionEvent.contexts?.trace?.op === 'pageload';

packages/react/src/tanstackrouter.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,13 @@ export function tanstackRouterBrowserTracingIntegration(
6464
if (instrumentNavigation) {
6565
// The onBeforeNavigate hook is called at the very beginning of a navigation and is only called once per navigation, even when the user is redirected
6666
castRouterInstance.subscribe('onBeforeNavigate', onBeforeNavigateArgs => {
67-
// onBeforeNavigate is called during pageloads. We can avoid creating navigation spans by comparing the states of the to and from arguments.
68-
if (onBeforeNavigateArgs.toLocation.state === onBeforeNavigateArgs.fromLocation?.state) {
67+
// onBeforeNavigate is called during pageloads. We can avoid creating navigation spans by:
68+
// 1. Checking if there's no fromLocation (initial pageload)
69+
// 2. Comparing the states of the to and from arguments
70+
if (
71+
!onBeforeNavigateArgs.fromLocation ||
72+
onBeforeNavigateArgs.toLocation.state === onBeforeNavigateArgs.fromLocation.state
73+
) {
6974
return;
7075
}
7176

0 commit comments

Comments
 (0)