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
d4fa6afa
Unverified
Commit
d4fa6afa
authored
Feb 28, 2020
by
Gerald Monaco
Committed by
GitHub
Feb 28, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract sendPayload and prepareServerlessUrl (#10732)
parent
586fdf4e
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
61 deletions
+72
-61
next-server.ts
packages/next/next-server/server/next-server.ts
+72
-61
No files found.
packages/next/next-server/server/next-server.ts
View file @
d4fa6afa
...
@@ -662,7 +662,7 @@ export default class Server {
...
@@ -662,7 +662,7 @@ export default class Server {
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
)
prepareServerlessUrl
(
req
,
query
)
await
pageModule
.
default
(
req
,
res
)
await
pageModule
.
default
(
req
,
res
)
return
true
return
true
}
}
...
@@ -823,50 +823,6 @@ export default class Server {
...
@@ -823,50 +823,6 @@ export default class Server {
return
null
return
null
}
}
private
__sendPayload
(
res
:
ServerResponse
,
payload
:
any
,
type
:
string
,
options
?:
{
revalidate
:
number
|
false
;
private
:
boolean
}
)
{
// TODO: ETag? Cache-Control headers? Next-specific headers?
res
.
setHeader
(
'Content-Type'
,
type
)
res
.
setHeader
(
'Content-Length'
,
Buffer
.
byteLength
(
payload
))
if
(
!
this
.
renderOpts
.
dev
)
{
if
(
options
?.
private
)
{
res
.
setHeader
(
'Cache-Control'
,
`private, no-cache, no-store, max-age=0, must-revalidate`
)
}
else
if
(
options
?.
revalidate
)
{
res
.
setHeader
(
'Cache-Control'
,
options
.
revalidate
<
0
?
`no-cache, no-store, must-revalidate`
:
`s-maxage=
${
options
.
revalidate
}
, stale-while-revalidate`
)
}
else
if
(
options
?.
revalidate
===
false
)
{
res
.
setHeader
(
'Cache-Control'
,
`s-maxage=31536000, stale-while-revalidate`
)
}
}
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
,
...
@@ -918,18 +874,20 @@ export default class Server {
...
@@ -918,18 +874,20 @@ export default class Server {
true
true
)
)
this
.
__
sendPayload
(
sendPayload
(
res
,
res
,
JSON
.
stringify
(
renderResult
?.
renderOpts
?.
pageData
),
JSON
.
stringify
(
renderResult
?.
renderOpts
?.
pageData
),
'application/json'
,
'application/json'
,
{
!
this
.
renderOpts
.
dev
?
{
revalidate
:
-
1
,
revalidate
:
-
1
,
private
:
false
,
// Leave to user-land caching
private
:
false
,
// Leave to user-land caching
}
}
:
undefined
)
)
return
null
return
null
}
}
this
.
prepareServerlessUrl
(
req
,
query
)
prepareServerlessUrl
(
req
,
query
)
return
(
components
.
Component
as
any
).
renderReqToHTML
(
req
,
res
)
return
(
components
.
Component
as
any
).
renderReqToHTML
(
req
,
res
)
}
}
...
@@ -939,10 +897,17 @@ export default class Server {
...
@@ -939,10 +897,17 @@ export default class Server {
...
opts
,
...
opts
,
isDataReq
,
isDataReq
,
})
})
this
.
__sendPayload
(
res
,
JSON
.
stringify
(
props
),
'application/json'
,
{
sendPayload
(
res
,
JSON
.
stringify
(
props
),
'application/json'
,
!
this
.
renderOpts
.
dev
?
{
revalidate
:
-
1
,
revalidate
:
-
1
,
private
:
false
,
// Leave to user-land caching
private
:
false
,
// Leave to user-land caching
})
}
:
undefined
)
return
null
return
null
}
}
...
@@ -972,11 +937,11 @@ export default class Server {
...
@@ -972,11 +937,11 @@ export default class Server {
?
JSON
.
stringify
(
cachedData
.
pageData
)
?
JSON
.
stringify
(
cachedData
.
pageData
)
:
cachedData
.
html
:
cachedData
.
html
this
.
__
sendPayload
(
sendPayload
(
res
,
res
,
data
,
data
,
isDataReq
?
'application/json'
:
'text/html; charset=utf-8'
,
isDataReq
?
'application/json'
:
'text/html; charset=utf-8'
,
cachedData
.
curRevalidate
!==
undefined
cachedData
.
curRevalidate
!==
undefined
&&
!
this
.
renderOpts
.
dev
?
{
revalidate
:
cachedData
.
curRevalidate
,
private
:
isPreviewMode
}
?
{
revalidate
:
cachedData
.
curRevalidate
,
private
:
isPreviewMode
}
:
undefined
:
undefined
)
)
...
@@ -1108,7 +1073,7 @@ export default class Server {
...
@@ -1108,7 +1073,7 @@ export default class Server {
else
{
else
{
query
.
__nextFallback
=
'true'
query
.
__nextFallback
=
'true'
if
(
isLikeServerless
)
{
if
(
isLikeServerless
)
{
this
.
prepareServerlessUrl
(
req
,
query
)
prepareServerlessUrl
(
req
,
query
)
html
=
await
(
components
.
Component
as
any
).
renderReqToHTML
(
req
,
res
)
html
=
await
(
components
.
Component
as
any
).
renderReqToHTML
(
req
,
res
)
}
else
{
}
else
{
html
=
(
await
renderToHTML
(
req
,
res
,
pathname
,
query
,
{
html
=
(
await
renderToHTML
(
req
,
res
,
pathname
,
query
,
{
...
@@ -1118,7 +1083,7 @@ export default class Server {
...
@@ -1118,7 +1083,7 @@ export default class Server {
}
}
}
}
this
.
__
sendPayload
(
res
,
html
,
'text/html; charset=utf-8'
)
sendPayload
(
res
,
html
,
'text/html; charset=utf-8'
)
}
}
const
{
const
{
...
@@ -1126,11 +1091,13 @@ export default class Server {
...
@@ -1126,11 +1091,13 @@ export default class Server {
value
:
{
html
,
pageData
,
sprRevalidate
},
value
:
{
html
,
pageData
,
sprRevalidate
},
}
=
await
doRender
(
ssgCacheKey
,
[])
}
=
await
doRender
(
ssgCacheKey
,
[])
if
(
!
isResSent
(
res
))
{
if
(
!
isResSent
(
res
))
{
this
.
__
sendPayload
(
sendPayload
(
res
,
res
,
isDataReq
?
JSON
.
stringify
(
pageData
)
:
html
,
isDataReq
?
JSON
.
stringify
(
pageData
)
:
html
,
isDataReq
?
'application/json'
:
'text/html; charset=utf-8'
,
isDataReq
?
'application/json'
:
'text/html; charset=utf-8'
,
{
revalidate
:
sprRevalidate
,
private
:
isPreviewMode
}
!
this
.
renderOpts
.
dev
?
{
revalidate
:
sprRevalidate
,
private
:
isPreviewMode
}
:
undefined
)
)
}
}
...
@@ -1352,3 +1319,47 @@ export default class Server {
...
@@ -1352,3 +1319,47 @@ export default class Server {
return
isTargetLikeServerless
(
this
.
nextConfig
.
target
)
return
isTargetLikeServerless
(
this
.
nextConfig
.
target
)
}
}
}
}
function
sendPayload
(
res
:
ServerResponse
,
payload
:
any
,
type
:
string
,
options
?:
{
revalidate
:
number
|
false
;
private
:
boolean
}
)
{
// TODO: ETag? Cache-Control headers? Next-specific headers?
res
.
setHeader
(
'Content-Type'
,
type
)
res
.
setHeader
(
'Content-Length'
,
Buffer
.
byteLength
(
payload
))
if
(
options
!=
null
)
{
if
(
options
?.
private
)
{
res
.
setHeader
(
'Cache-Control'
,
`private, no-cache, no-store, max-age=0, must-revalidate`
)
}
else
if
(
options
?.
revalidate
)
{
res
.
setHeader
(
'Cache-Control'
,
options
.
revalidate
<
0
?
`no-cache, no-store, must-revalidate`
:
`s-maxage=
${
options
.
revalidate
}
, stale-while-revalidate`
)
}
else
if
(
options
?.
revalidate
===
false
)
{
res
.
setHeader
(
'Cache-Control'
,
`s-maxage=31536000, stale-while-revalidate`
)
}
}
res
.
end
(
payload
)
}
function
prepareServerlessUrl
(
req
:
IncomingMessage
,
query
:
ParsedUrlQuery
)
{
const
curUrl
=
parseUrl
(
req
.
url
!
,
true
)
req
.
url
=
formatUrl
({
...
curUrl
,
search
:
undefined
,
query
:
{
...
curUrl
.
query
,
...
query
,
},
})
}
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