Repository navigation
fix(nextjs): Deliver the release at runtime with release.inject: false
#26509
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
isaacs
merged 2 commits into
fix/react-router-release-runtime
from
fix/nextjs-release-runtime
Nov 18, 2026
+367
−7
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
dev-packages/e2e-tests/test-applications/nextjs-15/tests/release-injection.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { getSpanOp, waitForError, waitForStreamedSpan } from '@sentry-internal/test-utils'; | ||
| import { HTTP_SERVER, PAGELOAD } from '@sentry/conventions/op'; | ||
|
|
||
| test('sends App Router server and pageload spans with the release', async ({ page }) => { | ||
| const serverSpanPromise = waitForStreamedSpan('nextjs-15', span => { | ||
| return span.name === 'GET /pageload-tracing' && getSpanOp(span) === HTTP_SERVER && span.is_segment; | ||
| }); | ||
| const pageloadSpanPromise = waitForStreamedSpan('nextjs-15', span => { | ||
| return span.name === '/pageload-tracing' && getSpanOp(span) === PAGELOAD && span.is_segment; | ||
| }); | ||
|
|
||
| await page.goto('/pageload-tracing'); | ||
|
|
||
| const [serverSpan, pageloadSpan] = await Promise.all([serverSpanPromise, pageloadSpanPromise]); | ||
| expect(serverSpan.attributes['sentry.release']?.value).toBe('foobar123'); | ||
| expect(pageloadSpan.attributes['sentry.release']?.value).toBe('foobar123'); | ||
| }); | ||
|
|
||
| test('sends Pages Router client errors with the release', async ({ page }) => { | ||
| const errorPromise = waitForError('nextjs-15', errorEvent => { | ||
| return errorEvent.exception?.values?.[0]?.value === 'click error'; | ||
| }); | ||
|
|
||
| await page.goto('/42/click-error'); | ||
| await page.click('#error-button'); | ||
|
|
||
| expect((await errorPromise).release).toBe('foobar123'); | ||
| }); | ||
|
|
||
| test('keeps the release out of the client bundles with `release.inject: false`', async ({ page }) => { | ||
| test.skip(!process.env.E2E_RELEASE_NOT_INJECTED, 'Only for builds with `release.inject: false`'); | ||
|
|
||
| const scriptBodies: Promise<string>[] = []; | ||
| page.on('response', response => { | ||
| if (response.url().includes('/_next/static/') && response.url().endsWith('.js')) { | ||
| scriptBodies.push(response.text()); | ||
| } | ||
| }); | ||
|
|
||
| const documentResponse = await page.goto('/42/click-error'); | ||
| await page.waitForLoadState('networkidle'); | ||
|
|
||
| expect(await documentResponse?.headerValue('server-timing')).toContain('sentry-release;desc="foobar123"'); | ||
| expect(scriptBodies.length).toBeGreaterThan(0); | ||
| expect((await Promise.all(scriptBodies)).filter(body => body.includes('foobar123'))).toEqual([]); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
dev-packages/e2e-tests/test-applications/nextjs-16/tests/release-injection.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { getSpanOp, waitForStreamedSpan } from '@sentry-internal/test-utils'; | ||
| import { HTTP_SERVER, PAGELOAD } from '@sentry/conventions/op'; | ||
|
|
||
| test.skip(!process.env.E2E_RELEASE_NOT_INJECTED, 'Only for builds with `release.inject: false`'); | ||
|
|
||
| test('sends server and pageload spans with the release', async ({ page }) => { | ||
| const serverSpanPromise = waitForStreamedSpan('nextjs-16', span => { | ||
| return span.name === 'GET /pageload-tracing' && getSpanOp(span) === HTTP_SERVER && span.is_segment; | ||
| }); | ||
| const pageloadSpanPromise = waitForStreamedSpan('nextjs-16', span => { | ||
| return span.name === '/pageload-tracing' && getSpanOp(span) === PAGELOAD && span.is_segment; | ||
| }); | ||
|
|
||
| await page.goto('/pageload-tracing'); | ||
|
|
||
| const [serverSpan, pageloadSpan] = await Promise.all([serverSpanPromise, pageloadSpanPromise]); | ||
| expect(serverSpan.attributes['sentry.release']?.value).toBe('foobar123'); | ||
| expect(pageloadSpan.attributes['sentry.release']?.value).toBe('foobar123'); | ||
| }); | ||
|
|
||
| test('keeps the release out of the client bundles', async ({ page }) => { | ||
| const scriptBodies: Promise<string>[] = []; | ||
| page.on('response', response => { | ||
| if (response.url().includes('/_next/static/') && response.url().endsWith('.js')) { | ||
| scriptBodies.push(response.text()); | ||
| } | ||
| }); | ||
|
|
||
| const documentResponse = await page.goto('/pageload-tracing'); | ||
| await page.waitForLoadState('networkidle'); | ||
|
|
||
| expect(await documentResponse?.headerValue('server-timing')).toContain('sentry-release;desc="foobar123"'); | ||
| expect(scriptBodies.length).toBeGreaterThan(0); | ||
| expect((await Promise.all(scriptBodies)).filter(body => body.includes('foobar123'))).toEqual([]); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { WINDOW } from '@sentry/react'; | ||
| import { RELEASE_SERVER_TIMING_NAME } from '../common/utils/releaseServerTiming'; | ||
|
|
||
| /** | ||
| * Reads the release from the `Server-Timing` header of the document response, which `withSentryConfig` adds with | ||
| * `release.inject: false`. | ||
| */ | ||
| export function getServerTimingRelease(): string | undefined { | ||
| // The cast is required for the declaration build, which resolves `getEntriesByType('navigation')` | ||
| // to `PerformanceEntry[]` without `serverTiming`. | ||
| const navigation = WINDOW.performance?.getEntriesByType?.('navigation')[0] as PerformanceNavigationTiming | undefined; | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| const description = navigation?.serverTiming?.find(entry => entry.name === RELEASE_SERVER_TIMING_NAME)?.description; | ||
| if (!description) { | ||
| return undefined; | ||
| } | ||
| try { | ||
| return decodeURIComponent(description); | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /** | ||
| * The name of the `Server-Timing` entry that carries the release to the browser SDK when the build keeps it out of | ||
| * the client bundles (`release.inject: false`). | ||
| */ | ||
| export const RELEASE_SERVER_TIMING_NAME = 'sentry-release'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { RELEASE_SERVER_TIMING_NAME } from '../../common/utils/releaseServerTiming'; | ||
| import type { NextConfigObject, SentryBuildOptions } from '../types'; | ||
|
|
||
| /** | ||
| * Whether the release goes into the client bundles. A static export has no server to send it in a header, so it | ||
| * always does. | ||
| */ | ||
| export function shouldInjectReleaseIntoClient( | ||
| userNextConfig: NextConfigObject, | ||
| userSentryOptions: SentryBuildOptions, | ||
| ): boolean { | ||
| return userSentryOptions.release?.inject !== false || userNextConfig.output === 'export'; | ||
| } | ||
|
|
||
| /** | ||
| * Adds the release in a `Server-Timing` header to every response outside of `/_next/`, for the browser SDK to read | ||
| * from the document response when the release is not in the client bundles. | ||
| * | ||
| * Note: this mutates `userNextConfig`. | ||
| */ | ||
| export function maybeAddReleaseServerTimingHeader( | ||
| userNextConfig: NextConfigObject, | ||
| userSentryOptions: SentryBuildOptions, | ||
| releaseName: string | undefined, | ||
| ): void { | ||
| if (!releaseName || shouldInjectReleaseIntoClient(userNextConfig, userSentryOptions)) { | ||
| return; | ||
| } | ||
|
|
||
| // Encoded, so the value is valid in a header and has no `:` that Next.js would read as a path parameter. | ||
| const value = `${RELEASE_SERVER_TIMING_NAME};desc="${encodeURIComponent(releaseName)}"`; | ||
| const originalHeaders = userNextConfig.headers; | ||
| userNextConfig.headers = async () => [ | ||
| // First, because Next.js lets the last matching header with the same key win, and the app's own | ||
| // `Server-Timing` header should not be replaced. | ||
| { source: '/:path((?!_next/).*)', locale: false, headers: [{ key: 'Server-Timing', value }] }, | ||
| ...((await originalHeaders?.()) ?? []), | ||
| ]; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.