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
d3a53a6f
Unverified
Commit
d3a53a6f
authored
Mar 17, 2022
by
JJ Kasper
Committed by
GitHub
Mar 17, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update on-demand ISR to skip fetch locally (#35386)
parent
24f450e8
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
132 additions
and
101 deletions
+132
-101
node.ts
packages/next/server/api-utils/node.ts
+50
-44
image-optimizer.ts
packages/next/server/image-optimizer.ts
+7
-55
mock-request.ts
packages/next/server/lib/mock-request.ts
+70
-0
next-server.ts
packages/next/server/next-server.ts
+5
-2
No files found.
packages/next/server/api-utils/node.ts
View file @
d3a53a6f
...
@@ -31,6 +31,7 @@ import {
...
@@ -31,6 +31,7 @@ import {
SYMBOL_PREVIEW_DATA
,
SYMBOL_PREVIEW_DATA
,
RESPONSE_LIMIT_DEFAULT
,
RESPONSE_LIMIT_DEFAULT
,
}
from
'./index'
}
from
'./index'
import
{
mockRequest
}
from
'../lib/mock-request'
export
function
tryGetPreviewData
(
export
function
tryGetPreviewData
(
req
:
IncomingMessage
|
BaseNextRequest
,
req
:
IncomingMessage
|
BaseNextRequest
,
...
@@ -149,16 +150,17 @@ export async function parseBody(
...
@@ -149,16 +150,17 @@ export async function parseBody(
}
}
}
}
type
ApiContext
=
__ApiPreviewProps
&
{
trustHostHeader
?:
boolean
revalidate
?:
(
_req
:
IncomingMessage
,
_res
:
ServerResponse
)
=>
Promise
<
any
>
}
export
async
function
apiResolver
(
export
async
function
apiResolver
(
req
:
IncomingMessage
,
req
:
IncomingMessage
,
res
:
ServerResponse
,
res
:
ServerResponse
,
query
:
any
,
query
:
any
,
resolverModule
:
any
,
resolverModule
:
any
,
apiContext
:
__ApiPreviewProps
&
{
apiContext
:
ApiContext
,
trustHostHeader
?:
boolean
hostname
?:
string
port
?:
number
},
propagateError
:
boolean
,
propagateError
:
boolean
,
dev
?:
boolean
,
dev
?:
boolean
,
page
?:
string
page
?:
string
...
@@ -277,55 +279,59 @@ export async function apiResolver(
...
@@ -277,55 +279,59 @@ export async function apiResolver(
async
function
unstable_revalidate
(
async
function
unstable_revalidate
(
urlPath
:
string
,
urlPath
:
string
,
req
:
IncomingMessage
|
BaseNextRequest
,
req
:
IncomingMessage
,
context
:
{
context
:
ApiContext
hostname
?:
string
port
?:
number
previewModeId
:
string
trustHostHeader
?:
boolean
}
)
{
)
{
if
(
!
context
.
trustHostHeader
&&
(
!
context
.
hostname
||
!
context
.
port
))
{
throw
new
Error
(
`"hostname" and "port" must be provided when starting next to use "unstable_revalidate". See more here https://nextjs.org/docs/advanced-features/custom-server`
)
}
if
(
typeof
urlPath
!==
'string'
||
!
urlPath
.
startsWith
(
'/'
))
{
if
(
typeof
urlPath
!==
'string'
||
!
urlPath
.
startsWith
(
'/'
))
{
throw
new
Error
(
throw
new
Error
(
`Invalid urlPath provided to revalidate(), must be a path e.g. /blog/post-1, received
${
urlPath
}
`
`Invalid urlPath provided to revalidate(), must be a path e.g. /blog/post-1, received
${
urlPath
}
`
)
)
}
}
const
baseUrl
=
context
.
trustHostHeader
?
`https://
${
req
.
headers
.
host
}
`
:
`http://
${
context
.
hostname
}
:
${
context
.
port
}
`
const
extraHeaders
:
Record
<
string
,
string
|
undefined
>
=
{}
if
(
context
.
trustHostHeader
)
{
extraHeaders
.
cookie
=
req
.
headers
.
cookie
}
try
{
try
{
const
res
=
await
fetch
(
`
${
baseUrl
}${
urlPath
}
`
,
{
if
(
context
.
trustHostHeader
)
{
headers
:
{
const
res
=
await
fetch
(
`https://
${
req
.
headers
.
host
}${
urlPath
}
`
,
{
[
PRERENDER_REVALIDATE_HEADER
]:
context
.
previewModeId
,
headers
:
{
...
extraHeaders
,
[
PRERENDER_REVALIDATE_HEADER
]:
context
.
previewModeId
,
},
cookie
:
req
.
headers
.
cookie
||
''
,
})
},
})
// we use the cache header to determine successful revalidate as
// we use the cache header to determine successful revalidate as
// a non-200 status code can be returned from a successful revalidate
// a non-200 status code can be returned from a successful revalidate
// e.g. notFound: true returns 404 status code but is successful
// e.g. notFound: true returns 404 status code but is successful
const
cacheHeader
=
const
cacheHeader
=
res
.
headers
.
get
(
'x-vercel-cache'
)
||
res
.
headers
.
get
(
'x-nextjs-cache'
)
res
.
headers
.
get
(
'x-vercel-cache'
)
||
res
.
headers
.
get
(
'x-nextjs-cache'
)
if
(
cacheHeader
?.
toUpperCase
()
!==
'REVALIDATED'
)
{
throw
new
Error
(
`Invalid response
${
res
.
status
}
`
)
}
}
else
if
(
context
.
revalidate
)
{
const
{
req
:
mockReq
,
res
:
mockRes
,
streamPromise
,
}
=
mockRequest
(
urlPath
,
{
[
PRERENDER_REVALIDATE_HEADER
]:
context
.
previewModeId
,
},
'GET'
)
await
context
.
revalidate
(
mockReq
,
mockRes
)
await
streamPromise
if
(
cacheHeader
?.
toUpperCase
()
!==
'REVALIDATED'
)
{
if
(
mockRes
.
getHeader
(
'x-nextjs-cache'
)
!==
'REVALIDATED'
)
{
throw
new
Error
(
`Invalid response
${
res
.
status
}
`
)
throw
new
Error
(
`Invalid response
${
mockRes
.
status
}
`
)
}
}
else
{
throw
new
Error
(
`Invariant: required internal revalidate method not passed to api-utils`
)
}
}
}
catch
(
err
)
{
}
catch
(
err
:
unknown
)
{
throw
new
Error
(
`Failed to revalidate
${
urlPath
}
`
)
throw
new
Error
(
`Failed to revalidate
${
urlPath
}
:
${
isError
(
err
)
?
err
.
message
:
err
}
`
)
}
}
}
}
...
...
packages/next/server/image-optimizer.ts
View file @
d3a53a6f
...
@@ -8,7 +8,6 @@ import { IncomingMessage, ServerResponse } from 'http'
...
@@ -8,7 +8,6 @@ import { IncomingMessage, ServerResponse } from 'http'
import
isAnimated
from
'next/dist/compiled/is-animated'
import
isAnimated
from
'next/dist/compiled/is-animated'
import
contentDisposition
from
'next/dist/compiled/content-disposition'
import
contentDisposition
from
'next/dist/compiled/content-disposition'
import
{
join
}
from
'path'
import
{
join
}
from
'path'
import
Stream
from
'stream'
import
nodeUrl
,
{
UrlWithParsedQuery
}
from
'url'
import
nodeUrl
,
{
UrlWithParsedQuery
}
from
'url'
import
{
NextConfigComplete
}
from
'./config-shared'
import
{
NextConfigComplete
}
from
'./config-shared'
import
{
processBuffer
,
decodeBuffer
,
Operation
}
from
'./lib/squoosh/main'
import
{
processBuffer
,
decodeBuffer
,
Operation
}
from
'./lib/squoosh/main'
...
@@ -17,6 +16,7 @@ import { getContentType, getExtension } from './serve-static'
...
@@ -17,6 +16,7 @@ import { getContentType, getExtension } from './serve-static'
import
chalk
from
'next/dist/compiled/chalk'
import
chalk
from
'next/dist/compiled/chalk'
import
{
NextUrlWithParsedQuery
}
from
'./request-meta'
import
{
NextUrlWithParsedQuery
}
from
'./request-meta'
import
{
IncrementalCacheEntry
,
IncrementalCacheValue
}
from
'./response-cache'
import
{
IncrementalCacheEntry
,
IncrementalCacheValue
}
from
'./response-cache'
import
{
mockRequest
}
from
'./lib/mock-request'
type
XCacheHeader
=
'MISS'
|
'HIT'
|
'STALE'
type
XCacheHeader
=
'MISS'
|
'HIT'
|
'STALE'
...
@@ -307,60 +307,12 @@ export async function imageOptimizer(
...
@@ -307,60 +307,12 @@ export async function imageOptimizer(
maxAge
=
getMaxAge
(
upstreamRes
.
headers
.
get
(
'Cache-Control'
))
maxAge
=
getMaxAge
(
upstreamRes
.
headers
.
get
(
'Cache-Control'
))
}
else
{
}
else
{
try
{
try
{
const
resBuffers
:
Buffer
[]
=
[]
const
{
const
mockRes
:
any
=
new
Stream
.
Writable
()
resBuffers
,
req
:
mockReq
,
const
isStreamFinished
=
new
Promise
(
function
(
resolve
,
reject
)
{
res
:
mockRes
,
mockRes
.
on
(
'finish'
,
()
=>
resolve
(
true
))
streamPromise
:
isStreamFinished
,
mockRes
.
on
(
'end'
,
()
=>
resolve
(
true
))
}
=
mockRequest
(
href
,
_req
.
headers
,
_req
.
method
||
'GET'
,
_req
.
connection
)
mockRes
.
on
(
'error'
,
(
err
:
any
)
=>
reject
(
err
))
})
mockRes
.
write
=
(
chunk
:
Buffer
|
string
)
=>
{
resBuffers
.
push
(
Buffer
.
isBuffer
(
chunk
)
?
chunk
:
Buffer
.
from
(
chunk
))
}
mockRes
.
_write
=
(
chunk
:
Buffer
|
string
,
_encoding
:
string
,
callback
:
()
=>
void
)
=>
{
mockRes
.
write
(
chunk
)
// According to Node.js documentation, the callback MUST be invoked to signal that
// the write completed successfully. If this callback is not invoked, the 'finish' event
// will not be emitted.
// https://nodejs.org/docs/latest-v16.x/api/stream.html#writable_writechunk-encoding-callback
callback
()
}
const
mockHeaders
:
Record
<
string
,
string
|
string
[]
>
=
{}
mockRes
.
writeHead
=
(
_status
:
any
,
_headers
:
any
)
=>
Object
.
assign
(
mockHeaders
,
_headers
)
mockRes
.
getHeader
=
(
name
:
string
)
=>
mockHeaders
[
name
.
toLowerCase
()]
mockRes
.
getHeaders
=
()
=>
mockHeaders
mockRes
.
getHeaderNames
=
()
=>
Object
.
keys
(
mockHeaders
)
mockRes
.
setHeader
=
(
name
:
string
,
value
:
string
|
string
[])
=>
(
mockHeaders
[
name
.
toLowerCase
()]
=
value
)
mockRes
.
removeHeader
=
(
name
:
string
)
=>
{
delete
mockHeaders
[
name
.
toLowerCase
()]
}
mockRes
.
_implicitHeader
=
()
=>
{}
mockRes
.
connection
=
_res
.
connection
mockRes
.
finished
=
false
mockRes
.
statusCode
=
200
const
mockReq
:
any
=
new
Stream
.
Readable
()
mockReq
.
_read
=
()
=>
{
mockReq
.
emit
(
'end'
)
mockReq
.
emit
(
'close'
)
return
Buffer
.
from
(
''
)
}
mockReq
.
headers
=
_req
.
headers
mockReq
.
method
=
_req
.
method
mockReq
.
url
=
href
mockReq
.
connection
=
_req
.
connection
await
handleRequest
(
mockReq
,
mockRes
,
nodeUrl
.
parse
(
href
,
true
))
await
handleRequest
(
mockReq
,
mockRes
,
nodeUrl
.
parse
(
href
,
true
))
await
isStreamFinished
await
isStreamFinished
...
...
packages/next/server/lib/mock-request.ts
0 → 100644
View file @
d3a53a6f
import
Stream
from
'stream'
export
function
mockRequest
(
requestUrl
:
string
,
requestHeaders
:
Record
<
string
,
string
|
string
[]
|
undefined
>
,
requestMethod
:
string
,
requestConnection
?:
any
)
{
const
resBuffers
:
Buffer
[]
=
[]
const
mockRes
:
any
=
new
Stream
.
Writable
()
const
isStreamFinished
=
new
Promise
(
function
(
resolve
,
reject
)
{
mockRes
.
on
(
'finish'
,
()
=>
resolve
(
true
))
mockRes
.
on
(
'end'
,
()
=>
resolve
(
true
))
mockRes
.
on
(
'error'
,
(
err
:
any
)
=>
reject
(
err
))
})
mockRes
.
write
=
(
chunk
:
Buffer
|
string
)
=>
{
resBuffers
.
push
(
Buffer
.
isBuffer
(
chunk
)
?
chunk
:
Buffer
.
from
(
chunk
))
}
mockRes
.
_write
=
(
chunk
:
Buffer
|
string
,
_encoding
:
string
,
callback
:
()
=>
void
)
=>
{
mockRes
.
write
(
chunk
)
// According to Node.js documentation, the callback MUST be invoked to signal that
// the write completed successfully. If this callback is not invoked, the 'finish' event
// will not be emitted.
// https://nodejs.org/docs/latest-v16.x/api/stream.html#writable_writechunk-encoding-callback
callback
()
}
const
mockHeaders
:
Record
<
string
,
string
|
string
[]
>
=
{}
mockRes
.
writeHead
=
(
_status
:
any
,
_headers
:
any
)
=>
Object
.
assign
(
mockHeaders
,
_headers
)
mockRes
.
getHeader
=
(
name
:
string
)
=>
mockHeaders
[
name
.
toLowerCase
()]
mockRes
.
getHeaders
=
()
=>
mockHeaders
mockRes
.
getHeaderNames
=
()
=>
Object
.
keys
(
mockHeaders
)
mockRes
.
setHeader
=
(
name
:
string
,
value
:
string
|
string
[])
=>
(
mockHeaders
[
name
.
toLowerCase
()]
=
value
)
mockRes
.
removeHeader
=
(
name
:
string
)
=>
{
delete
mockHeaders
[
name
.
toLowerCase
()]
}
mockRes
.
_implicitHeader
=
()
=>
{}
mockRes
.
connection
=
requestConnection
mockRes
.
finished
=
false
mockRes
.
statusCode
=
200
const
mockReq
:
any
=
new
Stream
.
Readable
()
mockReq
.
_read
=
()
=>
{
mockReq
.
emit
(
'end'
)
mockReq
.
emit
(
'close'
)
return
Buffer
.
from
(
''
)
}
mockReq
.
headers
=
requestHeaders
mockReq
.
method
=
requestMethod
mockReq
.
url
=
requestUrl
mockReq
.
connection
=
requestConnection
return
{
resBuffers
,
req
:
mockReq
,
res
:
mockRes
,
streamPromise
:
isStreamFinished
,
}
}
packages/next/server/next-server.ts
View file @
d3a53a6f
...
@@ -554,8 +554,11 @@ export default class NextNodeServer extends BaseServer {
...
@@ -554,8 +554,11 @@ export default class NextNodeServer extends BaseServer {
pageModule
,
pageModule
,
{
{
...
this
.
renderOpts
.
previewProps
,
...
this
.
renderOpts
.
previewProps
,
port
:
this
.
port
,
revalidate
:
(
newReq
:
IncomingMessage
,
newRes
:
ServerResponse
)
=>
hostname
:
this
.
hostname
,
this
.
getRequestHandler
()(
new
NodeNextRequest
(
newReq
),
new
NodeNextResponse
(
newRes
)
),
// internal config so is not typed
// internal config so is not typed
trustHostHeader
:
(
this
.
nextConfig
.
experimental
as
any
).
trustHostHeader
,
trustHostHeader
:
(
this
.
nextConfig
.
experimental
as
any
).
trustHostHeader
,
},
},
...
...
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