AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Router.Replace With Shallow:True Persists Shallow Flag In History State, Causing GetServerSideProps To Be Skipped On Browser Back Navigation

When router.replace is called with shallow: true in Next.js Pages Router, the shallow flag is stored in the browser history state. On subsequent browser back navigation to that entry, the router reuses the flag and skips getServerSideProps, rendering stale data.

highConfidence 90%Next.js

Origin Analysis

The Next.js router stores the entire options object (including shallow) in window.history.state for both push and replace operations. When handling popstate, the router reads the stored options and applies shallow routing again, causing getServerSideProps to be bypassed on back navigation.
1. Call router.push('/page1') (data fetch occurs). 2. Call router.replace('/page1?count=1', { shallow: true }) (no data fetch, URL updates). 3. Call router.push('/page2') (data fetch occurs). 4. Call router.replace('/page2?count=1', { shallow: true }) (no data fetch). 5. Click browser back to return to '/page1?count=1'. 6. Observe that getServerSideProps is not called and the page shows stale data.

Fixing Code Block

Edge Case Audit

The patch modifies the shape of the history state by overriding the shallow flag. If any custom code or Next.js internal logic relies on reading history.state.options.shallow, it may be affected. However, shallow is not meant to be persisted. The fix is safe for standard navigation but should be thoroughly tested in applications that use custom history management or third-party routing wrappers. To roll back, revert the change to store the original options object. Note that the patch must be applied to the correct Next.js source file and a rebuild is required; using a prebuilt dist may require patching the compiled file, which is not recommended.

Ecosystem Topology