Skip to content

Commit

Permalink
fix(gatsby-plugin-image): Ensure cache hash is unique (#37087)
Browse files Browse the repository at this point in the history
  • Loading branch information
LekoArts committed Nov 29, 2022
1 parent 3b42386 commit c98d947
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
Expand Up @@ -24,7 +24,7 @@ export default function attrs({
}): PluginObj {
return {
visitor: {
JSXOpeningElement(nodePath): void {
JSXOpeningElement(nodePath, state): void {
if (
!nodePath
.get(`name`)
Expand All @@ -49,6 +49,8 @@ export default function attrs({
console.warn(`[gatsby-plugin-image] ${error}`)
}

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

const cacheDir = (this.opts as Record<string, string>)?.cacheDir
Expand Down
Expand Up @@ -11,6 +11,7 @@ export interface IStaticImageProps
extends Omit<GatsbyImageProps, "image">,
Omit<ISharpGatsbyImageArgs, "backgroundColor"> {
src: string
filename?: string
}

// These values are added by Babel. Do not add them manually
Expand Down
5 changes: 5 additions & 0 deletions packages/gatsby-plugin-image/src/node-apis/parser.ts
Expand Up @@ -74,6 +74,7 @@ export function babelParseToAst(
*/
export const extractStaticImageProps = (
ast: babel.types.File,
filename: string,
onError?: (prop: string, nodePath: NodePath) => void
): Map<string, IStaticImageProps> => {
const images: Map<string, IStaticImageProps> = new Map()
Expand All @@ -93,6 +94,10 @@ export const extractStaticImageProps = (
nodePath as unknown as NodePath<JSXOpeningElement>,
onError
) as unknown as IStaticImageProps
// When the image props are the same for multiple StaticImage but they are in different locations
// the hash will be the same then. We need to make sure that the hash is unique.
image.filename = filename

images.set(hashOptions(image), image)
},
})
Expand Down
15 changes: 9 additions & 6 deletions packages/gatsby-plugin-image/src/node-apis/preprocess-source.ts
Expand Up @@ -2,7 +2,7 @@ import { PreprocessSourceArgs } from "gatsby"
import { babelParseToAst } from "./parser"
import path from "path"
import { extractStaticImageProps } from "./parser"
import { codeFrameColumns } from "@babel/code-frame"
import { codeFrameColumns, SourceLocation } from "@babel/code-frame"

import { writeImages } from "./image-processing"
import { getCacheDir } from "./node-utils"
Expand Down Expand Up @@ -46,16 +46,19 @@ export async function preprocessSource({
},
})

const images = extractStaticImageProps(ast, (prop, nodePath) => {
const { start, end } = nodePath.node.loc
const location = { start, end }
const images = extractStaticImageProps(ast, filename, (prop, nodePath) => {
const sourceLocation = nodePath.node.loc as SourceLocation
const { start, end } = sourceLocation
reporter.error({
id: `95314`,
filePath: filename,
location,
location: {
start,
end,
},
context: {
prop,
codeFrame: codeFrameColumns(contents, nodePath.node.loc, {
codeFrame: codeFrameColumns(contents, sourceLocation, {
linesAbove: 6,
linesBelow: 6,
highlightCode: true,
Expand Down

0 comments on commit c98d947

Please sign in to comment.