Skip to content

Commit 7d93d2e

Browse files
authored
feat(gatsby-plugin-sharp): cache image metadata (#35791)
1 parent b802061 commit 7d93d2e

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

e2e-tests/development-runtime/cypress/integration/hot-reloading/new-file.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ describe(`client-side navigation to the new page`, () => {
66
})
77

88
it(`can navigate to newly created page using link`, () => {
9-
cy.findByTestId(`hot-reloading-new-file`).click().waitForRouteChange()
9+
cy.findByTestId(`hot-reloading-new-file`)
10+
.click({ force: true })
11+
.waitForRouteChange()
1012
cy.getTestElement(`message`).invoke(`text`).should(`contain`, `Hello`)
1113
})
1214
})

packages/gatsby-plugin-sharp/src/image-data.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,25 @@ const metadataCache = new Map<string, IImageMetadata>()
3636

3737
export async function getImageMetadata(
3838
file: FileNode,
39-
getDominantColor?: boolean
39+
getDominantColor?: boolean,
40+
cache?: GatsbyCache
4041
): Promise<IImageMetadata> {
4142
if (!getDominantColor) {
4243
// If we don't need the dominant color we can use the cheaper size function
4344
const { width, height, type } = await getImageSizeAsync(file)
4445
return { width, height, format: type }
4546
}
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+
}
4758
if (metadata && process.env.NODE_ENV !== `test`) {
4859
return metadata
4960
}
@@ -62,7 +73,11 @@ export async function getImageMetadata(
6273
: `#000000`
6374

6475
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+
}
6681
} catch (err) {
6782
reportError(`Failed to process image ${file.absolutePath}`, err)
6883
return {}
@@ -131,7 +146,11 @@ export async function generateImageData({
131146
)
132147
}
133148

134-
const metadata = await getImageMetadata(file, placeholder === `dominantColor`)
149+
const metadata = await getImageMetadata(
150+
file,
151+
placeholder === `dominantColor`,
152+
cache
153+
)
135154

136155
if ((args.width || args.height) && layout === `fullWidth`) {
137156
reporter.warn(
@@ -259,7 +278,11 @@ export async function generateImageData({
259278
if (!images?.length) {
260279
return undefined
261280
}
262-
const imageProps: IGatsbyImageData = {
281+
const imageProps: Pick<
282+
IGatsbyImageData,
283+
"backgroundColor" | "layout" | "placeholder" | "images"
284+
> &
285+
Partial<Pick<IGatsbyImageData, "width" | "height">> = {
263286
layout,
264287
placeholder: undefined,
265288
backgroundColor,
@@ -380,5 +403,5 @@ export async function generateImageData({
380403
imageProps.width = args.width || primaryImage.width || 1
381404
imageProps.height = (imageProps.width || 1) / primaryImage.aspectRatio
382405
}
383-
return imageProps
406+
return imageProps as IGatsbyImageData
384407
}

0 commit comments

Comments
 (0)