Skip to content

Commit 4c44125

Browse files
GatsbyJS Botascorbic
andauthored
fix(gatsby-plugin-image): Handle placeholder in plugin toolkit (#30141) (#30170)
* fix(gatsby-plugin-image): Handle placeholder in plugin toolkit * Add test (cherry picked from commit f851312) Co-authored-by: Matt Kane <matt@gatsbyjs.com>
1 parent 4fd16c5 commit 4c44125

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

packages/gatsby-plugin-image/src/__tests__/image-utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ https://example.com/afile.jpg/1920/1440/image.webp 1920w`)
225225
expect(data.images?.fallback).toBeUndefined()
226226
})
227227

228+
it(`includes a placeholder image if a URL is provided`, () => {
229+
const data = generateImageData({
230+
...args,
231+
placeholderURL: `data:image/jpg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD`,
232+
})
233+
expect(data.placeholder?.fallback).toEqual(
234+
`data:image/jpg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD`
235+
)
236+
})
237+
228238
it(`generates the same output as the input format if output is auto`, () => {
229239
const sourceMetadata = {
230240
width: 800,

packages/gatsby-plugin-image/src/image-utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ export interface IGatsbyImageHelperArgs {
9898
layout?: Layout
9999
formats?: Array<ImageFormat>
100100
filename: string
101-
placeholderURL?:
102-
| ((args: IGatsbyImageHelperArgs) => string | undefined)
103-
| string
101+
placeholderURL?: string
104102
width?: number
105103
height?: number
106104
sizes?: string
@@ -238,6 +236,7 @@ export function generateImageData(
238236
filename,
239237
reporter = { warn },
240238
backgroundColor,
239+
placeholderURL,
241240
} = args
242241

243242
if (!pluginName) {
@@ -342,6 +341,11 @@ export function generateImageData(
342341
layout,
343342
backgroundColor,
344343
}
344+
345+
if (placeholderURL) {
346+
imageProps.placeholder = { fallback: placeholderURL }
347+
}
348+
345349
switch (layout) {
346350
case `fixed`:
347351
imageProps.width = imageSizes.presentationWidth

0 commit comments

Comments
 (0)