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:
### sizes
Defines what proportion of the screen you expect the image to take up. Defaults to undefined.
Recommended, as it helps serve the correct sized image to each device.
A string mapping media queries to device sizes. Defaults to [Device Sizes](/docs/basic-features/image-optimization.md#device-sizes).
[Learn more](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-sizes).
......
......@@ -72,41 +72,26 @@ const allSizes = [...configDeviceSizes, ...configImageSizes]
configDeviceSizes.sort((a, b) => a - b)
allSizes.sort((a, b) => a - b)
function getSizes(
function getWidths(
width: number | undefined,
layout: LayoutValue
): { sizes: number[]; kind: 'w' | 'x' } {
): { widths: number[]; kind: 'w' | 'x' } {
if (
typeof width !== 'number' ||
layout === 'fill' ||
layout === 'responsive'
) {
return { sizes: configDeviceSizes, kind: 'w' }
return { widths: configDeviceSizes, kind: 'w' }
}
const sizes = [
const widths = [
...new Set(
[width, width * 2, width * 3].map(
(w) => allSizes.find((p) => p >= w) || allSizes[allSizes.length - 1]
)
),
]
return { sizes, 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 })
return { widths, kind: 'x' }
}
type CallLoaderProps = {
......@@ -127,36 +112,53 @@ function callLoader(loaderProps: CallLoaderProps) {
)
}
type SrcSetData = {
type GenImgAttrsData = {
src: string
unoptimized: boolean
layout: LayoutValue
width?: number
quality?: number
sizes?: string
}
function generateSrcSet({
type GenImgAttrsResult = Pick<
JSX.IntrinsicElements['img'],
'src' | 'sizes' | 'srcSet'
>
function generateImgAttrs({
src,
unoptimized,
layout,
width,
quality,
}: SrcSetData): string | undefined {
// At each breakpoint, generate an image url using the loader, such as:
// ' www.example.com/foo.jpg?w=480 480w, '
sizes,
}: GenImgAttrsData): GenImgAttrsResult {
if (unoptimized) {
return undefined
return { src }
}
const { sizes, kind } = getSizes(width, layout)
return sizes
const { widths, kind } = getWidths(width, layout)
const last = widths.length - 1
const srcSet = widths
.map(
(size, i) =>
`${callLoader({ src, width: size, quality })} ${
kind === 'w' ? size : i + 1
(w, i) =>
`${callLoader({ src, quality, width: w })} ${
kind === 'w' ? w : i + 1
}${kind}`
)
.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 {
......@@ -352,24 +354,20 @@ export default function Image({
}
}
// Generate attribute values
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'> = {
let imgAttributes: GenImgAttrsResult = {
src:
'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7',
}
if (isVisible) {
imgAttributes.src = imgSrc
imgAttributes.srcSet = imgSrcSet
imgAttributes = generateImgAttrs({
src,
unoptimized,
layout,
width: widthInt,
quality: qualityInt,
sizes,
})
}
if (unsized) {
......@@ -397,7 +395,6 @@ export default function Image({
{...imgAttributes}
decoding="async"
className={className}
sizes={sizes}
ref={setRef}
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) {
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'
)
expect(await browser.elementById(id).getAttribute('sizes')).toBeFalsy()
await browser.setDimensions({
width: width + delta,
height: height + delta,
......@@ -181,6 +182,7 @@ function runTests(mode) {
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'
)
expect(await browser.elementById(id).getAttribute('sizes')).toBeFalsy()
await browser.setDimensions({
width: width + delta,
height: height + delta,
......@@ -220,6 +222,9 @@ function runTests(mode) {
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: 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({
width: width + delta,
height: height + delta,
......@@ -259,6 +264,9 @@ function runTests(mode) {
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: 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({
width: width + delta,
height: height + delta,
......@@ -298,6 +306,9 @@ function runTests(mode) {
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: 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, 'height')).toBe(height)
const delta = 150
......@@ -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') {
it('should show missing src error', async () => {
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