Unverified Commit 20ab3512 authored by Steven's avatar Steven Committed by GitHub

Add missing `sizes` prop on image component (#19128)

- Fixes #19120
- Closes #18413 
- Related to #19041 
parent 9174b7a3
...@@ -93,9 +93,7 @@ Try it out: ...@@ -93,9 +93,7 @@ Try it out:
### sizes ### sizes
Defines what proportion of the screen you expect the image to take up. Defaults to undefined. A string mapping media queries to device sizes. Defaults to [Device Sizes](/docs/basic-features/image-optimization.md#device-sizes).
Recommended, as it helps serve the correct sized image to each device.
[Learn more](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-sizes). [Learn more](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-sizes).
......
...@@ -72,41 +72,26 @@ const allSizes = [...configDeviceSizes, ...configImageSizes] ...@@ -72,41 +72,26 @@ const allSizes = [...configDeviceSizes, ...configImageSizes]
configDeviceSizes.sort((a, b) => a - b) configDeviceSizes.sort((a, b) => a - b)
allSizes.sort((a, b) => a - b) allSizes.sort((a, b) => a - b)
function getSizes( function getWidths(
width: number | undefined, width: number | undefined,
layout: LayoutValue layout: LayoutValue
): { sizes: number[]; kind: 'w' | 'x' } { ): { widths: number[]; kind: 'w' | 'x' } {
if ( if (
typeof width !== 'number' || typeof width !== 'number' ||
layout === 'fill' || layout === 'fill' ||
layout === 'responsive' layout === 'responsive'
) { ) {
return { sizes: configDeviceSizes, kind: 'w' } return { widths: configDeviceSizes, kind: 'w' }
} }
const sizes = [ const widths = [
...new Set( ...new Set(
[width, width * 2, width * 3].map( [width, width * 2, width * 3].map(
(w) => allSizes.find((p) => p >= w) || allSizes[allSizes.length - 1] (w) => allSizes.find((p) => p >= w) || allSizes[allSizes.length - 1]
) )
), ),
] ]
return { sizes, kind: 'x' } return { widths, kind: 'x' }
}
function computeSrc(
src: string,
unoptimized: boolean,
layout: LayoutValue,
width?: number,
quality?: number
): string {
if (unoptimized) {
return src
}
const { sizes } = getSizes(width, layout)
const largest = sizes[sizes.length - 1]
return callLoader({ src, width: largest, quality })
} }
type CallLoaderProps = { type CallLoaderProps = {
...@@ -127,36 +112,53 @@ function callLoader(loaderProps: CallLoaderProps) { ...@@ -127,36 +112,53 @@ function callLoader(loaderProps: CallLoaderProps) {
) )
} }
type SrcSetData = { type GenImgAttrsData = {
src: string src: string
unoptimized: boolean unoptimized: boolean
layout: LayoutValue layout: LayoutValue
width?: number width?: number
quality?: number quality?: number
sizes?: string
} }
function generateSrcSet({ type GenImgAttrsResult = Pick<
JSX.IntrinsicElements['img'],
'src' | 'sizes' | 'srcSet'
>
function generateImgAttrs({
src, src,
unoptimized, unoptimized,
layout, layout,
width, width,
quality, quality,
}: SrcSetData): string | undefined { sizes,
// At each breakpoint, generate an image url using the loader, such as: }: GenImgAttrsData): GenImgAttrsResult {
// ' www.example.com/foo.jpg?w=480 480w, '
if (unoptimized) { if (unoptimized) {
return undefined return { src }
} }
const { sizes, kind } = getSizes(width, layout) const { widths, kind } = getWidths(width, layout)
return sizes const last = widths.length - 1
const srcSet = widths
.map( .map(
(size, i) => (w, i) =>
`${callLoader({ src, width: size, quality })} ${ `${callLoader({ src, quality, width: w })} ${
kind === 'w' ? size : i + 1 kind === 'w' ? w : i + 1
}${kind}` }${kind}`
) )
.join(', ') .join(', ')
if (!sizes && kind === 'w') {
sizes = widths
.map((w, i) => (i === last ? `${w}px` : `(max-width: ${w}px) ${w}px`))
.join(', ')
}
src = callLoader({ src, quality, width: widths[last] })
return { src, sizes, srcSet }
} }
function getInt(x: unknown): number | undefined { function getInt(x: unknown): number | undefined {
...@@ -352,24 +354,20 @@ export default function Image({ ...@@ -352,24 +354,20 @@ export default function Image({
} }
} }
// Generate attribute values let imgAttributes: GenImgAttrsResult = {
const imgSrc = computeSrc(src, unoptimized, layout, widthInt, qualityInt)
const imgSrcSet = generateSrcSet({
src,
unoptimized,
layout,
width: widthInt,
quality: qualityInt,
})
const imgAttributes: Pick<JSX.IntrinsicElements['img'], 'src' | 'srcSet'> = {
src: src:
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
} }
if (isVisible) { if (isVisible) {
imgAttributes.src = imgSrc imgAttributes = generateImgAttrs({
imgAttributes.srcSet = imgSrcSet src,
unoptimized,
layout,
width: widthInt,
quality: qualityInt,
sizes,
})
} }
if (unsized) { if (unsized) {
...@@ -397,7 +395,6 @@ export default function Image({ ...@@ -397,7 +395,6 @@ export default function Image({
{...imgAttributes} {...imgAttributes}
decoding="async" decoding="async"
className={className} className={className}
sizes={sizes}
ref={setRef} ref={setRef}
style={imgStyle} style={imgStyle}
/> />
......
import React from 'react'
import Image from 'next/image'
const Page = () => {
return (
<div>
<p>Assign sizes prop</p>
<Image
id="sizes1"
src="/wide.png"
width="1200"
height="700"
sizes="(max-width: 2048px) 1200px, 3840px"
/>
<p>Assign sizes prop</p>
</div>
)
}
export default Page
...@@ -148,6 +148,7 @@ function runTests(mode) { ...@@ -148,6 +148,7 @@ function runTests(mode) {
expect(await browser.elementById(id).getAttribute('srcset')).toBe( expect(await browser.elementById(id).getAttribute('srcset')).toBe(
'/_next/image?url=%2Fwide.png&w=1200&q=75 1x, /_next/image?url=%2Fwide.png&w=3840&q=75 2x' '/_next/image?url=%2Fwide.png&w=1200&q=75 1x, /_next/image?url=%2Fwide.png&w=3840&q=75 2x'
) )
expect(await browser.elementById(id).getAttribute('sizes')).toBeFalsy()
await browser.setDimensions({ await browser.setDimensions({
width: width + delta, width: width + delta,
height: height + delta, height: height + delta,
...@@ -181,6 +182,7 @@ function runTests(mode) { ...@@ -181,6 +182,7 @@ function runTests(mode) {
expect(await browser.elementById(id).getAttribute('srcset')).toBe( expect(await browser.elementById(id).getAttribute('srcset')).toBe(
'/_next/image?url=%2Fwide.png&w=1200&q=75 1x, /_next/image?url=%2Fwide.png&w=3840&q=75 2x' '/_next/image?url=%2Fwide.png&w=1200&q=75 1x, /_next/image?url=%2Fwide.png&w=3840&q=75 2x'
) )
expect(await browser.elementById(id).getAttribute('sizes')).toBeFalsy()
await browser.setDimensions({ await browser.setDimensions({
width: width + delta, width: width + delta,
height: height + delta, height: height + delta,
...@@ -220,6 +222,9 @@ function runTests(mode) { ...@@ -220,6 +222,9 @@ function runTests(mode) {
expect(await browser.elementById(id).getAttribute('srcset')).toBe( expect(await browser.elementById(id).getAttribute('srcset')).toBe(
'/_next/image?url=%2Fwide.png&w=640&q=75 640w, /_next/image?url=%2Fwide.png&w=750&q=75 750w, /_next/image?url=%2Fwide.png&w=828&q=75 828w, /_next/image?url=%2Fwide.png&w=1080&q=75 1080w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w, /_next/image?url=%2Fwide.png&w=1920&q=75 1920w, /_next/image?url=%2Fwide.png&w=2048&q=75 2048w, /_next/image?url=%2Fwide.png&w=3840&q=75 3840w' '/_next/image?url=%2Fwide.png&w=640&q=75 640w, /_next/image?url=%2Fwide.png&w=750&q=75 750w, /_next/image?url=%2Fwide.png&w=828&q=75 828w, /_next/image?url=%2Fwide.png&w=1080&q=75 1080w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w, /_next/image?url=%2Fwide.png&w=1920&q=75 1920w, /_next/image?url=%2Fwide.png&w=2048&q=75 2048w, /_next/image?url=%2Fwide.png&w=3840&q=75 3840w'
) )
expect(await browser.elementById(id).getAttribute('sizes')).toBe(
'(max-width: 640px) 640px, (max-width: 750px) 750px, (max-width: 828px) 828px, (max-width: 1080px) 1080px, (max-width: 1200px) 1200px, (max-width: 1920px) 1920px, (max-width: 2048px) 2048px, 3840px'
)
await browser.setDimensions({ await browser.setDimensions({
width: width + delta, width: width + delta,
height: height + delta, height: height + delta,
...@@ -259,6 +264,9 @@ function runTests(mode) { ...@@ -259,6 +264,9 @@ function runTests(mode) {
expect(await browser.elementById(id).getAttribute('srcset')).toBe( expect(await browser.elementById(id).getAttribute('srcset')).toBe(
'/_next/image?url=%2Fwide.png&w=640&q=75 640w, /_next/image?url=%2Fwide.png&w=750&q=75 750w, /_next/image?url=%2Fwide.png&w=828&q=75 828w, /_next/image?url=%2Fwide.png&w=1080&q=75 1080w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w, /_next/image?url=%2Fwide.png&w=1920&q=75 1920w, /_next/image?url=%2Fwide.png&w=2048&q=75 2048w, /_next/image?url=%2Fwide.png&w=3840&q=75 3840w' '/_next/image?url=%2Fwide.png&w=640&q=75 640w, /_next/image?url=%2Fwide.png&w=750&q=75 750w, /_next/image?url=%2Fwide.png&w=828&q=75 828w, /_next/image?url=%2Fwide.png&w=1080&q=75 1080w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w, /_next/image?url=%2Fwide.png&w=1920&q=75 1920w, /_next/image?url=%2Fwide.png&w=2048&q=75 2048w, /_next/image?url=%2Fwide.png&w=3840&q=75 3840w'
) )
expect(await browser.elementById(id).getAttribute('sizes')).toBe(
'(max-width: 640px) 640px, (max-width: 750px) 750px, (max-width: 828px) 828px, (max-width: 1080px) 1080px, (max-width: 1200px) 1200px, (max-width: 1920px) 1920px, (max-width: 2048px) 2048px, 3840px'
)
await browser.setDimensions({ await browser.setDimensions({
width: width + delta, width: width + delta,
height: height + delta, height: height + delta,
...@@ -298,6 +306,9 @@ function runTests(mode) { ...@@ -298,6 +306,9 @@ function runTests(mode) {
expect(await browser.elementById(id).getAttribute('srcset')).toBe( expect(await browser.elementById(id).getAttribute('srcset')).toBe(
'/_next/image?url=%2Fwide.png&w=640&q=75 640w, /_next/image?url=%2Fwide.png&w=750&q=75 750w, /_next/image?url=%2Fwide.png&w=828&q=75 828w, /_next/image?url=%2Fwide.png&w=1080&q=75 1080w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w, /_next/image?url=%2Fwide.png&w=1920&q=75 1920w, /_next/image?url=%2Fwide.png&w=2048&q=75 2048w, /_next/image?url=%2Fwide.png&w=3840&q=75 3840w' '/_next/image?url=%2Fwide.png&w=640&q=75 640w, /_next/image?url=%2Fwide.png&w=750&q=75 750w, /_next/image?url=%2Fwide.png&w=828&q=75 828w, /_next/image?url=%2Fwide.png&w=1080&q=75 1080w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w, /_next/image?url=%2Fwide.png&w=1920&q=75 1920w, /_next/image?url=%2Fwide.png&w=2048&q=75 2048w, /_next/image?url=%2Fwide.png&w=3840&q=75 3840w'
) )
expect(await browser.elementById(id).getAttribute('sizes')).toBe(
'(max-width: 640px) 640px, (max-width: 750px) 750px, (max-width: 828px) 828px, (max-width: 1080px) 1080px, (max-width: 1200px) 1200px, (max-width: 1920px) 1920px, (max-width: 2048px) 2048px, 3840px'
)
expect(await getComputed(browser, id, 'width')).toBe(width) expect(await getComputed(browser, id, 'width')).toBe(width)
expect(await getComputed(browser, id, 'height')).toBe(height) expect(await getComputed(browser, id, 'height')).toBe(height)
const delta = 150 const delta = 150
...@@ -333,6 +344,48 @@ function runTests(mode) { ...@@ -333,6 +344,48 @@ function runTests(mode) {
} }
}) })
it('should work with sizes and automatically use layout-responsive', async () => {
let browser
try {
browser = await webdriver(appPort, '/sizes')
const width = 1200
const height = 700
const delta = 250
const id = 'sizes1'
expect(await getSrc(browser, id)).toBe(
'/_next/image?url=%2Fwide.png&w=3840&q=75'
)
expect(await browser.elementById(id).getAttribute('srcset')).toBe(
'/_next/image?url=%2Fwide.png&w=640&q=75 640w, /_next/image?url=%2Fwide.png&w=750&q=75 750w, /_next/image?url=%2Fwide.png&w=828&q=75 828w, /_next/image?url=%2Fwide.png&w=1080&q=75 1080w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w, /_next/image?url=%2Fwide.png&w=1920&q=75 1920w, /_next/image?url=%2Fwide.png&w=2048&q=75 2048w, /_next/image?url=%2Fwide.png&w=3840&q=75 3840w'
)
expect(await browser.elementById(id).getAttribute('sizes')).toBe(
'(max-width: 2048px) 1200px, 3840px'
)
await browser.setDimensions({
width: width + delta,
height: height + delta,
})
expect(await getComputed(browser, id, 'width')).toBeGreaterThan(width)
expect(await getComputed(browser, id, 'height')).toBeGreaterThan(height)
await browser.setDimensions({
width: width - delta,
height: height - delta,
})
const newWidth = await getComputed(browser, id, 'width')
const newHeight = await getComputed(browser, id, 'height')
expect(newWidth).toBeLessThan(width)
expect(newHeight).toBeLessThan(height)
expect(getRatio(newWidth, newHeight)).toBeCloseTo(
getRatio(width, height),
1
)
} finally {
if (browser) {
await browser.close()
}
}
})
if (mode === 'dev') { if (mode === 'dev') {
it('should show missing src error', async () => { it('should show missing src error', async () => {
const browser = await webdriver(appPort, '/missing-src') const browser = await webdriver(appPort, '/missing-src')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment