Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-plugin-sharp): presentation sizes #23905

Merged
merged 12 commits into from
May 19, 2020
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-sharp/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ describe(`gatsby-plugin-sharp`, () => {
})

expect(result.presentationWidth).toEqual(20)
expect(result.presentationHeight).toEqual(10)
expect(result.presentationHeight).toEqual(20)
pieh marked this conversation as resolved.
Show resolved Hide resolved
expect(boundActionCreators.createJobV2).toMatchSnapshot()
})

Expand Down
35 changes: 20 additions & 15 deletions packages/gatsby-plugin-sharp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,20 +474,6 @@ async function fluid({ file, args = {}, reporter, cache }) {
)
}

let presentationWidth, presentationHeight
if (fixedDimension === `maxWidth`) {
presentationWidth = Math.min(options.maxWidth, width)
presentationHeight = Math.round(presentationWidth * (height / width))
} else {
presentationHeight = Math.min(options.maxHeight, height)
presentationWidth = Math.round(presentationHeight * (width / height))
}

// If the users didn't set default sizes, we'll make one.
if (!options.sizes) {
options.sizes = `(max-width: ${presentationWidth}px) 100vw, ${presentationWidth}px`
}

// Create sizes (in width) for the image if no custom breakpoints are
// provided. If the max width of the container for the rendered markdown file
// is 800px, the sizes would then be: 200, 400, 800, 1200, 1600.
Expand Down Expand Up @@ -588,6 +574,7 @@ async function fluid({ file, args = {}, reporter, cache }) {
const fallbackSrc = _.minBy(images, image =>
Math.abs(options[fixedDimension] - image[dimensionAttr])
).src

const srcSet = images
.map(image => `${image.src} ${Math.round(image.width)}w`)
.join(`,\n`)
Expand All @@ -614,13 +601,31 @@ async function fluid({ file, args = {}, reporter, cache }) {
}
}

// calculate presentationSizes
const maxWidth = options.maxWidth
? Math.min(options.maxWidth, width)
: undefined
const maxHeight = options.maxHeight
? Math.min(options.maxHeight, height)
: undefined
const imageWithDensity1 = images.find(
image => maxWidth === image.width || maxHeight === image.height
)
const presentationWidth = imageWithDensity1.width
const presentationHeight = imageWithDensity1.height

// If the users didn't set default sizes, we'll make one.
const sizes =
options.sizes ??
`(max-width: ${presentationWidth}px) 100vw, ${presentationHeight}px`
wardpeet marked this conversation as resolved.
Show resolved Hide resolved

return {
base64: base64Image && base64Image.src,
aspectRatio: images[0].aspectRatio,
src: fallbackSrc,
srcSet,
srcSetType,
sizes: options.sizes,
sizes,
originalImg,
originalName,
density,
Expand Down