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

GetInitialProps Not Re-Called When Router.Replace/Push Href Contains Query And Hash With As Also Containing Hash

When using Router.replace or Router.push with a href that includes query parameters followed by a hash (e.g. /page?test=1#hash) and an as path that also includes the same hash, the getInitialProps method is not re-invoked on subsequent query changes. This breaks data fetching for masked-query patterns where query is hidden from the browser URL but hash must remain visible.

highConfidence 75%Next.js

Origin Analysis

The router's change detection parses the href without stripping the hash correctly before extracting the query. When the href contains a trailing hash, the query parsing either returns an empty object or the hash interferes with the comparison, causing the early return condition (route unchanged and query equal) to be truthy. Consequently, Router.set is never called, so getInitialProps is skipped.
1. Add the provided component to any page.\n2. Enable Test2 (href with query and #hash, as with #hash) and run the interval.\n3. Observe that the displayed query.test does not increment and getInitialProps console.log is not printed.\n4. Enable Test1 (href with query only, no #hash) and note that getInitialProps is called on each interval and the number increments.

Fixing Code Block

Edge Case Audit

This change alters URL parsing for hash-containing hrefs. Ensure that routes using hash-based navigation (e.g. /page#section) are not affected, and that dynamic routes with both query and hash still resolve correctly. Test with router.push, router.replace, and shallow routing. If issues arise, revert this block and rely on the original parseRelativeUrl implementation. Do not apply if the codebase already has a fixed parseRelativeUrl that strips hash before query parsing.

Ecosystem Topology