Skip to content

Commit 3b5dcda

Browse files
GatsbyJS Botascorbic
andauthored
fix(gatsby-plugin-image): Handle breakpoints and auto-format in plugin utils (#29187) (#29197)
(cherry picked from commit 25912f0) Co-authored-by: Matt Kane <[email protected]>
1 parent 4282e05 commit 3b5dcda

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

packages/gatsby-plugin-image/src/components/hooks.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
IGatsbyImageHelperArgs,
1717
generateImageData,
1818
Layout,
19+
EVERY_BREAKPOINT,
1920
} from "../image-utils"
2021
const imageCache = new Set<string>()
2122

@@ -74,10 +75,10 @@ export function getWrapperProps(
7475

7576
export function useGatsbyImage({
7677
pluginName = `useGatsbyImage`,
78+
breakpoints = EVERY_BREAKPOINT,
7779
...args
7880
}: IGatsbyImageHelperArgs): IGatsbyImageData {
79-
// TODO: use context to get default plugin options and spread them in here
80-
return generateImageData({ pluginName, ...args })
81+
return generateImageData({ pluginName, breakpoints, ...args })
8182
}
8283

8384
export function getMainProps(

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@ import { IGatsbyImageData } from "."
44
import type sharp from "gatsby-plugin-sharp/safe-sharp"
55

66
const DEFAULT_PIXEL_DENSITIES = [0.25, 0.5, 1, 2]
7-
const DEFAULT_BREAKPOINTS = [750, 1080, 1366, 1920]
7+
export const DEFAULT_BREAKPOINTS = [750, 1080, 1366, 1920]
8+
export const EVERY_BREAKPOINT = [
9+
320,
10+
654,
11+
768,
12+
1024,
13+
1366,
14+
1600,
15+
1920,
16+
2048,
17+
2560,
18+
3440,
19+
3840,
20+
4096,
21+
]
822
const DEFAULT_FLUID_WIDTH = 800
923
const DEFAULT_FIXED_WIDTH = 400
1024

@@ -228,7 +242,7 @@ export function generateImageData(
228242
})
229243
.filter(Boolean)
230244

231-
if (format === `jpg` || format === `png`) {
245+
if (format === `jpg` || format === `png` || format === `auto`) {
232246
const unscaled =
233247
images.find(img => img.width === imageSizes.unscaledWidth) || images[0]
234248

@@ -279,6 +293,7 @@ export function calculateImageSizes(args: IImageSizeArgs): IImageSizes {
279293
layout = `constrained`,
280294
sourceMetadata: imgDimensions,
281295
reporter = { warn },
296+
breakpoints = DEFAULT_BREAKPOINTS,
282297
} = args
283298

284299
// check that all dimensions provided are positive
@@ -299,7 +314,7 @@ export function calculateImageSizes(args: IImageSizeArgs): IImageSizes {
299314
} else if (layout === `constrained`) {
300315
return responsiveImageSizes(args)
301316
} else if (layout === `fullWidth`) {
302-
return responsiveImageSizes({ breakpoints: DEFAULT_BREAKPOINTS, ...args })
317+
return responsiveImageSizes({ breakpoints, ...args })
303318
} else {
304319
reporter.warn(
305320
`No valid layout was provided for the image at ${filename}. Valid image layouts are fixed, fullWidth, and constrained.`

0 commit comments

Comments
 (0)