Skip to content

fix(proxy): strip malformed Next-Router-State-Tree headers #70

Merged
mattdjenkinson merged 1 commit into
mainfrom
fix/malformed-router-state-header
Apr 8, 2026
Merged

fix(proxy): strip malformed Next-Router-State-Tree headers #70
mattdjenkinson merged 1 commit into
mainfrom
fix/malformed-router-state-header

Conversation

@mattdjenkinson
Copy link
Copy Markdown
Collaborator

Problem

After deployments, clients that had the login page open retain stale router state.
When they navigate, Next.js App Router sends an RSC request with a malformed
Next-Router-State-Tree header — specifically [""] instead of the full routing
tree. Next.js fails to parse this and throws an unhandled error:

Error: The router state header was sent but could not be parsed.

This surfaces on the /(main)/(illustration)/loginname/page route and is tracked
in Sentry:
https://sentry.prod.env.datum.net/organizations/sentry/issues/1918/events/105a342aff4e46f8946d950cb8c6a6bc/

Fix

Added a check in proxy.ts that validates the Next-Router-State-Tree header
before the request reaches the Next.js rendering pipeline. If the value cannot be
parsed as JSON or is structurally incomplete (fewer than 2 elements), the RSC-related
headers (Next-Router-State-Tree, Rsc, Next-Router-Prefetch) are stripped.

Without these headers, Next.js falls back to a full SSR page render, which resets
the client's router state and serves the page correctly.

Impact on normal users

  • Normal full page loads — no Next-Router-State-Tree header present, code is a no-op.
  • Normal RSC navigations — header parses correctly with 2+ elements, nothing is stripped.
  • Stale clients — headers stripped, user gets a full page load instead of an error.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@mattdjenkinson mattdjenkinson merged commit 848112a into main Apr 8, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants