Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-plugin-sharp): cache image metadata #35791

Merged
merged 2 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ describe(`client-side navigation to the new page`, () => {
})

it(`can navigate to newly created page using link`, () => {
cy.findByTestId(`hot-reloading-new-file`).click().waitForRouteChange()
cy.findByTestId(`hot-reloading-new-file`)
.click({ force: true })
.waitForRouteChange()
cy.getTestElement(`message`).invoke(`text`).should(`contain`, `Hello`)
})
})
Expand Down
35 changes: 29 additions & 6 deletions packages/gatsby-plugin-sharp/src/image-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,25 @@ const metadataCache = new Map<string, IImageMetadata>()

export async function getImageMetadata(
file: FileNode,
getDominantColor?: boolean
getDominantColor?: boolean,
cache?: GatsbyCache
): Promise<IImageMetadata> {
if (!getDominantColor) {
// If we don't need the dominant color we can use the cheaper size function
const { width, height, type } = await getImageSizeAsync(file)
return { width, height, format: type }
}
let metadata = metadataCache.get(file.internal.contentDigest)

let metadata: IImageMetadata | undefined
const METADATA_KEY = `metadata-${file.internal.contentDigest}`

if (cache) {
// Use plugin cache
metadata = await cache.get(METADATA_KEY)
} else {
// Use in-memory cache instead
metadata = metadataCache.get(METADATA_KEY)
}
if (metadata && process.env.NODE_ENV !== `test`) {
return metadata
}
Expand All @@ -62,7 +73,11 @@ export async function getImageMetadata(
: `#000000`

metadata = { width, height, density, format, dominantColor }
metadataCache.set(file.internal.contentDigest, metadata)
if (cache) {
await cache.set(METADATA_KEY, metadata)
} else {
metadataCache.set(METADATA_KEY, metadata)
}
} catch (err) {
reportError(`Failed to process image ${file.absolutePath}`, err)
return {}
Expand Down Expand Up @@ -131,7 +146,11 @@ export async function generateImageData({
)
}

const metadata = await getImageMetadata(file, placeholder === `dominantColor`)
const metadata = await getImageMetadata(
file,
placeholder === `dominantColor`,
cache
)

if ((args.width || args.height) && layout === `fullWidth`) {
reporter.warn(
Expand Down Expand Up @@ -259,7 +278,11 @@ export async function generateImageData({
if (!images?.length) {
return undefined
}
const imageProps: IGatsbyImageData = {
const imageProps: Pick<
IGatsbyImageData,
"backgroundColor" | "layout" | "placeholder" | "images"
> &
Partial<Pick<IGatsbyImageData, "width" | "height">> = {
layout,
placeholder: undefined,
backgroundColor,
Expand Down Expand Up @@ -380,5 +403,5 @@ export async function generateImageData({
imageProps.width = args.width || primaryImage.width || 1
imageProps.height = (imageProps.width || 1) / primaryImage.aspectRatio
}
return imageProps
return imageProps as IGatsbyImageData
}