Skip to content

Commit 8340e6d

Browse files
authored
[Image Component] image sizes property becomes 100vw by default (vercel#20067)
Currently if sizes is not defined, Next.js is setting sizes as: ``` (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' ``` This pull request will make sizes be `100vw` by default, which will allow us to download "smaller" images than what's currently happening. In a demo app I have, the difference is between downloading 488KB vs 1.4MB (in images)
1 parent f4809b9 commit 8340e6d

File tree

3 files changed

+7
-13
lines changed

3 files changed

+7
-13
lines changed

docs/api-reference/next/image.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ Try it out:
9393

9494
### sizes
9595

96-
A string mapping media queries to device sizes. Defaults to [Device Sizes](/docs/basic-features/image-optimization.md#device-sizes).
96+
A string mapping media queries to device sizes. Defaults to `100vw`.
97+
98+
We recommend setting `sizes` when `layout="responsive"` and your image will not be the same width as the viewport.
9799

98100
[Learn more](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-sizes).
99101

packages/next/client/image.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ function generateImgAttrs({
151151
.join(', ')
152152

153153
if (!sizes && kind === 'w') {
154-
sizes = widths
155-
.map((w, i) => (i === last ? `${w}px` : `(max-width: ${w}px) ${w}px`))
156-
.join(', ')
154+
sizes = '100vw'
157155
}
158156

159157
src = callLoader({ src, quality, width: widths[last] })

test/integration/image-component/default/test/index.test.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,7 @@ function runTests(mode) {
222222
expect(await browser.elementById(id).getAttribute('srcset')).toBe(
223223
'/_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'
224224
)
225-
expect(await browser.elementById(id).getAttribute('sizes')).toBe(
226-
'(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'
227-
)
225+
expect(await browser.elementById(id).getAttribute('sizes')).toBe('100vw')
228226
await browser.setDimensions({
229227
width: width + delta,
230228
height: height + delta,
@@ -264,9 +262,7 @@ function runTests(mode) {
264262
expect(await browser.elementById(id).getAttribute('srcset')).toBe(
265263
'/_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'
266264
)
267-
expect(await browser.elementById(id).getAttribute('sizes')).toBe(
268-
'(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'
269-
)
265+
expect(await browser.elementById(id).getAttribute('sizes')).toBe('100vw')
270266
await browser.setDimensions({
271267
width: width + delta,
272268
height: height + delta,
@@ -306,9 +302,7 @@ function runTests(mode) {
306302
expect(await browser.elementById(id).getAttribute('srcset')).toBe(
307303
'/_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'
308304
)
309-
expect(await browser.elementById(id).getAttribute('sizes')).toBe(
310-
'(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'
311-
)
305+
expect(await browser.elementById(id).getAttribute('sizes')).toBe('100vw')
312306
expect(await getComputed(browser, id, 'width')).toBe(width)
313307
expect(await getComputed(browser, id, 'height')).toBe(height)
314308
const delta = 150

0 commit comments

Comments
 (0)