Skip to content

feat(nextjs): Mark clientside prefetch request spans with http.request.prefetch: true attribute #15980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 7, 2025
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
@@ -0,0 +1,9 @@
import Link from 'next/link';

export default function Page() {
return (
<Link id="prefetch-link" href="/prefetching/to-be-prefetched">
link
</Link>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const dynamic = 'force-dynamic';

export default function Page() {
return <p>Hello</p>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@types/node": "^18.19.1",
"@types/react": "18.0.26",
"@types/react-dom": "18.0.9",
"next": "15.0.0-canary.182",
"next": "15.3.0-canary.33",
"react": "beta",
"react-dom": "beta",
"typescript": "~5.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, test } from '@playwright/test';
import { waitForTransaction } from '@sentry-internal/test-utils';

test('Prefetch client spans should have a http.request.prefetch attribute', async ({ page }) => {
test.skip(process.env.TEST_ENV === 'development', "Prefetch requests don't have the prefetch header in dev mode");

const pageloadTransactionPromise = waitForTransaction('nextjs-15', async transactionEvent => {
return transactionEvent?.transaction === '/prefetching';
});

await page.goto(`/prefetching`);

// Make it more likely that nextjs prefetches
await page.hover('#prefetch-link');

expect((await pageloadTransactionPromise).spans).toContainEqual(
expect.objectContaining({
op: 'http.client',
data: expect.objectContaining({
'http.request.prefetch': true,
}),
}),
);
});
10 changes: 10 additions & 0 deletions packages/nextjs/src/client/browserTracingIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ export function browserTracingIntegration(
...options,
instrumentNavigation: false,
instrumentPageLoad: false,
onRequestSpanStart(...args) {
Copy link
Member

Choose a reason for hiding this comment

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

l: do we care about cases where users register their own browserTracingIntegration()? As in should we merge a possible custom implementation with our logic? Not sure what our general approach to this is so feel free to declare undefined behaviour :D

Copy link
Member Author

Choose a reason for hiding this comment

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

I think user's would be always importing this browserTracingIntegration (the Next.js one) so the behavior should already be merged (see line 23). Unless I am misunderstanding.

const [span, { headers }] = args;

// Next.js prefetch requests have a `next-router-prefetch` header
if (headers?.get('next-router-prefetch')) {
span?.setAttribute('http.request.prefetch', true);
}

return options.onRequestSpanStart?.(...args);
},
});

const { instrumentPageLoad = true, instrumentNavigation = true } = options;
Expand Down
Loading