Skip to content

Commit

Permalink
fix(gatsby-plugin-image): Handle breakpoints and auto-format in plugi…
Browse files Browse the repository at this point in the history
…n utils (#29187) (#29197)

(cherry picked from commit 25912f0)

Co-authored-by: Matt Kane <matt@gatsbyjs.com>
  • Loading branch information
GatsbyJS Bot and ascorbic committed Jan 26, 2021
1 parent 4282e05 commit 3b5dcda
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/gatsby-plugin-image/src/components/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
IGatsbyImageHelperArgs,
generateImageData,
Layout,
EVERY_BREAKPOINT,
} from "../image-utils"
const imageCache = new Set<string>()

Expand Down Expand Up @@ -74,10 +75,10 @@ export function getWrapperProps(

export function useGatsbyImage({
pluginName = `useGatsbyImage`,
breakpoints = EVERY_BREAKPOINT,
...args
}: IGatsbyImageHelperArgs): IGatsbyImageData {
// TODO: use context to get default plugin options and spread them in here
return generateImageData({ pluginName, ...args })
return generateImageData({ pluginName, breakpoints, ...args })
}

export function getMainProps(
Expand Down
21 changes: 18 additions & 3 deletions packages/gatsby-plugin-image/src/image-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@ import { IGatsbyImageData } from "."
import type sharp from "gatsby-plugin-sharp/safe-sharp"

const DEFAULT_PIXEL_DENSITIES = [0.25, 0.5, 1, 2]
const DEFAULT_BREAKPOINTS = [750, 1080, 1366, 1920]
export const DEFAULT_BREAKPOINTS = [750, 1080, 1366, 1920]
export const EVERY_BREAKPOINT = [
320,
654,
768,
1024,
1366,
1600,
1920,
2048,
2560,
3440,
3840,
4096,
]
const DEFAULT_FLUID_WIDTH = 800
const DEFAULT_FIXED_WIDTH = 400

Expand Down Expand Up @@ -228,7 +242,7 @@ export function generateImageData(
})
.filter(Boolean)

if (format === `jpg` || format === `png`) {
if (format === `jpg` || format === `png` || format === `auto`) {
const unscaled =
images.find(img => img.width === imageSizes.unscaledWidth) || images[0]

Expand Down Expand Up @@ -279,6 +293,7 @@ export function calculateImageSizes(args: IImageSizeArgs): IImageSizes {
layout = `constrained`,
sourceMetadata: imgDimensions,
reporter = { warn },
breakpoints = DEFAULT_BREAKPOINTS,
} = args

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

0 comments on commit 3b5dcda

Please sign in to comment.