Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CoCo_Code_Op2
next.js
提交
04e4c457
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,发现更多精彩内容 >>
未验证
提交
04e4c457
编写于
4月 06, 2020
作者:
J
JJ Kasper
提交者:
GitHub
4月 06, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update to not modify req.url for getServerSideProps requests (#11637)
上级
7227824b
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
93 addition
and
18 deletion
+93
-18
packages/next/next-server/server/next-server.ts
packages/next/next-server/server/next-server.ts
+18
-16
test/integration/getserversideprops/pages/_app.js
test/integration/getserversideprops/pages/_app.js
+29
-0
test/integration/getserversideprops/pages/blog/[post]/index.js
...integration/getserversideprops/pages/blog/[post]/index.js
+3
-1
test/integration/getserversideprops/pages/something.js
test/integration/getserversideprops/pages/something.js
+3
-1
test/integration/getserversideprops/test/index.test.js
test/integration/getserversideprops/test/index.test.js
+40
-0
未找到文件。
packages/next/next-server/server/next-server.ts
浏览文件 @
04e4c457
...
...
@@ -431,7 +431,6 @@ export default class Server {
.
replace
(
/
\.
json$/
,
''
)
.
replace
(
/
\/
index$/
,
'
/
'
)
req
.
url
=
pathname
const
parsedUrl
=
parseUrl
(
pathname
,
true
)
await
this
.
render
(
req
,
...
...
@@ -794,9 +793,14 @@ export default class Server {
const
url
:
any
=
req
.
url
// we allow custom servers to call render for all URLs
// so check if we need to serve a static _next file or not.
// we don't modify the URL for _next/data request but still
// call render so we special case this to prevent an infinite loop
if
(
url
.
match
(
/^
\/
_next
\/
/
)
||
(
this
.
hasStaticDir
&&
url
.
match
(
/^
\/
static
\/
/
))
!
query
.
_nextDataReq
&&
(
url
.
match
(
/^
\/
_next
\/
/
)
||
(
this
.
hasStaticDir
&&
url
.
match
(
/^
\/
static
\/
/
)))
)
{
return
this
.
handleRequest
(
req
,
res
,
parsedUrl
)
}
...
...
@@ -926,17 +930,6 @@ export default class Server {
const
isDataReq
=
!!
query
.
_nextDataReq
delete
query
.
_nextDataReq
// Serverless requests need its URL transformed back into the original
// request path (to emulate lambda behavior in production)
if
(
isLikeServerless
&&
isDataReq
)
{
let
{
pathname
}
=
parseUrl
(
req
.
url
||
''
,
true
)
pathname
=
!
pathname
||
pathname
===
'
/
'
?
'
/index
'
:
pathname
req
.
url
=
formatUrl
({
pathname
:
`/_next/data/
${
this
.
buildId
}${
pathname
}
.json`
,
query
,
})
}
let
previewData
:
string
|
false
|
object
|
undefined
let
isPreviewMode
=
false
...
...
@@ -1009,10 +1002,19 @@ export default class Server {
return
html
}
// Compute the
SPR
cache key
cons
t
urlPathname
=
`
${
parseUrl
(
req
.
url
||
''
).
pathname
!
}${
// Compute the
iSSG
cache key
le
t
urlPathname
=
`
${
parseUrl
(
req
.
url
||
''
).
pathname
!
}${
query
.
amp
?
'
.amp
'
:
''
}
`
// remove /_next/data prefix from urlPathname so it matches
// for direct page visit and /_next/data visit
if
(
isDataReq
&&
urlPathname
.
includes
(
this
.
buildId
))
{
urlPathname
=
(
urlPathname
.
split
(
this
.
buildId
).
pop
()
||
'
/
'
)
.
replace
(
/
\.
json$/
,
''
)
.
replace
(
/
\/
index$/
,
'
/
'
)
}
const
ssgCacheKey
=
isPreviewMode
?
`__`
+
nanoid
()
// Preview mode uses a throw away key to not coalesce preview invokes
:
urlPathname
...
...
test/integration/getserversideprops/pages/_app.js
0 → 100644
浏览文件 @
04e4c457
import
App
from
'
next/app
'
class
MyApp
extends
App
{
static
async
getInitialProps
(
ctx
)
{
const
{
req
,
query
,
pathname
,
asPath
}
=
ctx
.
ctx
let
pageProps
=
{}
if
(
ctx
.
Component
.
getInitialProps
)
{
pageProps
=
await
ctx
.
Component
.
getInitialProps
(
ctx
.
ctx
)
}
return
{
appProps
:
{
url
:
(
req
||
{}).
url
,
query
,
pathname
,
asPath
,
},
pageProps
,
}
}
render
()
{
const
{
Component
,
pageProps
,
appProps
}
=
this
.
props
return
<
Component
{...
pageProps
}
appProps
=
{
appProps
}
/
>
}
}
export
default
MyApp
test/integration/getserversideprops/pages/blog/[post]/index.js
浏览文件 @
04e4c457
...
...
@@ -22,13 +22,15 @@ export async function getServerSideProps({ params }) {
}
}
export
default
({
post
,
time
,
params
})
=>
{
export
default
({
post
,
time
,
params
,
appProps
})
=>
{
return
(
<>
<
p
>
Post
:
{
post
}
<
/p
>
<
span
>
time
:
{
time
}
<
/span
>
<
div
id
=
"
params
"
>
{
JSON
.
stringify
(
params
)}
<
/div
>
<
div
id
=
"
query
"
>
{
JSON
.
stringify
(
useRouter
().
query
)}
<
/div
>
<
div
id
=
"
app-query
"
>
{
JSON
.
stringify
(
appProps
.
query
)}
<
/div
>
<
div
id
=
"
app-url
"
>
{
appProps
.
url
}
<
/div
>
<
Link
href
=
"
/
"
>
<
a
id
=
"
home
"
>
to
home
<
/a
>
<
/Link
>
...
...
test/integration/getserversideprops/pages/something.js
浏览文件 @
04e4c457
...
...
@@ -14,7 +14,7 @@ export async function getServerSideProps({ params, query }) {
}
}
export
default
({
world
,
time
,
params
,
random
,
query
})
=>
{
export
default
({
world
,
time
,
params
,
random
,
query
,
appProps
})
=>
{
return
(
<>
<
p
>
hello
:
{
world
}
<
/p
>
...
...
@@ -23,6 +23,8 @@ export default ({ world, time, params, random, query }) => {
<
div
id
=
"
params
"
>
{
JSON
.
stringify
(
params
)}
<
/div
>
<
div
id
=
"
initial-query
"
>
{
JSON
.
stringify
(
query
)}
<
/div
>
<
div
id
=
"
query
"
>
{
JSON
.
stringify
(
useRouter
().
query
)}
<
/div
>
<
div
id
=
"
app-query
"
>
{
JSON
.
stringify
(
appProps
.
query
)}
<
/div
>
<
div
id
=
"
app-url
"
>
{
appProps
.
url
}
<
/div
>
<
Link
href
=
"
/
"
>
<
a
id
=
"
home
"
>
to
home
<
/a
>
<
/Link
>
...
...
test/integration/getserversideprops/test/index.test.js
浏览文件 @
04e4c457
...
...
@@ -260,6 +260,46 @@ const runTests = (dev = false) => {
expect
(
data
.
pageProps
.
params
).
toEqual
({
path
:
[
'
first
'
]
})
})
it
(
'
should have original req.url for /_next/data request dynamic page
'
,
async
()
=>
{
const
curUrl
=
`/_next/data/
${
buildId
}
/blog/post-1.json`
const
data
=
await
renderViaHTTP
(
appPort
,
curUrl
)
const
{
appProps
}
=
JSON
.
parse
(
data
)
expect
(
appProps
).
toEqual
({
url
:
curUrl
,
query
:
{
post
:
'
post-1
'
},
asPath
:
curUrl
,
pathname
:
'
/blog/[post]
'
,
})
})
it
(
'
should have original req.url for /_next/data request
'
,
async
()
=>
{
const
curUrl
=
`/_next/data/
${
buildId
}
/something.json`
const
data
=
await
renderViaHTTP
(
appPort
,
curUrl
)
const
{
appProps
}
=
JSON
.
parse
(
data
)
expect
(
appProps
).
toEqual
({
url
:
curUrl
,
query
:
{},
asPath
:
curUrl
,
pathname
:
'
/something
'
,
})
})
it
(
'
should have correct req.url and query for direct visit dynamic page
'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'
/blog/post-1
'
)
const
$
=
cheerio
.
load
(
html
)
expect
(
$
(
'
#app-url
'
).
text
()).
toContain
(
'
/blog/post-1
'
)
expect
(
JSON
.
parse
(
$
(
'
#app-query
'
).
text
())).
toEqual
({
post
:
'
post-1
'
})
})
it
(
'
should have correct req.url and query for direct visit
'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'
/something
'
)
const
$
=
cheerio
.
load
(
html
)
expect
(
$
(
'
#app-url
'
).
text
()).
toContain
(
'
/something
'
)
expect
(
JSON
.
parse
(
$
(
'
#app-query
'
).
text
())).
toEqual
({})
})
it
(
'
should return data correctly
'
,
async
()
=>
{
const
data
=
JSON
.
parse
(
await
renderViaHTTP
(
appPort
,
`/_next/data/
${
buildId
}
/something.json`
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录