Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
37c98faa
Unverified
Commit
37c98faa
authored
Apr 11, 2020
by
JJ Kasper
Committed by
GitHub
Apr 11, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make sure to pass through query values for custom routes (#11812)
parent
099ea8b9
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
12 deletions
+66
-12
next-serverless-loader.ts
...ages/next/build/webpack/loaders/next-serverless-loader.ts
+2
-1
next-server.ts
packages/next/next-server/server/next-server.ts
+5
-3
router.ts
packages/next/next-server/server/router.ts
+14
-6
index.test.js
test/integration/custom-routes/test/index.test.js
+45
-2
No files found.
packages/next/build/webpack/loaders/next-serverless-loader.ts
View file @
37c98faa
...
@@ -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
...
...
packages/next/next-server/server/next-server.ts
View file @
37c98faa
...
@@ -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
...
...
packages/next/next-server/server/router.ts
View file @
37c98faa
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
...
...
test/integration/custom-routes/test/index.test.js
View file @
37c98faa
...
@@ -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'
)
})
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment