Unverified Commit 37c98faa authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Make sure to pass through query values for custom routes (#11812)

parent 099ea8b9
...@@ -103,7 +103,8 @@ const nextServerlessLoader: loader.Loader = function() { ...@@ -103,7 +103,8 @@ const nextServerlessLoader: loader.Loader = function() {
if (params) { if (params) {
const { parsedDestination } = prepareDestination( const { parsedDestination } = prepareDestination(
rewrite.destination, rewrite.destination,
params params,
parsedUrl.query
) )
Object.assign(parsedUrl.query, parsedDestination.query, params) Object.assign(parsedUrl.query, parsedDestination.query, params)
delete parsedDestination.query delete parsedDestination.query
......
...@@ -496,10 +496,11 @@ export default class Server { ...@@ -496,10 +496,11 @@ export default class Server {
match: route.match, match: route.match,
statusCode: route.statusCode, statusCode: route.statusCode,
name: `Redirect route`, name: `Redirect route`,
fn: async (_req, res, params, _parsedUrl) => { fn: async (_req, res, params, parsedUrl) => {
const { parsedDestination } = prepareDestination( const { parsedDestination } = prepareDestination(
route.destination, route.destination,
params, params,
parsedUrl.query,
true true
) )
const updatedDestination = formatUrl(parsedDestination) const updatedDestination = formatUrl(parsedDestination)
...@@ -528,10 +529,11 @@ export default class Server { ...@@ -528,10 +529,11 @@ export default class Server {
type: route.type, type: route.type,
name: `Rewrite route`, name: `Rewrite route`,
match: route.match, match: route.match,
fn: async (req, res, params, _parsedUrl) => { fn: async (req, res, params, parsedUrl) => {
const { newUrl, parsedDestination } = prepareDestination( const { newUrl, parsedDestination } = prepareDestination(
route.destination, route.destination,
params params,
parsedUrl.query
) )
// external rewrite, proxy it // external rewrite, proxy it
......
import { IncomingMessage, ServerResponse } from 'http' import { IncomingMessage, ServerResponse } from 'http'
import { parse as parseUrl, UrlWithParsedQuery } from 'url' import { parse as parseUrl, UrlWithParsedQuery } from 'url'
import { ParsedUrlQuery } from 'querystring'
import { compile as compilePathToRegex } from 'next/dist/compiled/path-to-regexp' import { compile as compilePathToRegex } from 'next/dist/compiled/path-to-regexp'
import pathMatch from './lib/path-match' import pathMatch from './lib/path-match'
...@@ -36,6 +37,7 @@ export type PageChecker = (pathname: string) => Promise<boolean> ...@@ -36,6 +37,7 @@ export type PageChecker = (pathname: string) => Promise<boolean>
export const prepareDestination = ( export const prepareDestination = (
destination: string, destination: string,
params: Params, params: Params,
query: ParsedUrlQuery,
isRedirect?: boolean isRedirect?: boolean
) => { ) => {
const parsedDestination = parseUrl(destination, true) const parsedDestination = parseUrl(destination, true)
...@@ -62,7 +64,8 @@ export const prepareDestination = ( ...@@ -62,7 +64,8 @@ export const prepareDestination = (
destQuery[key] = value destQuery[key] = value
} }
// add params to query if it's not a redirect // add path params to query if it's not a redirect and not
// already defined in destination query
if (!isRedirect) { if (!isRedirect) {
for (const [name, value] of Object.entries(params)) { for (const [name, value] of Object.entries(params)) {
if (!(name in destQuery)) { if (!(name in destQuery)) {
...@@ -87,6 +90,16 @@ export const prepareDestination = ( ...@@ -87,6 +90,16 @@ export const prepareDestination = (
} }
throw err throw err
} }
// Query merge order lowest priority to highest
// 1. initial URL query values
// 2. path segment values
// 3. destination specified query values
parsedDestination.query = {
...query,
...parsedDestination.query,
}
return { return {
newUrl, newUrl,
parsedDestination, parsedDestination,
...@@ -203,11 +216,6 @@ export default class Router { ...@@ -203,11 +216,6 @@ export default class Router {
// Check if the match function matched // Check if the match function matched
if (newParams) { if (newParams) {
// Combine parameters and querystring
if (route.type === 'rewrite' || route.type === 'redirect') {
parsedUrlUpdated.query = { ...parsedUrlUpdated.query, ...newParams }
}
const result = await route.fn(req, res, newParams, parsedUrlUpdated) const result = await route.fn(req, res, newParams, parsedUrlUpdated)
// The response was handled // The response was handled
......
...@@ -181,6 +181,7 @@ const runTests = (isDev = false) => { ...@@ -181,6 +181,7 @@ const runTests = (isDev = false) => {
expect(query).toEqual({ expect(query).toEqual({
first: 'hello', first: 'hello',
second: 'world', second: 'world',
a: 'b',
}) })
}) })
...@@ -191,6 +192,31 @@ const runTests = (isDev = false) => { ...@@ -191,6 +192,31 @@ const runTests = (isDev = false) => {
expect(html).toMatch(/second/) expect(html).toMatch(/second/)
}) })
it('should handle query for rewrite correctly', async () => {
// query merge order lowest priority to highest
// 1. initial URL query values
// 2. path segment values
// 3. destination specified query values
const html = await renderViaHTTP(
appPort,
'/query-rewrite/first/second?section=overridden&name=overridden&first=overridden&second=overridden&keep=me'
)
const data = JSON.parse(
cheerio
.load(html)('p')
.text()
)
expect(data).toEqual({
first: 'first',
second: 'second',
section: 'first',
name: 'second',
keep: 'me',
})
})
// current routes order do not allow rewrites to override page // current routes order do not allow rewrites to override page
// but allow redirects to // but allow redirects to
it('should not allow rewrite to override page file', async () => { it('should not allow rewrite to override page file', async () => {
...@@ -281,9 +307,26 @@ const runTests = (isDev = false) => { ...@@ -281,9 +307,26 @@ const runTests = (isDev = false) => {
}) })
it('should support proxying to external resource', async () => { it('should support proxying to external resource', async () => {
const res = await fetchViaHTTP(appPort, '/proxy-me/first') const res = await fetchViaHTTP(appPort, '/proxy-me/first?keep=me&and=me')
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect([...externalServerHits]).toEqual(['/first?path=first']) expect(
[...externalServerHits].map(u => {
const { pathname, query } = url.parse(u, true)
return {
pathname,
query,
}
})
).toEqual([
{
pathname: '/first',
query: {
path: 'first',
keep: 'me',
and: 'me',
},
},
])
expect(await res.text()).toContain('hi from external') expect(await res.text()).toContain('hi from external')
}) })
......
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