Skip to main content
GoodFirstPicks
DashboardIssuesReposLeaderboard

GoodFirstPicks by Leaveitblank © 2026

CreatorRequest a RepoPrivacy PolicyTerms of Service
ERR_HTTP_HEADERS_SENT and __next_metadata_boundary__ hydration mismatch when mixing "use cache" with draftMode()/cookies() in same page component | GoodFirstPicks

ERR_HTTP_HEADERS_SENT and __next_metadata_boundary__ hydration mismatch when mixing "use cache" with draftMode()/cookies() in same page component

vercel/next.js 0 comments 1d ago
View on GitHub
highopenScope: somewhat clearSkill match: maybeNext.jsTypeScriptReact

Why this is a good first issue

Complex interaction between caching, metadata boundaries, and dynamic APIs causes server-side errors.

AI Summary

The issue involves a hydration mismatch and header timing problem when combining cached data with dynamic APIs in Next.js. It requires understanding of Next.js rendering pipeline and metadata handling. The solution likely needs changes to how metadata boundaries interact with cached content.

Issue Description

Link to the code that reproduces this issue

https://github.com/lucianobfs/nextjs-use-cache-metadata-boundary-repro

To Reproduce

  1. Clone the repo and run:
    pnpm install
    pnpm build
    pnpm start
    
  2. Visit http://localhost:3000/posts/1
  3. The page renders correctly (HTTP 200), but check the server terminal for errors

The pattern that triggers the bug (app/posts/[slug]/page.tsx):

  1. generateMetadata() calls await connection() — required because the underlying library uses crypto.randomBytes() (e.g., Effect-TS, Prisma)
  2. The page component calls a "use cache" function first (cachedPostData)
  3. Then accesses draftMode() and searchParams — dynamic APIs called outside the cache scope, as the docs recommend

Current vs. Expected behavior

Current: The page renders correctly (HTTP 200), but the server logs contain two recurring errors:

Error A — Metadata boundary mismatch (most frequent): ``` Error: Expected the resume to render

in this slot but instead it rendered <next_metadata_boundary>. The tree doesn't match so React will fallback to client rendering. { digest: '2161942505' } ```

Error B — ERR_HTTP_HEADERS_SENT (intermittent): ``` Error: Cannot set headers after they are sent to the client at g.setHeader (.next/server/chunks/ssr/[root-of-the-server]__*.js) { code: 'ERR_HTTP_HEADERS_SENT', digest: '1390897066' } ```

Expected: No server-side errors. The cached portion and the dynamic portion should coexist without hydration mismatches or header conflicts.

Impact

Although pages return HTTP 200, these errors cause:

  1. Performance degradation: React falls back to full client rendering, discarding streamed SSR output
  2. Wasted server resources: Full SSR work gets thrown away
  3. SEO risk: Crawlers may receive incomplete HTML before client JS executes
  4. Log noise: ~20-30 errors/hour in production with moderate traffic

Observed patterns from

Want to work on this?

Claim this issue to let others know you're working on it. You'll earn 30 points when you complete it!

Risk Flags

  • hydration mismatch
  • headers timing issue
  • requires Next.js internals knowledge
Loading labels...

Details

Points30 pts
Difficultyhigh
Scopesomewhat clear
Skill Matchmaybe
Test Focusedno