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
16b5bfa7
Unverified
Commit
16b5bfa7
authored
Jan 27, 2022
by
Steven
Committed by
GitHub
Jan 27, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `stale-while-revalidate` pattern to Image Optimization API (#33735)
- Resolves #27208
parent
eea3adc5
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
97 additions
and
21 deletions
+97
-21
image-optimizer.ts
packages/next/server/image-optimizer.ts
+36
-20
index.test.js
test/integration/image-optimizer/test/index.test.js
+61
-1
No files found.
packages/next/server/image-optimizer.ts
View file @
16b5bfa7
...
@@ -178,6 +178,7 @@ export async function imageOptimizer(
...
@@ -178,6 +178,7 @@ export async function imageOptimizer(
const
imagesDir
=
join
(
distDir
,
'cache'
,
'images'
)
const
imagesDir
=
join
(
distDir
,
'cache'
,
'images'
)
const
hashDir
=
join
(
imagesDir
,
hash
)
const
hashDir
=
join
(
imagesDir
,
hash
)
const
now
=
Date
.
now
()
const
now
=
Date
.
now
()
let
staleWhileRevalidate
=
false
// If there're concurrent requests hitting the same resource and it's still
// If there're concurrent requests hitting the same resource and it's still
// being optimized, wait before accessing the cache.
// being optimized, wait before accessing the cache.
...
@@ -199,7 +200,8 @@ export async function imageOptimizer(
...
@@ -199,7 +200,8 @@ export async function imageOptimizer(
const
expireAt
=
Number
(
expireAtSt
)
const
expireAt
=
Number
(
expireAtSt
)
const
contentType
=
getContentType
(
extension
)
const
contentType
=
getContentType
(
extension
)
const
fsPath
=
join
(
hashDir
,
file
)
const
fsPath
=
join
(
hashDir
,
file
)
if
(
now
<
expireAt
)
{
const
isFresh
=
now
<
expireAt
const
xCache
=
isFresh
?
'HIT'
:
'STALE'
const
result
=
setResponseHeaders
(
const
result
=
setResponseHeaders
(
req
,
req
,
res
,
res
,
...
@@ -208,14 +210,17 @@ export async function imageOptimizer(
...
@@ -208,14 +210,17 @@ export async function imageOptimizer(
maxAge
,
maxAge
,
contentType
,
contentType
,
isStatic
,
isStatic
,
isDev
isDev
,
xCache
)
)
if
(
!
result
.
finished
)
{
if
(
!
result
.
finished
)
{
createReadStream
(
fsPath
).
pipe
(
res
)
createReadStream
(
fsPath
).
pipe
(
res
)
}
}
if
(
isFresh
)
{
return
{
finished
:
true
}
return
{
finished
:
true
}
}
else
{
}
else
{
await
promises
.
unlink
(
fsPath
)
await
promises
.
unlink
(
fsPath
)
staleWhileRevalidate
=
true
}
}
}
}
}
}
...
@@ -332,7 +337,8 @@ export async function imageOptimizer(
...
@@ -332,7 +337,8 @@ export async function imageOptimizer(
upstreamType
,
upstreamType
,
upstreamBuffer
,
upstreamBuffer
,
isStatic
,
isStatic
,
isDev
isDev
,
staleWhileRevalidate
)
)
return
{
finished
:
true
}
return
{
finished
:
true
}
}
}
...
@@ -485,7 +491,8 @@ export async function imageOptimizer(
...
@@ -485,7 +491,8 @@ export async function imageOptimizer(
contentType
,
contentType
,
optimizedBuffer
,
optimizedBuffer
,
isStatic
,
isStatic
,
isDev
isDev
,
staleWhileRevalidate
)
)
}
else
{
}
else
{
throw
new
Error
(
'Unable to optimize buffer'
)
throw
new
Error
(
'Unable to optimize buffer'
)
...
@@ -499,7 +506,8 @@ export async function imageOptimizer(
...
@@ -499,7 +506,8 @@ export async function imageOptimizer(
upstreamType
,
upstreamType
,
upstreamBuffer
,
upstreamBuffer
,
isStatic
,
isStatic
,
isDev
isDev
,
staleWhileRevalidate
)
)
}
}
...
@@ -548,7 +556,8 @@ function setResponseHeaders(
...
@@ -548,7 +556,8 @@ function setResponseHeaders(
maxAge
:
number
,
maxAge
:
number
,
contentType
:
string
|
null
,
contentType
:
string
|
null
,
isStatic
:
boolean
,
isStatic
:
boolean
,
isDev
:
boolean
isDev
:
boolean
,
xCache
:
'MISS'
|
'HIT'
|
'STALE'
)
{
)
{
res
.
setHeader
(
'Vary'
,
'Accept'
)
res
.
setHeader
(
'Vary'
,
'Accept'
)
res
.
setHeader
(
res
.
setHeader
(
...
@@ -574,6 +583,7 @@ function setResponseHeaders(
...
@@ -574,6 +583,7 @@ function setResponseHeaders(
}
}
res
.
setHeader
(
'Content-Security-Policy'
,
`script-src 'none'; sandbox;`
)
res
.
setHeader
(
'Content-Security-Policy'
,
`script-src 'none'; sandbox;`
)
res
.
setHeader
(
'X-Nextjs-Cache'
,
xCache
)
return
{
finished
:
false
}
return
{
finished
:
false
}
}
}
...
@@ -586,8 +596,13 @@ function sendResponse(
...
@@ -586,8 +596,13 @@ function sendResponse(
contentType
:
string
|
null
,
contentType
:
string
|
null
,
buffer
:
Buffer
,
buffer
:
Buffer
,
isStatic
:
boolean
,
isStatic
:
boolean
,
isDev
:
boolean
isDev
:
boolean
,
staleWhileRevalidate
:
boolean
)
{
)
{
if
(
staleWhileRevalidate
)
{
return
}
const
xCache
=
'MISS'
const
etag
=
getHash
([
buffer
])
const
etag
=
getHash
([
buffer
])
const
result
=
setResponseHeaders
(
const
result
=
setResponseHeaders
(
req
,
req
,
...
@@ -597,7 +612,8 @@ function sendResponse(
...
@@ -597,7 +612,8 @@ function sendResponse(
maxAge
,
maxAge
,
contentType
,
contentType
,
isStatic
,
isStatic
,
isDev
isDev
,
xCache
)
)
if
(
!
result
.
finished
)
{
if
(
!
result
.
finished
)
{
res
.
end
(
buffer
)
res
.
end
(
buffer
)
...
...
test/integration/image-optimizer/test/index.test.js
View file @
16b5bfa7
...
@@ -500,6 +500,49 @@ function runTests({
...
@@ -500,6 +500,49 @@ function runTests({
)
)
await
expectWidth
(
res
,
w
)
await
expectWidth
(
res
,
w
)
})
})
it
(
'should use cache and stale-while-revalidate when query is the same for external image'
,
async
()
=>
{
await
fs
.
remove
(
imagesDir
)
const
url
=
'https://image-optimization-test.vercel.app/test.jpg'
const
query
=
{
url
,
w
,
q
:
39
}
const
opts
=
{
headers
:
{
accept
:
'image/webp'
}
}
const
res1
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
expect
(
res1
.
status
).
toBe
(
200
)
expect
(
res1
.
headers
.
get
(
'X-Nextjs-Cache'
)).
toBe
(
'MISS'
)
expect
(
res1
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/webp'
)
expect
(
res1
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
`inline; filename="test.webp"`
)
const
json1
=
await
fsToJson
(
imagesDir
)
expect
(
Object
.
keys
(
json1
).
length
).
toBe
(
1
)
const
res2
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
expect
(
res2
.
status
).
toBe
(
200
)
expect
(
res2
.
headers
.
get
(
'X-Nextjs-Cache'
)).
toBe
(
'HIT'
)
expect
(
res2
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/webp'
)
expect
(
res2
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
`inline; filename="test.webp"`
)
const
json2
=
await
fsToJson
(
imagesDir
)
expect
(
json2
).
toStrictEqual
(
json1
)
if
(
ttl
)
{
// Wait until expired so we can confirm image is regenerated
await
waitFor
(
ttl
*
1000
)
const
res3
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
expect
(
res3
.
status
).
toBe
(
200
)
expect
(
res3
.
headers
.
get
(
'X-Nextjs-Cache'
)).
toBe
(
'STALE'
)
expect
(
res3
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/webp'
)
expect
(
res3
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
`inline; filename="test.webp"`
)
const
json3
=
await
fsToJson
(
imagesDir
)
expect
(
json3
).
not
.
toStrictEqual
(
json1
)
expect
(
Object
.
keys
(
json3
).
length
).
toBe
(
1
)
}
})
}
}
it
(
'should fail when url has file protocol'
,
async
()
=>
{
it
(
'should fail when url has file protocol'
,
async
()
=>
{
...
@@ -532,7 +575,7 @@ function runTests({
...
@@ -532,7 +575,7 @@ function runTests({
})
})
}
}
it
(
'should use cache
d image file when parameters are the sam
e'
,
async
()
=>
{
it
(
'should use cache
and stale-while-revalidate when query is the same for internal imag
e'
,
async
()
=>
{
await
fs
.
remove
(
imagesDir
)
await
fs
.
remove
(
imagesDir
)
const
query
=
{
url
:
'/test.png'
,
w
,
q
:
80
}
const
query
=
{
url
:
'/test.png'
,
w
,
q
:
80
}
...
@@ -540,6 +583,7 @@ function runTests({
...
@@ -540,6 +583,7 @@ function runTests({
const
res1
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
const
res1
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
expect
(
res1
.
status
).
toBe
(
200
)
expect
(
res1
.
status
).
toBe
(
200
)
expect
(
res1
.
headers
.
get
(
'X-Nextjs-Cache'
)).
toBe
(
'MISS'
)
expect
(
res1
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/webp'
)
expect
(
res1
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/webp'
)
expect
(
res1
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
expect
(
res1
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
`inline; filename="test.webp"`
`inline; filename="test.webp"`
...
@@ -549,6 +593,7 @@ function runTests({
...
@@ -549,6 +593,7 @@ function runTests({
const
res2
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
const
res2
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
expect
(
res2
.
status
).
toBe
(
200
)
expect
(
res2
.
status
).
toBe
(
200
)
expect
(
res2
.
headers
.
get
(
'X-Nextjs-Cache'
)).
toBe
(
'HIT'
)
expect
(
res2
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/webp'
)
expect
(
res2
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/webp'
)
expect
(
res2
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
expect
(
res2
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
`inline; filename="test.webp"`
`inline; filename="test.webp"`
...
@@ -561,6 +606,7 @@ function runTests({
...
@@ -561,6 +606,7 @@ function runTests({
await
waitFor
(
ttl
*
1000
)
await
waitFor
(
ttl
*
1000
)
const
res3
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
const
res3
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
expect
(
res3
.
status
).
toBe
(
200
)
expect
(
res3
.
status
).
toBe
(
200
)
expect
(
res3
.
headers
.
get
(
'X-Nextjs-Cache'
)).
toBe
(
'STALE'
)
expect
(
res3
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/webp'
)
expect
(
res3
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/webp'
)
expect
(
res3
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
expect
(
res3
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
`inline; filename="test.webp"`
`inline; filename="test.webp"`
...
@@ -579,6 +625,7 @@ function runTests({
...
@@ -579,6 +625,7 @@ function runTests({
const
res1
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
const
res1
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
expect
(
res1
.
status
).
toBe
(
200
)
expect
(
res1
.
status
).
toBe
(
200
)
expect
(
res1
.
headers
.
get
(
'X-Nextjs-Cache'
)).
toBe
(
'MISS'
)
expect
(
res1
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/svg+xml'
)
expect
(
res1
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/svg+xml'
)
expect
(
res1
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
expect
(
res1
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
`inline; filename="test.svg"`
`inline; filename="test.svg"`
...
@@ -588,6 +635,7 @@ function runTests({
...
@@ -588,6 +635,7 @@ function runTests({
const
res2
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
const
res2
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
expect
(
res2
.
status
).
toBe
(
200
)
expect
(
res2
.
status
).
toBe
(
200
)
expect
(
res2
.
headers
.
get
(
'X-Nextjs-Cache'
)).
toBe
(
'HIT'
)
expect
(
res2
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/svg+xml'
)
expect
(
res2
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/svg+xml'
)
expect
(
res2
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
expect
(
res2
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
`inline; filename="test.svg"`
`inline; filename="test.svg"`
...
@@ -604,6 +652,7 @@ function runTests({
...
@@ -604,6 +652,7 @@ function runTests({
const
res1
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
const
res1
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
expect
(
res1
.
status
).
toBe
(
200
)
expect
(
res1
.
status
).
toBe
(
200
)
expect
(
res1
.
headers
.
get
(
'X-Nextjs-Cache'
)).
toBe
(
'MISS'
)
expect
(
res1
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/gif'
)
expect
(
res1
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/gif'
)
expect
(
res1
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
expect
(
res1
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
`inline; filename="animated.gif"`
`inline; filename="animated.gif"`
...
@@ -613,6 +662,7 @@ function runTests({
...
@@ -613,6 +662,7 @@ function runTests({
const
res2
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
const
res2
=
await
fetchViaHTTP
(
appPort
,
'/_next/image'
,
query
,
opts
)
expect
(
res2
.
status
).
toBe
(
200
)
expect
(
res2
.
status
).
toBe
(
200
)
expect
(
res2
.
headers
.
get
(
'X-Nextjs-Cache'
)).
toBe
(
'HIT'
)
expect
(
res2
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/gif'
)
expect
(
res2
.
headers
.
get
(
'Content-Type'
)).
toBe
(
'image/gif'
)
expect
(
res2
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
expect
(
res2
.
headers
.
get
(
'Content-Disposition'
)).
toBe
(
`inline; filename="animated.gif"`
`inline; filename="animated.gif"`
...
@@ -810,6 +860,16 @@ function runTests({
...
@@ -810,6 +860,16 @@ function runTests({
const
json1
=
await
fsToJson
(
imagesDir
)
const
json1
=
await
fsToJson
(
imagesDir
)
expect
(
Object
.
keys
(
json1
).
length
).
toBe
(
1
)
expect
(
Object
.
keys
(
json1
).
length
).
toBe
(
1
)
const
xCache1
=
res1
.
headers
.
get
(
'X-Nextjs-Cache'
)
const
xCache2
=
res2
.
headers
.
get
(
'X-Nextjs-Cache'
)
if
(
xCache1
===
'HIT'
)
{
expect
(
xCache1
).
toBe
(
'HIT'
)
expect
(
xCache2
).
toBe
(
'MISS'
)
}
else
{
expect
(
xCache1
).
toBe
(
'MISS'
)
expect
(
xCache2
).
toBe
(
'HIT'
)
}
})
})
if
(
isDev
||
isSharp
)
{
if
(
isDev
||
isSharp
)
{
...
...
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