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
e3e62aa7
Unverified
Commit
e3e62aa7
authored
Feb 02, 2022
by
Steven
Committed by
GitHub
Feb 02, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix image cache race condition (#33883)
- Fixes #33860
parent
aae0370d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
21 deletions
+34
-21
image-optimizer.ts
packages/next/server/image-optimizer.ts
+34
-21
No files found.
packages/next/server/image-optimizer.ts
View file @
e3e62aa7
...
@@ -19,6 +19,8 @@ import { getContentType, getExtension } from './serve-static'
...
@@ -19,6 +19,8 @@ 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'
type
XCacheHeader
=
'MISS'
|
'HIT'
|
'STALE'
const
AVIF
=
'image/avif'
const
AVIF
=
'image/avif'
const
WEBP
=
'image/webp'
const
WEBP
=
'image/webp'
const
PNG
=
'image/png'
const
PNG
=
'image/png'
...
@@ -29,7 +31,7 @@ const CACHE_VERSION = 3
...
@@ -29,7 +31,7 @@ const CACHE_VERSION = 3
const
ANIMATABLE_TYPES
=
[
WEBP
,
PNG
,
GIF
]
const
ANIMATABLE_TYPES
=
[
WEBP
,
PNG
,
GIF
]
const
VECTOR_TYPES
=
[
SVG
]
const
VECTOR_TYPES
=
[
SVG
]
const
BLUR_IMG_SIZE
=
8
// should match `next-image-loader`
const
BLUR_IMG_SIZE
=
8
// should match `next-image-loader`
const
inflightRequests
=
new
Map
<
string
,
Promise
<
undefine
d
>>
()
const
inflightRequests
=
new
Map
<
string
,
Promise
<
voi
d
>>
()
let
sharp
:
let
sharp
:
|
((
|
((
...
@@ -178,18 +180,15 @@ export async function imageOptimizer(
...
@@ -178,18 +180,15 @@ 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
let
xCache
:
XCacheHeader
=
'MISS'
// 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.
if
(
inflightRequests
.
has
(
hash
))
{
if
(
inflightRequests
.
has
(
hash
))
{
await
inflightRequests
.
get
(
hash
)
await
inflightRequests
.
get
(
hash
)
}
}
let
dedupeResolver
:
(
val
?:
PromiseLike
<
undefined
>
)
=>
void
const
dedupe
=
new
Deferred
<
void
>
()
inflightRequests
.
set
(
inflightRequests
.
set
(
hash
,
dedupe
.
promise
)
hash
,
new
Promise
((
resolve
)
=>
(
dedupeResolver
=
resolve
))
)
try
{
try
{
if
(
await
fileExists
(
hashDir
,
'directory'
))
{
if
(
await
fileExists
(
hashDir
,
'directory'
))
{
...
@@ -200,8 +199,7 @@ export async function imageOptimizer(
...
@@ -200,8 +199,7 @@ 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
)
const
isFresh
=
now
<
expireAt
xCache
=
now
<
expireAt
?
'HIT'
:
'STALE'
const
xCache
=
isFresh
?
'HIT'
:
'STALE'
const
result
=
setResponseHeaders
(
const
result
=
setResponseHeaders
(
req
,
req
,
res
,
res
,
...
@@ -214,13 +212,17 @@ export async function imageOptimizer(
...
@@ -214,13 +212,17 @@ export async function imageOptimizer(
xCache
xCache
)
)
if
(
!
result
.
finished
)
{
if
(
!
result
.
finished
)
{
createReadStream
(
fsPath
).
pipe
(
res
)
await
new
Promise
<
void
>
((
resolve
,
reject
)
=>
{
createReadStream
(
fsPath
)
.
on
(
'end'
,
resolve
)
.
on
(
'error'
,
reject
)
.
pipe
(
res
)
})
}
}
if
(
isFresh
)
{
if
(
xCache
===
'HIT'
)
{
return
{
finished
:
true
}
return
{
finished
:
true
}
}
else
{
}
else
{
await
promises
.
unlink
(
fsPath
)
await
promises
.
unlink
(
fsPath
)
staleWhileRevalidate
=
true
}
}
}
}
}
}
...
@@ -338,7 +340,7 @@ export async function imageOptimizer(
...
@@ -338,7 +340,7 @@ export async function imageOptimizer(
upstreamBuffer
,
upstreamBuffer
,
isStatic
,
isStatic
,
isDev
,
isDev
,
staleWhileRevalidat
e
xCach
e
)
)
return
{
finished
:
true
}
return
{
finished
:
true
}
}
}
...
@@ -492,7 +494,7 @@ export async function imageOptimizer(
...
@@ -492,7 +494,7 @@ export async function imageOptimizer(
optimizedBuffer
,
optimizedBuffer
,
isStatic
,
isStatic
,
isDev
,
isDev
,
staleWhileRevalidat
e
xCach
e
)
)
}
else
{
}
else
{
throw
new
Error
(
'Unable to optimize buffer'
)
throw
new
Error
(
'Unable to optimize buffer'
)
...
@@ -507,14 +509,13 @@ export async function imageOptimizer(
...
@@ -507,14 +509,13 @@ export async function imageOptimizer(
upstreamBuffer
,
upstreamBuffer
,
isStatic
,
isStatic
,
isDev
,
isDev
,
staleWhileRevalidat
e
xCach
e
)
)
}
}
return
{
finished
:
true
}
return
{
finished
:
true
}
}
finally
{
}
finally
{
// Make sure to remove the hash in the end.
dedupe
.
resolve
()
dedupeResolver
!
()
inflightRequests
.
delete
(
hash
)
inflightRequests
.
delete
(
hash
)
}
}
}
}
...
@@ -557,7 +558,7 @@ function setResponseHeaders(
...
@@ -557,7 +558,7 @@ function setResponseHeaders(
contentType
:
string
|
null
,
contentType
:
string
|
null
,
isStatic
:
boolean
,
isStatic
:
boolean
,
isDev
:
boolean
,
isDev
:
boolean
,
xCache
:
'MISS'
|
'HIT'
|
'STALE'
xCache
:
XCacheHeader
)
{
)
{
res
.
setHeader
(
'Vary'
,
'Accept'
)
res
.
setHeader
(
'Vary'
,
'Accept'
)
res
.
setHeader
(
res
.
setHeader
(
...
@@ -597,12 +598,11 @@ function sendResponse(
...
@@ -597,12 +598,11 @@ function sendResponse(
buffer
:
Buffer
,
buffer
:
Buffer
,
isStatic
:
boolean
,
isStatic
:
boolean
,
isDev
:
boolean
,
isDev
:
boolean
,
staleWhileRevalidate
:
boolean
xCache
:
XCacheHeader
)
{
)
{
if
(
staleWhileRevalidate
)
{
if
(
xCache
===
'STALE'
)
{
return
return
}
}
const
xCache
=
'MISS'
const
etag
=
getHash
([
buffer
])
const
etag
=
getHash
([
buffer
])
const
result
=
setResponseHeaders
(
const
result
=
setResponseHeaders
(
req
,
req
,
...
@@ -782,3 +782,16 @@ export async function getImageSize(
...
@@ -782,3 +782,16 @@ export async function getImageSize(
const
{
width
,
height
}
=
imageSizeOf
(
buffer
)
const
{
width
,
height
}
=
imageSizeOf
(
buffer
)
return
{
width
,
height
}
return
{
width
,
height
}
}
}
export
class
Deferred
<
T
>
{
promise
:
Promise
<
T
>
resolve
!
:
(
value
:
T
)
=>
void
reject
!
:
(
error
?:
Error
)
=>
void
constructor
()
{
this
.
promise
=
new
Promise
((
resolve
,
reject
)
=>
{
this
.
resolve
=
resolve
this
.
reject
=
reject
})
}
}
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