Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,47 @@ test('sends a pageload transaction with a parameterized URL', async ({ page }) =
});
});

test('sends pageload transaction with web vitals measurements', async ({ page }) => {
const transactionPromise = waitForTransaction('tanstack-router', async transactionEvent => {
return !!transactionEvent?.transaction && transactionEvent.contexts?.trace?.op === 'pageload';
});

await page.goto(`/`);

const transaction = await transactionPromise;

expect(transaction).toMatchObject({
contexts: {
trace: {
op: 'pageload',
origin: 'auto.pageload.react.tanstack_router',
},
},
transaction: '/',
transaction_info: {
source: 'route',
},
measurements: expect.objectContaining({
ttfb: expect.objectContaining({
value: expect.any(Number),
unit: 'millisecond',
}),
lcp: expect.objectContaining({
value: expect.any(Number),
unit: 'millisecond',
}),
fp: expect.objectContaining({
value: expect.any(Number),
unit: 'millisecond',
}),
fcp: expect.objectContaining({
value: expect.any(Number),
unit: 'millisecond',
}),
}),
});
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Test doesn't reproduce the regression being fixed (Bugbot Rules)

Per the review rules for fix PRs: the test should fail before the fix and pass after. The PR author explicitly states "I included a test anyways even though it doesn't reproduce it," meaning this test would have passed with the old buggy code. For proper regression testing, a test that specifically validates the condition where fromLocation is undefined during initial pageload and verifies no spurious navigation span is created would more directly test the fix.

Fix in Cursor Fix in Web


test('sends a navigation transaction with a parameterized URL', async ({ page }) => {
const pageloadTxnPromise = waitForTransaction('tanstack-router', async transactionEvent => {
return !!transactionEvent?.transaction && transactionEvent.contexts?.trace?.op === 'pageload';
Expand Down
9 changes: 7 additions & 2 deletions packages/react/src/tanstackrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ export function tanstackRouterBrowserTracingIntegration(
if (instrumentNavigation) {
// 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
castRouterInstance.subscribe('onBeforeNavigate', onBeforeNavigateArgs => {
// onBeforeNavigate is called during pageloads. We can avoid creating navigation spans by comparing the states of the to and from arguments.
if (onBeforeNavigateArgs.toLocation.state === onBeforeNavigateArgs.fromLocation?.state) {
// onBeforeNavigate is called during pageloads. We can avoid creating navigation spans by:
// 1. Checking if there's no fromLocation (initial pageload)
// 2. Comparing the states of the to and from arguments
if (
!onBeforeNavigateArgs.fromLocation ||
onBeforeNavigateArgs.toLocation.state === onBeforeNavigateArgs.fromLocation.state
) {
return;
}

Expand Down