Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CoCo_Code_Op2
next.js
提交
17cb1ef9
N
next.js
项目概览
CoCo_Code_Op2
/
next.js
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
next.js
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
17cb1ef9
编写于
1月 23, 2020
作者:
J
JJ Kasper
提交者:
Tim Neutkens
1月 23, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix rewriting to API routes not including query (#10223)
上级
e2de7a8e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
65 addition
and
12 deletion
+65
-12
packages/next/next-server/server/next-server.ts
packages/next/next-server/server/next-server.ts
+20
-12
test/integration/custom-routes/next.config.js
test/integration/custom-routes/next.config.js
+12
-0
test/integration/custom-routes/pages/api/hello.js
test/integration/custom-routes/pages/api/hello.js
+1
-0
test/integration/custom-routes/test/index.test.js
test/integration/custom-routes/test/index.test.js
+32
-0
未找到文件。
packages/next/next-server/server/next-server.ts
浏览文件 @
17cb1ef9
...
...
@@ -561,7 +561,8 @@ export default class Server {
const
handled
=
await
this
.
handleApiRequest
(
req
as
NextApiRequest
,
res
as
NextApiResponse
,
pathname
!
pathname
!
,
query
)
if
(
handled
)
{
return
{
finished
:
true
}
...
...
@@ -633,7 +634,8 @@ export default class Server {
private
async
handleApiRequest
(
req
:
IncomingMessage
,
res
:
ServerResponse
,
pathname
:
string
pathname
:
string
,
query
:
ParsedUrlQuery
)
{
let
page
=
pathname
let
params
:
Params
|
boolean
=
false
...
...
@@ -659,15 +661,17 @@ export default class Server {
const
builtPagePath
=
await
this
.
getPagePath
(
page
)
const
pageModule
=
require
(
builtPagePath
)
query
=
{
...
query
,
...
params
}
if
(
!
this
.
renderOpts
.
dev
&&
this
.
_isLikeServerless
)
{
if
(
typeof
pageModule
.
default
===
'
function
'
)
{
this
.
prepareServerlessUrl
(
req
,
query
)
await
pageModule
.
default
(
req
,
res
)
return
true
}
}
await
apiResolver
(
req
,
res
,
params
,
pageModule
,
this
.
onErrorMiddleware
)
await
apiResolver
(
req
,
res
,
query
,
pageModule
,
this
.
onErrorMiddleware
)
return
true
}
...
...
@@ -831,6 +835,18 @@ export default class Server {
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
(
req
:
IncomingMessage
,
res
:
ServerResponse
,
...
...
@@ -854,15 +870,7 @@ export default class Server {
if
(
!
isSSG
)
{
// handle serverless
if
(
isLikeServerless
)
{
const
curUrl
=
parseUrl
(
req
.
url
!
,
true
)
req
.
url
=
formatUrl
({
...
curUrl
,
search
:
undefined
,
query
:
{
...
curUrl
.
query
,
...
query
,
},
})
this
.
prepareServerlessUrl
(
req
,
query
)
return
result
.
Component
.
renderReqToHTML
(
req
,
res
)
}
...
...
test/integration/custom-routes/next.config.js
浏览文件 @
17cb1ef9
...
...
@@ -55,6 +55,18 @@ module.exports = {
source
:
'
/hidden/_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
()
{
...
...
test/integration/custom-routes/pages/api/hello.js
0 → 100644
浏览文件 @
17cb1ef9
export
default
async
(
req
,
res
)
=>
res
.
json
({
query
:
req
.
query
})
test/integration/custom-routes/test/index.test.js
浏览文件 @
17cb1ef9
...
...
@@ -260,6 +260,23 @@ const runTests = (isDev = false) => {
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
)
{
it
(
'
should output routes-manifest successfully
'
,
async
()
=>
{
const
manifest
=
await
fs
.
readJSON
(
...
...
@@ -476,6 +493,21 @@ const runTests = (isDev = false) => {
),
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
:
[
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录