nextjs - ✅(Solved) Fix `Date` usage tracking is leaking too much outside of Next.js rendering [1 pull requests, 3 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#83795Fetched 2026-04-08 02:21:28
View on GitHub
Comments
3
Participants
3
Timeline
12
Reactions
2
Author
Timeline (top)
commented ×3cross-referenced ×2labeled ×2closed ×1

Error Message

[2025-09-12T13:34:02.181Z] Error: Route "/[slug]/server" used new Date() before accessing either uncached data (e.g. fetch()) or Request data (e.g. cookies(), headers(), connection(), and searchParams). Accessing the current time in a Server Component requires reading one of these data sources first. Alternatively, consider moving this expression into a Client Component or Cache Component. See more info here: https://nextjs.org/docs/messages/next-prerender-current-time at console.<computed> [as error] (patch-console.mjs:7:18) at <unknown> (.next/server/app/[slug]/fallback/page.js:1:946) at a (.next/server/app/[slug]/fallback/page.js:1:2327) [2025-09-12T13:34:02.184Z] To get a more detailed stack trace and pinpoint the issue, try one of the following: - Start the app in development mode by running next dev, then open "/[slug]/server" in your browser to investigate the error. - Rerun the production build with next build --debug-prerender to generate better stack traces. [2025-09-12T13:34:02.185Z] Error: at async o (.next/server/app/[slug]/server/page.js:1:11119) { code: 'NEXT_STATIC_GEN_BAILOUT' }

Root Cause

  • A non-fatal warning is being logged with console.error (this is specific to this concrete reproduction, but any log would result in the same problem):
    [2025-09-12T13:34:01.133Z] A Cache Function (`use cache`) was passed to startActiveSpan which means it will receive a Span argument with a possibly random ID on every invocation leading to cache misses. Provide a wrapping function around the Cache Function that does not forward the Span argument to avoid this issue.
    • Because console patching make use of new Date(), logging above warning results in new Date() bailout and fatal/500 error:
      [2025-09-12T13:34:02.181Z] Error: Route "/[slug]/server" used `new Date()` before accessing either uncached data (e.g. `fetch()`) or Request data (e.g. `cookies()`, `headers()`, `connection()`, and `searchParams`). Accessing the current time in a Server Component requires reading one of these data sources first. Alternatively, consider moving this expression into a Client Component or Cache Component. See more info here: https://nextjs.org/docs/messages/next-prerender-current-time
          at console.<computed> [as error] (patch-console.mjs:7:18)
          at <unknown> (.next/server/app/[slug]/fallback/page.js:1:946)
          at a (.next/server/app/[slug]/fallback/page.js:1:2327)
      [2025-09-12T13:34:02.184Z] To get a more detailed stack trace and pinpoint the issue, try one of the following:
        - Start the app in development mode by running `next dev`, then open "/[slug]/server" in your browser to investigate the error.
        - Rerun the production build with `next build --debug-prerender` to generate better stack traces.
      [2025-09-12T13:34:02.185Z] Error:
          at async o (.next/server/app/[slug]/server/page.js:1:11119) {
        code: 'NEXT_STATIC_GEN_BAILOUT'
      }

Fix Action

Fix / Workaround

The reproduction loosely mimics console.* patching that is done by AWS Lambda Node Runtime where any of methods will get timestamp via new Date().

  1. Install, build and start the app:
    git clone [email protected]:pieh/next-cache-components-timestamped-console-methods.git
    cd next-cache-components-timestamped-console-methods
    npm i
    npm run build
    npm run start
    # start script injects console patching: "NODE_OPTIONS='--import ./patch-console.mjs' next start"
  2. Visit or curl http://localhost:3000/novel/server
  • A non-fatal warning is being logged with console.error (this is specific to this concrete reproduction, but any log would result in the same problem):
    [2025-09-12T13:34:01.133Z] A Cache Function (`use cache`) was passed to startActiveSpan which means it will receive a Span argument with a possibly random ID on every invocation leading to cache misses. Provide a wrapping function around the Cache Function that does not forward the Span argument to avoid this issue.
    • Because console patching make use of new Date(), logging above warning results in new Date() bailout and fatal/500 error:
      [2025-09-12T13:34:02.181Z] Error: Route "/[slug]/server" used `new Date()` before accessing either uncached data (e.g. `fetch()`) or Request data (e.g. `cookies()`, `headers()`, `connection()`, and `searchParams`). Accessing the current time in a Server Component requires reading one of these data sources first. Alternatively, consider moving this expression into a Client Component or Cache Component. See more info here: https://nextjs.org/docs/messages/next-prerender-current-time
          at console.<computed> [as error] (patch-console.mjs:7:18)
          at <unknown> (.next/server/app/[slug]/fallback/page.js:1:946)
          at a (.next/server/app/[slug]/fallback/page.js:1:2327)
      [2025-09-12T13:34:02.184Z] To get a more detailed stack trace and pinpoint the issue, try one of the following:
        - Start the app in development mode by running `next dev`, then open "/[slug]/server" in your browser to investigate the error.
        - Rerun the production build with `next build --debug-prerender` to generate better stack traces.
      [2025-09-12T13:34:02.185Z] Error:
          at async o (.next/server/app/[slug]/server/page.js:1:11119) {
        code: 'NEXT_STATIC_GEN_BAILOUT'
      }

PR fix notes

PR #83843: [Cache Components] Allow sync IO inside console methods

Description (problem / solution / changelog)

console methods may be patched to do things like add a timestamp to the log row. While we can't determine the meaning of reading the current time in arbitrary contexts we can know with reasonable certainty that any sync IO inside a console method is not being used in the logical output of the program. To account for this we can make a special affordance for sync IO inside of these methods that allows them to run without interrupting the prerender early.

To do this we patch these properties on the console global and set up a wrapper in the setter. This wrapper will make any assigned function into another function that exits the workUnitAsyncStorage scope before executing the wrapped function. This will allow new Date() and other sync IO APIs to be called in the internal implementation of these methods without triggering an error while prerendering.

Fixes NAR-403

Changed files

  • .vscode/settings.json (modified, +1/-0)
  • packages/next/src/server/app-render/app-render.tsx (modified, +4/-1)
  • packages/next/src/server/app-render/dev-logs-async-storage-instance.ts (added, +5/-0)
  • packages/next/src/server/app-render/dev-logs-async-storage.external.ts (added, +17/-0)
  • packages/next/src/server/node-environment-extensions/console-dev.tsx (modified, +6/-19)
  • packages/next/src/server/node-environment-extensions/console-exit.test.ts (added, +689/-0)
  • packages/next/src/server/node-environment-extensions/console-exit.tsx (added, +55/-0)
  • packages/next/src/server/node-environment.ts (modified, +1/-0)
  • test/e2e/app-dir/cache-components-errors/cache-components-console-patch.test.ts (added, +81/-0)
  • test/e2e/app-dir/cache-components-errors/fixtures/console-patch/app/layout.tsx (added, +9/-0)
  • test/e2e/app-dir/cache-components-errors/fixtures/console-patch/app/page.tsx (added, +12/-0)
  • test/e2e/app-dir/cache-components-errors/fixtures/console-patch/next.config.js (added, +10/-0)
  • test/e2e/app-dir/cache-components-errors/fixtures/console-patch/patch-console.js (added, +12/-0)

Code Example

git clone git@github.com:pieh/next-cache-components-timestamped-console-methods.git
   cd next-cache-components-timestamped-console-methods
   npm i
   npm run build
   npm run start
   # start script injects console patching: "NODE_OPTIONS='--import ./patch-console.mjs' next start"

---

[2025-09-12T13:34:01.133Z] A Cache Function (`use cache`) was passed to startActiveSpan which means it will receive a Span argument with a possibly random ID on every invocation leading to cache misses. Provide a wrapping function around the Cache Function that does not forward the Span argument to avoid this issue.

---

[2025-09-12T13:34:02.181Z] Error: Route "/[slug]/server" used `new Date()` before accessing either uncached data (e.g. `fetch()`) or Request data (e.g. `cookies()`, `headers()`, `connection()`, and `searchParams`). Accessing the current time in a Server Component requires reading one of these data sources first. Alternatively, consider moving this expression into a Client Component or Cache Component. See more info here: https://nextjs.org/docs/messages/next-prerender-current-time
         at console.<computed> [as error] (patch-console.mjs:7:18)
         at <unknown> (.next/server/app/[slug]/fallback/page.js:1:946)
         at a (.next/server/app/[slug]/fallback/page.js:1:2327)
     [2025-09-12T13:34:02.184Z] To get a more detailed stack trace and pinpoint the issue, try one of the following:
       - Start the app in development mode by running `next dev`, then open "/[slug]/server" in your browser to investigate the error.
       - Rerun the production build with `next build --debug-prerender` to generate better stack traces.
     [2025-09-12T13:34:02.185Z] Error:
         at async o (.next/server/app/[slug]/server/page.js:1:11119) {
       code: 'NEXT_STATIC_GEN_BAILOUT'
     }

---

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:30 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6020
  Available memory (MB): 32768
  Available CPU cores: 12
Binaries:
  Node: 20.19.0
  npm: 10.8.2
  Yarn: N/A
  pnpm: 8.15.8
Relevant Packages:
  next: 15.6.0-canary.6 // Latest available version is detected (15.6.0-canary.6).
  eslint-config-next: N/A
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.9.2
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/pieh/next-cache-components-timestamped-console-methods

To Reproduce

The reproduction loosely mimics console.* patching that is done by AWS Lambda Node Runtime where any of methods will get timestamp via new Date().

  1. Install, build and start the app:
    git clone [email protected]:pieh/next-cache-components-timestamped-console-methods.git
    cd next-cache-components-timestamped-console-methods
    npm i
    npm run build
    npm run start
    # start script injects console patching: "NODE_OPTIONS='--import ./patch-console.mjs' next start"
  2. Visit or curl http://localhost:3000/novel/server

Current vs. Expected behavior

Current:

Accessing http://localhost:3000/novel/server result in 500 error, due to:

  • A non-fatal warning is being logged with console.error (this is specific to this concrete reproduction, but any log would result in the same problem):
    [2025-09-12T13:34:01.133Z] A Cache Function (`use cache`) was passed to startActiveSpan which means it will receive a Span argument with a possibly random ID on every invocation leading to cache misses. Provide a wrapping function around the Cache Function that does not forward the Span argument to avoid this issue.
  • Because console patching make use of new Date(), logging above warning results in new Date() bailout and fatal/500 error:
    [2025-09-12T13:34:02.181Z] Error: Route "/[slug]/server" used `new Date()` before accessing either uncached data (e.g. `fetch()`) or Request data (e.g. `cookies()`, `headers()`, `connection()`, and `searchParams`). Accessing the current time in a Server Component requires reading one of these data sources first. Alternatively, consider moving this expression into a Client Component or Cache Component. See more info here: https://nextjs.org/docs/messages/next-prerender-current-time
        at console.<computed> [as error] (patch-console.mjs:7:18)
        at <unknown> (.next/server/app/[slug]/fallback/page.js:1:946)
        at a (.next/server/app/[slug]/fallback/page.js:1:2327)
    [2025-09-12T13:34:02.184Z] To get a more detailed stack trace and pinpoint the issue, try one of the following:
      - Start the app in development mode by running `next dev`, then open "/[slug]/server" in your browser to investigate the error.
      - Rerun the production build with `next build --debug-prerender` to generate better stack traces.
    [2025-09-12T13:34:02.185Z] Error:
        at async o (.next/server/app/[slug]/server/page.js:1:11119) {
      code: 'NEXT_STATIC_GEN_BAILOUT'
    }

Expected:

Usage of Date outside of Next.js rendering itself should not cause prerender bailout. Ideally global Date is not patched at all and maybe instead this could be scoped to Next app code itself during bundling if that is viable?

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:30 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6020
  Available memory (MB): 32768
  Available CPU cores: 12
Binaries:
  Node: 20.19.0
  npm: 10.8.2
  Yarn: N/A
  pnpm: 8.15.8
Relevant Packages:
  next: 15.6.0-canary.6 // Latest available version is detected (15.6.0-canary.6).
  eslint-config-next: N/A
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.9.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

cacheComponents

Which stage(s) are affected? (Select all that apply)

next start (local)

Additional context

There is workaround possible for this specific case by doing another console.* patch and temporarily restore global Date to its original, unpatched version -> call original console method -> restore patched Date again, but fact that it needs to be done is bad signal, because it indicate there is possibility of not yet known cases that might need similar workaround. For reference - this is a workaround that works for problems discovered so far https://github.com/opennextjs/opennextjs-netlify/commit/57d4b572abaa65f5a53a20e77ac75d85c2d5c9aa#diff-dcf9b6fe3dc0a3eaf360c9f67687ba91e75a41007e4e25a596edcfda5ed83d77R39-R62

Other than AWS Lambda Node Runtime specific interaction - another very similar case that comes to mind would be if someone would use some logging solution - like winston lib with its timestamp feature (but this one seems difficult to support without getting rid of heuristic completely, because this would become part of application code, so only workaround here is to not use timestamp feature, change logging library or attempt to adjust implementation of logging library).

Overall, it feels like the Date usage prerender bailout heuristic is leaking too much and will be ongoing source of issues with both deployment platform's internal usage of Date (something deployment targets will need to workaround) as well as 3rd party dependencies (users will be facing frustrations and will need to workaround/replace dependency/request implementation changes to move of new Date() to performance)

extent analysis

TL;DR

The most likely fix is to modify the console patching to temporarily restore the global Date to its original version when logging, to prevent the prerender bailout heuristic from being triggered.

Guidance

  • Identify the console patching code and modify it to restore the original Date when logging, as shown in the provided workaround example.
  • Consider scoping the Date patching to only Next.js app code during bundling, if viable, to prevent similar issues in the future.
  • Be aware that third-party dependencies using Date, such as logging libraries, may also trigger this issue and require workarounds or implementation changes.
  • Review the Next.js documentation on prerendering and the NEXT_STATIC_GEN_BAILOUT error to understand the underlying issue and potential solutions.

Example

// patch-console.mjs example
const originalDate = global.Date;
const originalConsoleError = console.error;

console.error = function(...args) {
  global.Date = originalDate;
  originalConsoleError(...args);
  global.Date = patchedDate;
};

Notes

The provided workaround is specific to this case, but similar issues may arise with other deployment platforms or third-party dependencies. A more comprehensive solution may require changes to the Next.js prerendering heuristic or the console patching implementation.

Recommendation

Apply the workaround by modifying the console patching code to temporarily restore the global Date to its original version when logging, as this is the most straightforward solution to prevent the prerender bailout heuristic from being triggered.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

nextjs - ✅(Solved) Fix `Date` usage tracking is leaking too much outside of Next.js rendering [1 pull requests, 3 comments, 3 participants]