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
17cb1ef9
Commit
17cb1ef9
authored
Jan 23, 2020
by
JJ Kasper
Committed by
Tim Neutkens
Jan 23, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix rewriting to API routes not including query (#10223)
parent
e2de7a8e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
12 deletions
+65
-12
next-server.ts
packages/next/next-server/server/next-server.ts
+20
-12
next.config.js
test/integration/custom-routes/next.config.js
+12
-0
hello.js
test/integration/custom-routes/pages/api/hello.js
+1
-0
index.test.js
test/integration/custom-routes/test/index.test.js
+32
-0
No files found.
packages/next/next-server/server/next-server.ts
View file @
17cb1ef9
...
@@ -561,7 +561,8 @@ export default class Server {
...
@@ -561,7 +561,8 @@ export default class Server {
const
handled
=
await
this
.
handleApiRequest
(
const
handled
=
await
this
.
handleApiRequest
(
req
as
NextApiRequest
,
req
as
NextApiRequest
,
res
as
NextApiResponse
,
res
as
NextApiResponse
,
pathname
!
pathname
!
,
query
)
)
if
(
handled
)
{
if
(
handled
)
{
return
{
finished
:
true
}
return
{
finished
:
true
}
...
@@ -633,7 +634,8 @@ export default class Server {
...
@@ -633,7 +634,8 @@ export default class Server {
private
async
handleApiRequest
(
private
async
handleApiRequest
(
req
:
IncomingMessage
,
req
:
IncomingMessage
,
res
:
ServerResponse
,
res
:
ServerResponse
,
pathname
:
string
pathname
:
string
,
query
:
ParsedUrlQuery
)
{
)
{
let
page
=
pathname
let
page
=
pathname
let
params
:
Params
|
boolean
=
false
let
params
:
Params
|
boolean
=
false
...
@@ -659,15 +661,17 @@ export default class Server {
...
@@ -659,15 +661,17 @@ export default class Server {
const
builtPagePath
=
await
this
.
getPagePath
(
page
)
const
builtPagePath
=
await
this
.
getPagePath
(
page
)
const
pageModule
=
require
(
builtPagePath
)
const
pageModule
=
require
(
builtPagePath
)
query
=
{
...
query
,
...
params
}
if
(
!
this
.
renderOpts
.
dev
&&
this
.
_isLikeServerless
)
{
if
(
!
this
.
renderOpts
.
dev
&&
this
.
_isLikeServerless
)
{
if
(
typeof
pageModule
.
default
===
'function'
)
{
if
(
typeof
pageModule
.
default
===
'function'
)
{
this
.
prepareServerlessUrl
(
req
,
query
)
await
pageModule
.
default
(
req
,
res
)
await
pageModule
.
default
(
req
,
res
)
return
true
return
true
}
}
}
}
await
apiResolver
(
req
,
res
,
params
,
pageModule
,
this
.
onErrorMiddleware
)
await
apiResolver
(
req
,
res
,
query
,
pageModule
,
this
.
onErrorMiddleware
)
return
true
return
true
}
}
...
@@ -831,6 +835,18 @@ export default class Server {
...
@@ -831,6 +835,18 @@ export default class Server {
res
.
end
(
payload
)
res
.
end
(
payload
)
}
}
private
prepareServerlessUrl
(
req
:
IncomingMessage
,
query
:
ParsedUrlQuery
)
{
const
curUrl
=
parseUrl
(
req
.
url
!
,
true
)
req
.
url
=
formatUrl
({
...
curUrl
,
search
:
undefined
,
query
:
{
...
curUrl
.
query
,
...
query
,
},
})
}
private
async
renderToHTMLWithComponents
(
private
async
renderToHTMLWithComponents
(
req
:
IncomingMessage
,
req
:
IncomingMessage
,
res
:
ServerResponse
,
res
:
ServerResponse
,
...
@@ -854,15 +870,7 @@ export default class Server {
...
@@ -854,15 +870,7 @@ export default class Server {
if
(
!
isSSG
)
{
if
(
!
isSSG
)
{
// handle serverless
// handle serverless
if
(
isLikeServerless
)
{
if
(
isLikeServerless
)
{
const
curUrl
=
parseUrl
(
req
.
url
!
,
true
)
this
.
prepareServerlessUrl
(
req
,
query
)
req
.
url
=
formatUrl
({
...
curUrl
,
search
:
undefined
,
query
:
{
...
curUrl
.
query
,
...
query
,
},
})
return
result
.
Component
.
renderReqToHTML
(
req
,
res
)
return
result
.
Component
.
renderReqToHTML
(
req
,
res
)
}
}
...
...
test/integration/custom-routes/next.config.js
View file @
17cb1ef9
...
@@ -55,6 +55,18 @@ module.exports = {
...
@@ -55,6 +55,18 @@ module.exports = {
source
:
'/hidden/_next/:path*'
,
source
:
'/hidden/_next/:path*'
,
destination
:
'/_next/:path*'
,
destination
:
'/_next/:path*'
,
},
},
{
source
:
'/api-hello'
,
destination
:
'/api/hello'
,
},
{
source
:
'/api-hello-regex/(.*)'
,
destination
:
'/api/hello?name=:1'
,
},
{
source
:
'/api-hello-param/:name'
,
destination
:
'/api/hello?name=:name'
,
},
]
]
},
},
async
redirects
()
{
async
redirects
()
{
...
...
test/integration/custom-routes/pages/api/hello.js
0 → 100644
View file @
17cb1ef9
export
default
async
(
req
,
res
)
=>
res
.
json
({
query
:
req
.
query
})
test/integration/custom-routes/test/index.test.js
View file @
17cb1ef9
...
@@ -260,6 +260,23 @@ const runTests = (isDev = false) => {
...
@@ -260,6 +260,23 @@ const runTests = (isDev = false) => {
expect
(
res
.
headers
.
get
(
'refresh'
)).
toBe
(
`0;url=/`
)
expect
(
res
.
headers
.
get
(
'refresh'
)).
toBe
(
`0;url=/`
)
})
})
it
(
'should handle basic api rewrite successfully'
,
async
()
=>
{
const
data
=
await
renderViaHTTP
(
appPort
,
'/api-hello'
)
expect
(
JSON
.
parse
(
data
)).
toEqual
({
query
:
{}
})
})
it
(
'should handle api rewrite with un-named param successfully'
,
async
()
=>
{
const
data
=
await
renderViaHTTP
(
appPort
,
'/api-hello-regex/hello/world'
)
expect
(
JSON
.
parse
(
data
)).
toEqual
({
query
:
{
'1'
:
'hello/world'
,
name
:
'hello/world'
},
})
})
it
(
'should handle api rewrite with param successfully'
,
async
()
=>
{
const
data
=
await
renderViaHTTP
(
appPort
,
'/api-hello-param/hello'
)
expect
(
JSON
.
parse
(
data
)).
toEqual
({
query
:
{
name
:
'hello'
}
})
})
if
(
!
isDev
)
{
if
(
!
isDev
)
{
it
(
'should output routes-manifest successfully'
,
async
()
=>
{
it
(
'should output routes-manifest successfully'
,
async
()
=>
{
const
manifest
=
await
fs
.
readJSON
(
const
manifest
=
await
fs
.
readJSON
(
...
@@ -476,6 +493,21 @@ const runTests = (isDev = false) => {
...
@@ -476,6 +493,21 @@ const runTests = (isDev = false) => {
),
),
source
:
'/hidden/_next/:path*'
,
source
:
'/hidden/_next/:path*'
,
},
},
{
destination
:
'/api/hello'
,
regex
:
normalizeRegEx
(
'^
\\
/api-hello$'
),
source
:
'/api-hello'
,
},
{
destination
:
'/api/hello?name=:1'
,
regex
:
normalizeRegEx
(
'^
\\
/api-hello-regex(?:
\\
/(.*))$'
),
source
:
'/api-hello-regex/(.*)'
,
},
{
destination
:
'/api/hello?name=:name'
,
regex
:
normalizeRegEx
(
'^
\\
/api-hello-param(?:
\\
/([^
\\
/]+?))$'
),
source
:
'/api-hello-param/:name'
,
},
],
],
dynamicRoutes
:
[
dynamicRoutes
:
[
{
{
...
...
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