Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rewrite params query appending #16189

Merged
merged 4 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 21 additions & 9 deletions packages/next/next-server/lib/router/utils/prepare-destination.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 }

Expand Down Expand Up @@ -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
Expand All @@ -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]
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions test/integration/custom-routes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down Expand Up @@ -397,7 +403,6 @@ const runTests = (isDev = false) => {
{
pathname: '/first',
query: {
path: 'first',
keep: 'me',
and: 'me',
},
Expand Down Expand Up @@ -1164,7 +1169,6 @@ describe('Custom routes', () => {
expect(data).toEqual({
query: {
slug: 'first',
name: 'first',
hello: 'first',
},
})
Expand Down
1 change: 0 additions & 1 deletion test/integration/prerender/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
})
})
Expand Down