File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ) {
You can’t perform that action at this time.
0 commit comments