Unverified Commit 1b4d463c authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update rewrite params query appending (#16189)

This updates to not automatically append params to the query for rewrites if one or more of the params are already used in the destination's path. No other behavior is being changed and if the user still wants the params in the query after using them in the destination's path they can manually add them like with redirects.

Closes: https://github.com/vercel/next.js/issues/15626
parent 746000ea
......@@ -163,7 +163,7 @@ const nextServerlessLoader: loader.Loader = function () {
"${basePath}"
)
Object.assign(parsedUrl.query, parsedDestination.query, params)
Object.assign(parsedUrl.query, parsedDestination.query)
delete parsedDestination.query
Object.assign(parsedUrl, parsedDestination)
......
import { ParsedUrlQuery } from 'querystring'
import { searchParamsToUrlQuery } from './querystring'
import { parseRelativeUrl } from './parse-relative-url'
import { compile as compilePathToRegex } from 'next/dist/compiled/path-to-regexp'
import * as pathToRegexp from 'next/dist/compiled/path-to-regexp'
type Params = { [param: string]: any }
......@@ -49,9 +49,16 @@ export default function prepareDestination(
parsedDestination.searchParams
)
const destQuery = parsedDestination.query
const destPath = `${parsedDestination.pathname!}${
parsedDestination.hash || ''
}`
const destPathParamKeys: pathToRegexp.Key[] = []
pathToRegexp.pathToRegexp(destPath, destPathParamKeys)
let destinationCompiler = compilePathToRegex(
`${parsedDestination.pathname!}${parsedDestination.hash || ''}`,
const destPathParams = destPathParamKeys.map((key) => key.name)
let destinationCompiler = pathToRegexp.compile(
destPath,
// we don't validate while compiling the destination since we should
// have already validated before we got to this point and validating
// breaks compiling destinations with named pattern params from the source
......@@ -69,18 +76,23 @@ export default function prepareDestination(
// the value needs to start with a forward-slash to be compiled
// correctly
value = `/${value}`
const queryCompiler = compilePathToRegex(value, { validate: false })
const queryCompiler = pathToRegexp.compile(value, { validate: false })
value = queryCompiler(params).substr(1)
}
destQuery[key] = value
}
// add path params to query if it's not a redirect and not
// already defined in destination query
if (appendParamsToQuery) {
for (const [name, value] of Object.entries(params)) {
if (!(name in destQuery)) {
destQuery[name] = value
// already defined in destination query or path
const paramKeys = Object.keys(params)
if (
appendParamsToQuery &&
!paramKeys.some((key) => destPathParams.includes(key))
) {
for (const key of paramKeys) {
if (!(key in destQuery)) {
destQuery[key] = params[key]
}
}
}
......
......@@ -173,6 +173,12 @@ const runTests = (isDev = false) => {
expect(html).toMatch(/Hello/)
})
it('should not append params when one is used in destination path', async () => {
const html = await renderViaHTTP(appPort, '/test/with-params?a=b')
const $ = cheerio.load(html)
expect(JSON.parse($('p').text())).toEqual({ a: 'b' })
})
it('should double redirect successfully', async () => {
const html = await renderViaHTTP(appPort, '/docs/github')
expect(html).toMatch(/hi there/)
......@@ -397,7 +403,6 @@ const runTests = (isDev = false) => {
{
pathname: '/first',
query: {
path: 'first',
keep: 'me',
and: 'me',
},
......@@ -1164,7 +1169,6 @@ describe('Custom routes', () => {
expect(data).toEqual({
query: {
slug: 'first',
name: 'first',
hello: 'first',
},
})
......
......@@ -810,7 +810,6 @@ const runTests = (dev = false, isEmulatedServerless = false) => {
post: `post-${item}`,
})
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
item: item + '',
post: `post-${item}`,
})
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment