Skip to content

Commit e68865a

Browse files
author
Luca Forstner
authored
fix(nextjs): Respect directives in value injection loader (#14083)
This PR is in preparation for turbopack (#8105). In the future, `sentry.client.config.ts` will likely need to be configured with a `"use client"` directive so that turbopack knows it needs to be treated as a file on the client. Our value injection loader currently always prepends the `sentry.client.config.ts` file with statements, rendering any directives in the file useless and crashing turbopack when the file is attempted to be imported somewhere. This PR detects any comments and directives on top of a file to only inject values after.
1 parent ff8e780 commit e68865a

File tree

13 files changed

+297
-27
lines changed

13 files changed

+297
-27
lines changed

dev-packages/e2e-tests/test-applications/nextjs-13/sentry.client.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use client';
2+
13
import * as Sentry from '@sentry/nextjs';
24

35
Sentry.init({

dev-packages/e2e-tests/test-applications/nextjs-14/sentry.client.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use client';
2+
13
import * as Sentry from '@sentry/nextjs';
24

35
Sentry.init({

dev-packages/e2e-tests/test-applications/nextjs-15/sentry.client.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use client';
2+
13
import * as Sentry from '@sentry/nextjs';
24

35
Sentry.init({

dev-packages/e2e-tests/test-applications/nextjs-app-dir/sentry.client.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use client';
2+
13
import * as Sentry from '@sentry/nextjs';
24

35
Sentry.init({

dev-packages/e2e-tests/test-applications/nextjs-t3/sentry.client.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use client';
2+
13
import * as Sentry from '@sentry/nextjs';
24

35
Sentry.init({
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import { HackComponentToRunSideEffectsInSentryClientConfig } from '../sentry.client.config';
2+
13
export default function Layout({ children }: { children: React.ReactNode }) {
24
return (
35
<html lang="en">
4-
<body>{children}</body>
6+
<body>
7+
<HackComponentToRunSideEffectsInSentryClientConfig />
8+
{children}
9+
</body>
510
</html>
611
);
712
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { AppProps } from 'next/app';
2+
import '../sentry.client.config';
3+
4+
export default function CustomApp({ Component, pageProps }: AppProps) {
5+
return <Component {...pageProps} />;
6+
}
Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
'use client';
2+
13
import * as Sentry from '@sentry/nextjs';
24

3-
Sentry.init({
4-
environment: 'qa', // dynamic sampling bias to keep transactions
5-
dsn: process.env.NEXT_PUBLIC_E2E_TEST_DSN,
6-
tunnel: `http://localhost:3031/`, // proxy server
7-
tracesSampleRate: 1.0,
8-
sendDefaultPii: true,
9-
});
5+
if (typeof window !== 'undefined') {
6+
Sentry.init({
7+
environment: 'qa', // dynamic sampling bias to keep transactions
8+
dsn: process.env.NEXT_PUBLIC_E2E_TEST_DSN,
9+
tunnel: `http://localhost:3031/`, // proxy server
10+
tracesSampleRate: 1.0,
11+
sendDefaultPii: true,
12+
});
13+
}
14+
15+
export function HackComponentToRunSideEffectsInSentryClientConfig() {
16+
return null;
17+
}
Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
import { expect, test } from '@playwright/test';
22
import { waitForTransaction } from '@sentry-internal/test-utils';
3-
import { extractTraceparentData } from '@sentry/utils';
43

54
test('Should propagate traces from server to client in pages router', async ({ page }) => {
65
const serverTransactionPromise = waitForTransaction('nextjs-turbo', async transactionEvent => {
7-
return transactionEvent?.transaction === 'GET /[param]/client-trace-propagation';
6+
return transactionEvent?.transaction === 'GET /[param]/pages-router-client-trace-propagation';
87
});
98

10-
await page.goto(`/123/client-trace-propagation`);
11-
12-
const sentryTraceLocator = await page.locator('meta[name="sentry-trace"]');
13-
const sentryTraceValue = await sentryTraceLocator.getAttribute('content');
14-
expect(sentryTraceValue).toMatch(/^[a-f0-9]{32}-[a-f0-9]{16}-[0-1]$/);
15-
16-
const baggageLocator = await page.locator('meta[name="baggage"]');
17-
const baggageValue = await baggageLocator.getAttribute('content');
18-
expect(baggageValue).toMatch(/sentry-public_key=/);
9+
const pageloadTransactionPromise = waitForTransaction('nextjs-turbo', async transactionEvent => {
10+
return transactionEvent?.transaction === '/[param]/pages-router-client-trace-propagation';
11+
});
1912

20-
const traceparentData = extractTraceparentData(sentryTraceValue!);
13+
await page.goto(`/123/pages-router-client-trace-propagation`);
2114

2215
const serverTransaction = await serverTransactionPromise;
16+
const pageloadTransaction = await pageloadTransactionPromise;
2317

24-
expect(serverTransaction.contexts?.trace?.trace_id).toBe(traceparentData?.traceId);
18+
expect(serverTransaction.contexts?.trace?.trace_id).toBeDefined();
19+
expect(pageloadTransaction.contexts?.trace?.trace_id).toBe(serverTransaction.contexts?.trace?.trace_id);
2520
});

0 commit comments

Comments
 (0)