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
04e4c457
Unverified
Commit
04e4c457
authored
Apr 06, 2020
by
JJ Kasper
Committed by
GitHub
Apr 06, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update to not modify req.url for getServerSideProps requests (#11637)
parent
7227824b
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
93 additions
and
18 deletions
+93
-18
next-server.ts
packages/next/next-server/server/next-server.ts
+18
-16
_app.js
test/integration/getserversideprops/pages/_app.js
+29
-0
index.js
...integration/getserversideprops/pages/blog/[post]/index.js
+3
-1
something.js
test/integration/getserversideprops/pages/something.js
+3
-1
index.test.js
test/integration/getserversideprops/test/index.test.js
+40
-0
No files found.
packages/next/next-server/server/next-server.ts
View file @
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
View file @
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
View file @
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
View file @
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
View file @
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`
)
...
...
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