Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign up[Next 9.5.4+] Router.push duplicates query params in production mode, but not in dev mode #17840
Comments
|
I can work on this. |
|
It seems that this bug exists as far back as version Thoughts? |
|
I spent about two hours faffing with this, particularly the if (
isDynamicRoute(finalUrl.pathname) &&
finalUrl.searchParams &&
resolveAs // I think this is usually undefined when called from router.push()?
) {
// throw new Error('HERE ' + finalUrl.search)
const query = searchParamsToUrlQuery(finalUrl.searchParams)
const { result, params } = interpolateAs(
finalUrl.pathname,
finalUrl.pathname,
query
)
if (result) {
interpolatedAs = formatWithValidation({
pathname: result,
hash: finalUrl.hash,
query: omitParmsFromQuery(query, params), // the only time we call omitParamsFromQuery()
})
}
}
// ... execution continues until:
return (resolveAs // again, appears to be undefined
? [resolvedHref, interpolatedAs || resolvedHref] // confused about returning an array here
: resolvedHref) as stringMaybe a red herring but this was my suspicion - hard to debug with so many assumptions built in. |
|
@erikdstock @toolmantim I think I know the source of the bug. Here Now I'm wondering whether we should fix the prod or the dev version due to backwards compatibility It seems to occurs due to let resolvedAs = as <-- "/posts/[pid]?pid=1234&foo=bar"
if (process.env.__NEXT_HAS_REWRITES) {
// this code is only run in dev
// here dev will set resolvedAs to "/posts/[pid]"
resolvedAs = resolveRewrites(
parseRelativeUrl(as).pathname, <------------------ "/posts/[pid]"
pages,
basePath,
rewrites,
query,
(p: string) => this._resolveHref({ pathname: p }, pages).pathname!
)
if (resolvedAs !== as) {
const potentialHref = removePathTrailingSlash(
this._resolveHref(
Object.assign({}, parsed, { pathname: resolvedAs }),
pages,
false
).pathname!
)
// if this directly matches a page we need to update the href to
// allow the correct page chunk to be loaded
if (pages.includes(potentialHref)) {
route = potentialHref
pathname = potentialHref
parsed.pathname = pathname
url = formatWithValidation(parsed)
}
}
}
// in dev resolvedAs = "/posts/[pid]" & and in prod resolvedAs = "/posts/[pid]?pid=1234&foo=bar"
resolvedAs = delLocale(delBasePath(resolvedAs), this.locale)This is because inside resolveRewrites export default function resolveRewrites(
asPath: string,
pages: string[],
basePath: string,
rewrites: Rewrite[],
query: ParsedUrlQuery,
resolveHref: (path: string) => string
) {
if (!pages.includes(asPath)) { <------------------- this is false
// ....
}
return asPath <---------- // returns /posts/[pid]
}changing this to |


Bug report
Describe the bug
I recently came across this bug while using the "new"
router.pushmethod. Using the followingIn dev, this directs me to
/posts/1234?foo=barIn prod, this directs me to
/posts/1234?pid=1234&foo=barTo Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
yarn buildyarn startlocalhost:3000The ButtonExpected behavior
I would have expected production to run the same as in dev mode. Like I said above,
pushing with this informationShould send me to
/posts/1234?foo=bar.System information