Next.js layout redirect looped 604 times on client navigation, causing server memory errors

A developer's redirect added to a Next.js App Router layout worked correctly on full page loads but caused an infinite loop during client-side navigation. When a new user's browser fetched the target page as an RSC payload, the layout redirect became an error that the client router replayed repeatedly. In one twelve-minute window, one browser requested the same page 604 times, and 79 Cloudflare Worker invocations exceeded their memory limit. The author traced the cause to the layout segment being cached and re-throwing the same redirect on each render. The proposed fix is to redirect only on real page loads, using the Sec-Fetch-Mode header to tell navigation requests apart from RSC fetches.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in