AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Turbopack Runtime Throws Invariant Error Due To Document.CurrentScript Being Null In Promise.Then

Turbopack-enabled Next.js apps fail to hydrate on Pale Moon because the runtime reads document.currentScript inside an asynchronous callback, where it is null.

mediumConfidence 75%Next.jsReact Server ComponentsApp RouterHydrationAffected V16.2.0-Canary.100

Origin Analysis

The Turbopack client runtime captures document.currentScript inside a Promise.then callback; by the time the microtask runs, the browser has already finished executing the script, so document.currentScript is null (especially strict in Pale Moon). Relying on this transient global for script URL resolution is a design flaw.
1. Open https://nextjs.org or any Turbopack-enabled Next.js app in Pale Moon.\n2. Open the browser console.\n3. Observe the invariant error: 'Expected document.currentScript to be a <script> element. Received null instead.' and hydration failure.

Fixing Code Block

--- a/packages/next/src/client/app-turbopack.ts +++ b/packages/next/src/client/app-turbopack.ts @@ -10,7 +10,9 @@ - Promise.resolve().then(() => { - const currentScript = document.currentScript + const currentScript = document.currentScript + Promise.resolve().then(() => { if (currentScript === null) { throw new Error('Invariant: Expected document.currentScript to be a <script> element. Received null instead. This is a bug in Next.js.') }
Move the document.currentScript read to synchronous module evaluation time, before any Promise scheduling. Store the script element in a local constant and reuse that reference asynchronously. This preserves the expected value even after the script has finished executing.

Edge Case Audit

Caching document.currentScript may keep a reference to a removed script element in memory until the asynchronous work completes. In module scripts or dynamically injected scripts where document.currentScript is undefined at evaluation time, this fix will still throw; consider a fallback to document.querySelector('script[src]') matching the script URL or use import.meta.url if available. Rolling back: revert to previous Next.js version or disable Turbopack (turbopack: false in next.config) until upstream fix is released.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state