-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(next): Wrap all Random APIs with a safe runner #18700
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
logaretm
merged 24 commits into
develop
from
awad/js-1250-generatemetadata-breaks-cachecomponents
Jan 9, 2026
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
6e1678a
chore(eslint): create a random id gen rule finder
logaretm 708280b
feat: implement a safe random runner
logaretm de30928
fix: wrap all random APIs with the safe runner
logaretm 9031948
fix: set the escape hatch in next sdk
logaretm fe8f4bc
fix: format
logaretm 4b27d51
fix: also patch otel pkg
logaretm 824d1b0
refactor: better comments and remove duplication
logaretm 9f40228
feat: eslint rule message improvements
logaretm d029ed2
style: enable the rule in all next sdk dep graph
logaretm 61256ef
test: added e2e test
logaretm 4caada8
fix: use safe wrappers and ignore rule when not needed in next sdk
logaretm d8ec196
test: added async metadata test
logaretm e29378c
chore: bump size-limit
logaretm 9bc80c2
fix: check for snapshot existence first
logaretm dec6f70
refactor: revert all core changes
logaretm 6a5d858
feat: update patching API name
logaretm 8a49853
fix: only patch the needed functions
logaretm 7de9d83
fix: bad reverts
logaretm c3c993f
fix: size-limit
logaretm 2240f9d
chore: bring back the eslint rule
logaretm bd3ad82
fix: re-patch all random API calls
logaretm 66bed30
fix: lint rules
logaretm 0c25ceb
feat: optimize lookup speed by doing it once
logaretm 0805c12
fix: lint
logaretm 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
Some comments aren't visible on the classic Files Changed page.
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
37 changes: 37 additions & 0 deletions
37
...ackages/e2e-tests/test-applications/nextjs-16-cacheComponents/app/metadata-async/page.tsx
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,37 @@ | ||
| import * as Sentry from '@sentry/nextjs'; | ||
|
|
||
| function fetchPost() { | ||
| return Promise.resolve({ id: '1', title: 'Post 1' }); | ||
| } | ||
|
|
||
| export async function generateMetadata() { | ||
| const { id } = await fetchPost(); | ||
| const product = `Product: ${id}`; | ||
|
|
||
| return { | ||
| title: product, | ||
| }; | ||
| } | ||
|
|
||
| export default function Page() { | ||
| return ( | ||
| <> | ||
| <h1>This will be pre-rendered</h1> | ||
| <DynamicContent /> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| async function DynamicContent() { | ||
| const getTodos = async () => { | ||
| return Sentry.startSpan({ name: 'getTodos', op: 'get.todos' }, async () => { | ||
| 'use cache'; | ||
| await new Promise(resolve => setTimeout(resolve, 100)); | ||
| return [1, 2, 3, 4, 5]; | ||
| }); | ||
| }; | ||
|
|
||
| const todos = await getTodos(); | ||
|
|
||
| return <div id="todos-fetched">Todos fetched: {todos.length}</div>; | ||
| } |
35 changes: 35 additions & 0 deletions
35
dev-packages/e2e-tests/test-applications/nextjs-16-cacheComponents/app/metadata/page.tsx
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,35 @@ | ||
| import * as Sentry from '@sentry/nextjs'; | ||
|
|
||
| /** | ||
| * Tests generateMetadata function with cache components, this calls the propagation context to be set | ||
| * Which will generate and set a trace id in the propagation context, which should trigger the random API error if unpatched | ||
| * See: https://github.com/getsentry/sentry-javascript/issues/18392 | ||
| */ | ||
| export function generateMetadata() { | ||
| return { | ||
| title: 'Cache Components Metadata Test', | ||
| }; | ||
| } | ||
|
|
||
| export default function Page() { | ||
| return ( | ||
| <> | ||
| <h1>This will be pre-rendered</h1> | ||
| <DynamicContent /> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| async function DynamicContent() { | ||
| const getTodos = async () => { | ||
| return Sentry.startSpan({ name: 'getTodos', op: 'get.todos' }, async () => { | ||
| 'use cache'; | ||
| await new Promise(resolve => setTimeout(resolve, 100)); | ||
| return [1, 2, 3, 4, 5]; | ||
| }); | ||
| }; | ||
|
|
||
| const todos = await getTodos(); | ||
|
|
||
| return <div id="todos-fetched">Todos fetched: {todos.length}</div>; | ||
| } | ||
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 |
|---|---|---|
| @@ -1,4 +1,15 @@ | ||
| module.exports = { | ||
| extends: ['../../.eslintrc.js'], | ||
| ignorePatterns: ['rollup.npm.config.mjs'], | ||
| rules: { | ||
| '@sentry-internal/sdk/no-unsafe-random-apis': 'error', | ||
| }, | ||
| overrides: [ | ||
| { | ||
| files: ['test/**/*.ts', 'test/**/*.tsx'], | ||
| rules: { | ||
| '@sentry-internal/sdk/no-unsafe-random-apis': 'off', | ||
| }, | ||
| }, | ||
| ], | ||
| }; |
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
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,43 @@ | ||
| import { GLOBAL_OBJ } from './worldwide'; | ||
|
|
||
| export type RandomSafeContextRunner = <T>(callback: () => T) => T; | ||
|
|
||
| // undefined = not yet resolved, null = no runner found, function = runner found | ||
| let RESOLVED_RUNNER: RandomSafeContextRunner | null | undefined; | ||
|
|
||
| /** | ||
| * Simple wrapper that allows SDKs to *secretly* set context wrapper to generate safe random IDs in cache components contexts | ||
| */ | ||
| export function withRandomSafeContext<T>(cb: () => T): T { | ||
| // Skips future symbol lookups if we've already resolved (or attempted to resolve) the runner once | ||
| if (RESOLVED_RUNNER !== undefined) { | ||
| return RESOLVED_RUNNER ? RESOLVED_RUNNER(cb) : cb(); | ||
| } | ||
|
|
||
| const sym = Symbol.for('__SENTRY_SAFE_RANDOM_ID_WRAPPER__'); | ||
| const globalWithSymbol: typeof GLOBAL_OBJ & { [sym]?: RandomSafeContextRunner } = GLOBAL_OBJ; | ||
|
|
||
| if (sym in globalWithSymbol && typeof globalWithSymbol[sym] === 'function') { | ||
| RESOLVED_RUNNER = globalWithSymbol[sym]; | ||
| return RESOLVED_RUNNER(cb); | ||
| } | ||
|
|
||
| RESOLVED_RUNNER = null; | ||
| return cb(); | ||
| } | ||
|
|
||
| /** | ||
| * Identical to Math.random() but wrapped in withRandomSafeContext | ||
| * to ensure safe random number generation in certain contexts (e.g., Next.js Cache Components). | ||
| */ | ||
| export function safeMathRandom(): number { | ||
| return withRandomSafeContext(() => Math.random()); | ||
| } | ||
|
|
||
| /** | ||
| * Identical to Date.now() but wrapped in withRandomSafeContext | ||
| * to ensure safe time value generation in certain contexts (e.g., Next.js Cache Components). | ||
| */ | ||
| export function safeDateNow(): number { | ||
| return withRandomSafeContext(() => Date.now()); | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we expand this test case, or better duplicate it and run some async computation within generateMetadata?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing, I will add some logic there