Skip to content

Commit fbaded7

Browse files
lforstonurtemizkan
authored andcommitted
feat(nextjs): Add release injection in Turbopack (#15958)
1 parent 2477239 commit fbaded7

File tree

6 files changed

+31
-5
lines changed

6 files changed

+31
-5
lines changed

dev-packages/e2e-tests/test-applications/nextjs-turbo/next.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ const nextConfig = {
99

1010
module.exports = withSentryConfig(nextConfig, {
1111
silent: true,
12+
release: {
13+
name: 'foobar123',
14+
},
1215
});

dev-packages/e2e-tests/test-applications/nextjs-turbo/tests/pages-router/client-trace-propagation.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,14 @@ test('Should propagate traces from server to client in pages router', async ({ p
2828

2929
expect(serverTransaction.contexts?.trace?.trace_id).toBeDefined();
3030
expect(pageloadTransaction.contexts?.trace?.trace_id).toBe(serverTransaction.contexts?.trace?.trace_id);
31+
32+
await test.step('release was successfully injected on the serverside', () => {
33+
// Release as defined in next.config.js
34+
expect(serverTransaction.release).toBe('foobar123');
35+
});
36+
37+
await test.step('release was successfully injected on the clientside', () => {
38+
// Release as defined in next.config.js
39+
expect(pageloadTransaction.release).toBe('foobar123');
40+
});
3141
});

packages/nextjs/src/client/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
2020
_sentryRewriteFramesAssetPrefixPath: string;
2121
_sentryAssetPrefix?: string;
2222
_sentryBasePath?: string;
23+
_sentryRelease?: string;
2324
_experimentalThirdPartyOriginStackFrames?: string;
2425
};
2526

@@ -31,6 +32,7 @@ export function init(options: BrowserOptions): Client | undefined {
3132
const opts = {
3233
environment: getVercelEnv(true) || process.env.NODE_ENV,
3334
defaultIntegrations: getDefaultIntegrations(options),
35+
release: process.env._sentryRelease || globalWithInjectedValues._sentryRelease,
3436
...options,
3537
} satisfies BrowserOptions;
3638

packages/nextjs/src/config/withSentryConfig.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ function getFinalConfigObject(
5050
incomingUserNextConfigObject: NextConfigObject,
5151
userSentryOptions: SentryBuildOptions,
5252
): NextConfigObject {
53+
const releaseName = userSentryOptions.release?.name ?? getSentryRelease() ?? getGitRevision();
54+
5355
if (userSentryOptions?.tunnelRoute) {
5456
if (incomingUserNextConfigObject.output === 'export') {
5557
if (!showedExportModeTunnelWarning) {
@@ -64,7 +66,7 @@ function getFinalConfigObject(
6466
}
6567
}
6668

67-
setUpBuildTimeVariables(incomingUserNextConfigObject, userSentryOptions);
69+
setUpBuildTimeVariables(incomingUserNextConfigObject, userSentryOptions, releaseName);
6870

6971
const nextJsVersion = getNextjsVersion();
7072

@@ -207,8 +209,6 @@ function getFinalConfigObject(
207209
);
208210
}
209211

210-
const releaseName = userSentryOptions.release?.name ?? getSentryRelease() ?? getGitRevision();
211-
212212
return {
213213
...incomingUserNextConfigObject,
214214
webpack: constructWebpackConfigFunction(incomingUserNextConfigObject, userSentryOptions, releaseName),
@@ -291,8 +291,11 @@ function setUpTunnelRewriteRules(userNextConfig: NextConfigObject, tunnelPath: s
291291
};
292292
}
293293

294-
// TODO: For Turbopack we need to pass the release name here and pick it up in the SDK
295-
function setUpBuildTimeVariables(userNextConfig: NextConfigObject, userSentryOptions: SentryBuildOptions): void {
294+
function setUpBuildTimeVariables(
295+
userNextConfig: NextConfigObject,
296+
userSentryOptions: SentryBuildOptions,
297+
releaseName: string | undefined,
298+
): void {
296299
const assetPrefix = userNextConfig.assetPrefix || userNextConfig.basePath || '';
297300
const basePath = userNextConfig.basePath ?? '';
298301
const rewritesTunnelPath =
@@ -335,6 +338,10 @@ function setUpBuildTimeVariables(userNextConfig: NextConfigObject, userSentryOpt
335338
buildTimeVariables._experimentalThirdPartyOriginStackFrames = 'true';
336339
}
337340

341+
if (releaseName) {
342+
buildTimeVariables._sentryRelease = releaseName;
343+
}
344+
338345
if (typeof userNextConfig.env === 'object') {
339346
userNextConfig.env = { ...buildTimeVariables, ...userNextConfig.env };
340347
} else if (userNextConfig.env === undefined) {

packages/nextjs/src/edge/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type EdgeOptions = VercelEdgeOptions;
2626

2727
const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
2828
_sentryRewriteFramesDistDir?: string;
29+
_sentryRelease?: string;
2930
};
3031

3132
/** Inits the Sentry NextJS SDK on the Edge Runtime. */
@@ -48,6 +49,7 @@ export function init(options: VercelEdgeOptions = {}): void {
4849

4950
const opts = {
5051
defaultIntegrations: customDefaultIntegrations,
52+
release: process.env._sentryRelease || globalWithInjectedValues._sentryRelease,
5153
...options,
5254
};
5355

packages/nextjs/src/server/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export { captureUnderscoreErrorException } from '../common/pages-router-instrume
4343
const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
4444
_sentryRewriteFramesDistDir?: string;
4545
_sentryRewritesTunnelPath?: string;
46+
_sentryRelease?: string;
4647
};
4748

4849
/**
@@ -115,6 +116,7 @@ export function init(options: NodeOptions): NodeClient | undefined {
115116

116117
const opts: NodeOptions = {
117118
environment: process.env.SENTRY_ENVIRONMENT || getVercelEnv(false) || process.env.NODE_ENV,
119+
release: process.env._sentryRelease || globalWithInjectedValues._sentryRelease,
118120
defaultIntegrations: customDefaultIntegrations,
119121
...options,
120122
};

0 commit comments

Comments
 (0)