@@ -36,14 +36,25 @@ const metadataCache = new Map<string, IImageMetadata>()
36
36
37
37
export async function getImageMetadata (
38
38
file : FileNode ,
39
- getDominantColor ?: boolean
39
+ getDominantColor ?: boolean ,
40
+ cache ?: GatsbyCache
40
41
) : Promise < IImageMetadata > {
41
42
if ( ! getDominantColor ) {
42
43
// If we don't need the dominant color we can use the cheaper size function
43
44
const { width, height, type } = await getImageSizeAsync ( file )
44
45
return { width, height, format : type }
45
46
}
46
- let metadata = metadataCache . get ( file . internal . contentDigest )
47
+
48
+ let metadata : IImageMetadata | undefined
49
+ const METADATA_KEY = `metadata-${ file . internal . contentDigest } `
50
+
51
+ if ( cache ) {
52
+ // Use plugin cache
53
+ metadata = await cache . get ( METADATA_KEY )
54
+ } else {
55
+ // Use in-memory cache instead
56
+ metadata = metadataCache . get ( METADATA_KEY )
57
+ }
47
58
if ( metadata && process . env . NODE_ENV !== `test` ) {
48
59
return metadata
49
60
}
@@ -62,7 +73,11 @@ export async function getImageMetadata(
62
73
: `#000000`
63
74
64
75
metadata = { width, height, density, format, dominantColor }
65
- metadataCache . set ( file . internal . contentDigest , metadata )
76
+ if ( cache ) {
77
+ await cache . set ( METADATA_KEY , metadata )
78
+ } else {
79
+ metadataCache . set ( METADATA_KEY , metadata )
80
+ }
66
81
} catch ( err ) {
67
82
reportError ( `Failed to process image ${ file . absolutePath } ` , err )
68
83
return { }
@@ -131,7 +146,11 @@ export async function generateImageData({
131
146
)
132
147
}
133
148
134
- const metadata = await getImageMetadata ( file , placeholder === `dominantColor` )
149
+ const metadata = await getImageMetadata (
150
+ file ,
151
+ placeholder === `dominantColor` ,
152
+ cache
153
+ )
135
154
136
155
if ( ( args . width || args . height ) && layout === `fullWidth` ) {
137
156
reporter . warn (
@@ -259,7 +278,11 @@ export async function generateImageData({
259
278
if ( ! images ?. length ) {
260
279
return undefined
261
280
}
262
- const imageProps : IGatsbyImageData = {
281
+ const imageProps : Pick <
282
+ IGatsbyImageData ,
283
+ "backgroundColor" | "layout" | "placeholder" | "images"
284
+ > &
285
+ Partial < Pick < IGatsbyImageData , "width" | "height" > > = {
263
286
layout,
264
287
placeholder : undefined ,
265
288
backgroundColor,
@@ -380,5 +403,5 @@ export async function generateImageData({
380
403
imageProps . width = args . width || primaryImage . width || 1
381
404
imageProps . height = ( imageProps . width || 1 ) / primaryImage . aspectRatio
382
405
}
383
- return imageProps
406
+ return imageProps as IGatsbyImageData
384
407
}
0 commit comments