Skip to content

Commit c98d947

Browse files
authored
fix(gatsby-plugin-image): Ensure cache hash is unique (#37087)
1 parent 3b42386 commit c98d947

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

packages/gatsby-plugin-image/src/babel-plugin-parse-static-images.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default function attrs({
2424
}): PluginObj {
2525
return {
2626
visitor: {
27-
JSXOpeningElement(nodePath): void {
27+
JSXOpeningElement(nodePath, state): void {
2828
if (
2929
!nodePath
3030
.get(`name`)
@@ -49,6 +49,8 @@ export default function attrs({
4949
console.warn(`[gatsby-plugin-image] ${error}`)
5050
}
5151

52+
// Adding the filename to the hashing, like in "extractStaticImageProps" function
53+
props.filename = state.filename
5254
const hash = hashOptions(props)
5355

5456
const cacheDir = (this.opts as Record<string, string>)?.cacheDir

packages/gatsby-plugin-image/src/components/static-image.server.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface IStaticImageProps
1111
extends Omit<GatsbyImageProps, "image">,
1212
Omit<ISharpGatsbyImageArgs, "backgroundColor"> {
1313
src: string
14+
filename?: string
1415
}
1516

1617
// These values are added by Babel. Do not add them manually

packages/gatsby-plugin-image/src/node-apis/parser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export function babelParseToAst(
7474
*/
7575
export const extractStaticImageProps = (
7676
ast: babel.types.File,
77+
filename: string,
7778
onError?: (prop: string, nodePath: NodePath) => void
7879
): Map<string, IStaticImageProps> => {
7980
const images: Map<string, IStaticImageProps> = new Map()
@@ -93,6 +94,10 @@ export const extractStaticImageProps = (
9394
nodePath as unknown as NodePath<JSXOpeningElement>,
9495
onError
9596
) as unknown as IStaticImageProps
97+
// When the image props are the same for multiple StaticImage but they are in different locations
98+
// the hash will be the same then. We need to make sure that the hash is unique.
99+
image.filename = filename
100+
96101
images.set(hashOptions(image), image)
97102
},
98103
})

packages/gatsby-plugin-image/src/node-apis/preprocess-source.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { PreprocessSourceArgs } from "gatsby"
22
import { babelParseToAst } from "./parser"
33
import path from "path"
44
import { extractStaticImageProps } from "./parser"
5-
import { codeFrameColumns } from "@babel/code-frame"
5+
import { codeFrameColumns, SourceLocation } from "@babel/code-frame"
66

77
import { writeImages } from "./image-processing"
88
import { getCacheDir } from "./node-utils"
@@ -46,16 +46,19 @@ export async function preprocessSource({
4646
},
4747
})
4848

49-
const images = extractStaticImageProps(ast, (prop, nodePath) => {
50-
const { start, end } = nodePath.node.loc
51-
const location = { start, end }
49+
const images = extractStaticImageProps(ast, filename, (prop, nodePath) => {
50+
const sourceLocation = nodePath.node.loc as SourceLocation
51+
const { start, end } = sourceLocation
5252
reporter.error({
5353
id: `95314`,
5454
filePath: filename,
55-
location,
55+
location: {
56+
start,
57+
end,
58+
},
5659
context: {
5760
prop,
58-
codeFrame: codeFrameColumns(contents, nodePath.node.loc, {
61+
codeFrame: codeFrameColumns(contents, sourceLocation, {
5962
linesAbove: 6,
6063
linesBelow: 6,
6164
highlightCode: true,

0 commit comments

Comments
 (0)