Skip to content

Commit 848112a

Browse files
Merge pull request #70 from datum-cloud/fix/malformed-router-state-header
fix(proxy): strip malformed Next-Router-State-Tree headers
2 parents 2c0abbe + 1cf026b commit 848112a

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

apps/login/src/proxy.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ export async function proxy(request: NextRequest) {
4242
// Add the original URL as a header to all requests
4343
const requestHeaders = new Headers(request.headers);
4444

45+
// Strip malformed Next-Router-State-Tree headers to prevent Next.js from
46+
// throwing "The router state header was sent but could not be parsed."
47+
// This happens when stale clients (pre-deployment) send incomplete router state.
48+
const routerStateHeader = requestHeaders.get("Next-Router-State-Tree");
49+
if (routerStateHeader) {
50+
let isValid = false;
51+
try {
52+
const parsed = JSON.parse(decodeURIComponent(routerStateHeader));
53+
isValid = Array.isArray(parsed) && parsed.length >= 2;
54+
} catch {
55+
isValid = false;
56+
}
57+
if (!isValid) {
58+
requestHeaders.delete("Next-Router-State-Tree");
59+
requestHeaders.delete("Rsc");
60+
requestHeaders.delete("Next-Router-Prefetch");
61+
}
62+
}
63+
4564
// Extract "organization" search param from the URL and set it as a header if available
4665
const organization = request.nextUrl.searchParams.get("organization");
4766
if (organization) {

0 commit comments

Comments
 (0)