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

Mixing 'Use Cache' With DraftMode()/Cookies() In Same Page Causes ERR_HTTP_HEADERS_SENT And Metadata Boundary Hydration Mismatch

Next.js 16.2.1 App Router encounters streaming/resume conflicts when a page component calls a 'use cache' function before accessing dynamic APIs like draftMode() or cookies(), causing metadata boundary mismatch and header errors.

highConfidence 82%Next.jsHydrationReact Server ComponentsAffected V16.2.1

Origin Analysis

The Next.js streaming/resumption protocol does not properly sequence the metadata boundary (<__next_metadata_boundary__>) relative to the 'use cache' cache boundary. When a cached function begins streaming and then dynamic APIs attempt to read/write headers, the server attempts to set headers after the response has started, and the metadata boundary interleaves incorrectly, causing hydration mismatch.
1. Clone https://github.com/lucianobfs/nextjs-use-cache-metadata-boundary-repro 2. Run pnpm install, pnpm build, pnpm start 3. Visit http://localhost:3000/posts/1 4. Observe server terminal for ERR_HTTP_HEADERS_SENT and metadata boundary mismatch errors.

Fixing Code Block

Edge Case Audit

This workaround sacrifices static generation for the route (page shell), which may increase server resource usage and affect SEO or performance for pages that were previously statically generated. If `generateMetadata` still calls `connection()`, the route may already be dynamic, but this ensures consistency. Rollback: remove `export const dynamic` and revert to original ordering; however, the bug may reappear until an official Next.js 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