Skip to content

Commit 25912f0

Browse files
authored
fix(gatsby-plugin-image): Handle breakpoints and auto-format in plugin utils (#29187)
1 parent 0ac0ac8 commit 25912f0

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
@@ -17,6 +17,7 @@ import {
1717
IGatsbyImageHelperArgs,
1818
generateImageData,
1919
Layout,
20+
EVERY_BREAKPOINT,
2021
} from "../image-utils"
2122
const imageCache = new Set<string>()
2223

@@ -86,10 +87,10 @@ export async function applyPolyfill(
8687

8788
export function useGatsbyImage({
8889
pluginName = `useGatsbyImage`,
90+
breakpoints = EVERY_BREAKPOINT,
8991
...args
9092
}: IGatsbyImageHelperArgs): IGatsbyImageData {
91-
// TODO: use context to get default plugin options and spread them in here
92-
return generateImageData({ pluginName, ...args })
93+
return generateImageData({ pluginName, breakpoints, ...args })
9394
}
9495

9596
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

@@ -229,7 +243,7 @@ export function generateImageData(
229243
})
230244
.filter(Boolean)
231245

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

@@ -280,6 +294,7 @@ export function calculateImageSizes(args: IImageSizeArgs): IImageSizes {
280294
layout = `constrained`,
281295
sourceMetadata: imgDimensions,
282296
reporter = { warn },
297+
breakpoints = DEFAULT_BREAKPOINTS,
283298
} = args
284299

285300
// check that all dimensions provided are positive
@@ -300,7 +315,7 @@ export function calculateImageSizes(args: IImageSizeArgs): IImageSizes {
300315
} else if (layout === `constrained`) {
301316
return responsiveImageSizes(args)
302317
} else if (layout === `fullWidth`) {
303-
return responsiveImageSizes({ breakpoints: DEFAULT_BREAKPOINTS, ...args })
318+
return responsiveImageSizes({ breakpoints, ...args })
304319
} else {
305320
reporter.warn(
306321
`No valid layout was provided for the image at ${filename}. Valid image layouts are fixed, fullWidth, and constrained.`

0 commit comments

Comments
 (0)