Unverified Commit 2b94b1ee authored by Steven's avatar Steven Committed by GitHub

Update default widths configuration to handle 2x/3x DPI (#18717)

- Update default `deviceSizes`
- Add default `imageSizes`
- Use `layout` value to determine which `srcset` to use

Fixes #18420 
Closes #18714
parent 3f84a55b
...@@ -46,8 +46,8 @@ export default Home ...@@ -46,8 +46,8 @@ export default Home
`Image` accepts the following props: `Image` accepts the following props:
- `src` - The path or URL to the source image. This is required. - `src` - The path or URL to the source image. This is required.
- `width` - The intrinsic width of the source image in pixels. Must be an integer without a unit. Required unless `layout="fill"`. - `width` - The width of the image, in pixels. Must be an integer without a unit. Required unless `layout="fill"`.
- `height` - The intrinsic height of the source image, in pixels. Must be an integer without a unit. Required unless `layout="fill"`. - `height` - The height of the image, in pixels. Must be an integer without a unit. Required unless `layout="fill"`.
- `layout` - The rendered layout of the image. If `fixed`, the image dimensions will not change as the viewport changes (no responsiveness). If `intrinsic`, the image will scale the dimensions down for smaller viewports but maintain the original dimensions for larger viewports. If `responsive`, the image will scale the dimensions down for smaller viewports and scale up for larger viewports. If `fill`, the image will stretch both width and height to the dimensions of the parent element. Default `intrinsic`. - `layout` - The rendered layout of the image. If `fixed`, the image dimensions will not change as the viewport changes (no responsiveness). If `intrinsic`, the image will scale the dimensions down for smaller viewports but maintain the original dimensions for larger viewports. If `responsive`, the image will scale the dimensions down for smaller viewports and scale up for larger viewports. If `fill`, the image will stretch both width and height to the dimensions of the parent element. Default `intrinsic`.
- `sizes` - Defines what proportion of the screen you expect the image to take up. Recommended, as it helps serve the correct sized image to each device. [More info](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-sizes). - `sizes` - Defines what proportion of the screen you expect the image to take up. Recommended, as it helps serve the correct sized image to each device. [More info](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-sizes).
- `quality` - The quality of the optimized image, an integer between 1 and 100 where 100 is the best quality. Default 75. - `quality` - The quality of the optimized image, an integer between 1 and 100 where 100 is the best quality. Default 75.
......
...@@ -62,8 +62,8 @@ If no configuration is provided, the following default configuration will be use ...@@ -62,8 +62,8 @@ If no configuration is provided, the following default configuration will be use
```js ```js
module.exports = { module.exports = {
images: { images: {
deviceSizes: [320, 420, 768, 1024, 1200], deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
imageSizes: [], imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
domains: [], domains: [],
path: '/_next/image', path: '/_next/image',
loader: 'default', loader: 'default',
...@@ -77,24 +77,24 @@ This means you only need to configure the properties you wish to change. ...@@ -77,24 +77,24 @@ This means you only need to configure the properties you wish to change.
### Device Sizes ### Device Sizes
You can specify a list of device width breakpoints using the `deviceSizes` property. Since images maintain their aspect ratio using the `width` and `height` attributes of the source image, there is no need to specify height in `next.config.js` – only the width. These values will be used by the browser to determine which size image should load. You can specify a list of device width breakpoints using the `deviceSizes` property. These widths are used when the [`next/image`](/docs/api-reference/next/image.md) component uses `layout="responsive"` or `layout="fill"` so that the correct image is served for the device visiting your website.
```js ```js
module.exports = { module.exports = {
images: { images: {
deviceSizes: [320, 420, 768, 1024, 1200], deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
}, },
} }
``` ```
### Image Sizes ### Image Sizes
You can specify a list of exact image widths using the `imageSizes` property. These widths should be different than the widths defined in `deviceSizes`. The purpose is for images that don't scale with the browser window, such as icons, badges, or profile images. If the `width` property of a [`next/image`](/docs/api-reference/next/image.md) component matches a value in `imageSizes`, the image will be rendered at that exact width. You can specify a list of image widths using the `imageSizes` property. These widths should be different than the widths defined in `deviceSizes` because the arrays will be concatentated. These widths are used when the [`next/image`](/docs/api-reference/next/image.md) component uses `layout="fixed"` or `layout="intrinsic"`.
```js ```js
module.exports = { module.exports = {
images: { images: {
imageSizes: [16, 32, 64], imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
}, },
} }
``` ```
...@@ -143,7 +143,7 @@ The expiration (or rather Max Age) is defined by the upstream server's `Cache-Co ...@@ -143,7 +143,7 @@ The expiration (or rather Max Age) is defined by the upstream server's `Cache-Co
If `s-maxage` is found in `Cache-Control`, it is used. If no `s-maxage` is found, then `max-age` is used. If no `max-age` is found, then 60 seconds is used. If `s-maxage` is found in `Cache-Control`, it is used. If no `s-maxage` is found, then `max-age` is used. If no `max-age` is found, then 60 seconds is used.
You can configure [`deviceSizes`](#device-sizes) to reduce the total number of possible generated images. You can configure [`deviceSizes`](#device-sizes) and [`imageSizes`](#device-sizes) to reduce the total number of possible generated images.
## Related ## Related
......
...@@ -12,9 +12,9 @@ Make sure your `images` field follows the allowed config shape and values: ...@@ -12,9 +12,9 @@ Make sure your `images` field follows the allowed config shape and values:
module.exports = { module.exports = {
images: { images: {
// limit of 25 deviceSizes values // limit of 25 deviceSizes values
deviceSizes: [320, 420, 768, 1024, 1200], deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
// limit of 25 imageSizes values // limit of 25 imageSizes values
imageSizes: [], imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
// limit of 50 domains values // limit of 50 domains values
domains: [], domains: [],
path: '/_next/image', path: '/_next/image',
......
...@@ -63,8 +63,9 @@ const { ...@@ -63,8 +63,9 @@ const {
domains: configDomains, domains: configDomains,
} = imageData } = imageData
// sort smallest to largest // sort smallest to largest
const allSizes = [...configDeviceSizes, ...configImageSizes]
configDeviceSizes.sort((a, b) => a - b) configDeviceSizes.sort((a, b) => a - b)
configImageSizes.sort((a, b) => a - b) allSizes.sort((a, b) => a - b)
let cachedObserver: IntersectionObserver let cachedObserver: IntersectionObserver
...@@ -105,28 +106,26 @@ function unLazifyImage(lazyImage: HTMLImageElement): void { ...@@ -105,28 +106,26 @@ function unLazifyImage(lazyImage: HTMLImageElement): void {
lazyImage.classList.remove('__lazy') lazyImage.classList.remove('__lazy')
} }
function getDeviceSizes( function getSizes(
width: number | undefined, width: number | undefined,
layout: LayoutValue layout: LayoutValue
): number[] { ): { sizes: number[]; kind: 'w' | 'x' } {
if ( if (
typeof width !== 'number' || typeof width !== 'number' ||
layout === 'fill' || layout === 'fill' ||
layout === 'responsive' layout === 'responsive'
) { ) {
return configDeviceSizes return { sizes: configDeviceSizes, kind: 'w' }
} }
if (configImageSizes.includes(width)) {
return [width] const sizes = [
} ...new Set(
const widths: number[] = [] [width, width * 2, width * 3].map(
for (let size of configDeviceSizes) { (w) => allSizes.find((p) => p >= w) || allSizes[allSizes.length - 1]
widths.push(size) )
if (size >= width) { ),
break ]
} return { sizes, kind: 'x' }
}
return widths
} }
function computeSrc( function computeSrc(
...@@ -139,8 +138,8 @@ function computeSrc( ...@@ -139,8 +138,8 @@ function computeSrc(
if (unoptimized) { if (unoptimized) {
return src return src
} }
const widths = getDeviceSizes(width, layout) const { sizes } = getSizes(width, layout)
const largest = widths[widths.length - 1] const largest = sizes[sizes.length - 1]
return callLoader({ src, width: largest, quality }) return callLoader({ src, width: largest, quality })
} }
...@@ -176,8 +175,14 @@ function generateSrcSet({ ...@@ -176,8 +175,14 @@ function generateSrcSet({
return undefined return undefined
} }
return getDeviceSizes(width, layout) const { sizes, kind } = getSizes(width, layout)
.map((w) => `${callLoader({ src, width: w, quality })} ${w}w`) return sizes
.map(
(size, i) =>
`${callLoader({ src, width: size, quality })} ${
kind === 'w' ? size : i + 1
}${kind}`
)
.join(', ') .join(', ')
} }
......
...@@ -25,8 +25,8 @@ const defaultConfig: { [key: string]: any } = { ...@@ -25,8 +25,8 @@ const defaultConfig: { [key: string]: any } = {
compress: true, compress: true,
analyticsId: process.env.VERCEL_ANALYTICS_ID || '', analyticsId: process.env.VERCEL_ANALYTICS_ID || '',
images: { images: {
deviceSizes: [320, 420, 768, 1024, 1200], deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
imageSizes: [], imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
domains: [], domains: [],
path: '/_next/image', path: '/_next/image',
loader: 'default', loader: 'default',
......
module.exports = { module.exports = {
images: { images: {
deviceSizes: [480, 1024, 1600, 2000], deviceSizes: [480, 1024, 1600, 2000],
imageSizes: [16, 64], imageSizes: [16, 32, 48, 64],
path: 'https://example.com/myaccount/', path: 'https://example.com/myaccount/',
loader: 'imgix', loader: 'imgix',
}, },
......
...@@ -54,11 +54,11 @@ const ClientSide = () => { ...@@ -54,11 +54,11 @@ const ClientSide = () => {
height={400} height={400}
/> />
<Image <Image
id="icon-image-64" id="icon-image-32"
src="/icon.png" src="/icon.png"
loading="eager" loading="eager"
width={64} width={32}
height={64} height={32}
/> />
<Image <Image
id="icon-image-16" id="icon-image-16"
......
...@@ -71,11 +71,11 @@ const Page = () => { ...@@ -71,11 +71,11 @@ const Page = () => {
height={400} height={400}
/> />
<Image <Image
id="icon-image-64" id="icon-image-32"
src="/icon.png" src="/icon.png"
loading="eager" loading="eager"
width={64} width={32}
height={64} height={32}
/> />
<Image <Image
id="icon-image-16" id="icon-image-16"
......
...@@ -33,46 +33,46 @@ function runTests() { ...@@ -33,46 +33,46 @@ function runTests() {
}) })
it('should modify src with the loader', async () => { it('should modify src with the loader', async () => {
expect(await browser.elementById('basic-image').getAttribute('src')).toBe( expect(await browser.elementById('basic-image').getAttribute('src')).toBe(
'https://example.com/myaccount/foo.jpg?auto=format&fit=max&w=480&q=60' 'https://example.com/myaccount/foo.jpg?auto=format&fit=max&w=1024&q=60'
) )
}) })
it('should correctly generate src even if preceding slash is included in prop', async () => { it('should correctly generate src even if preceding slash is included in prop', async () => {
expect( expect(
await browser.elementById('preceding-slash-image').getAttribute('src') await browser.elementById('preceding-slash-image').getAttribute('src')
).toBe( ).toBe(
'https://example.com/myaccount/fooslash.jpg?auto=format&fit=max&w=480' 'https://example.com/myaccount/fooslash.jpg?auto=format&fit=max&w=1024'
) )
}) })
it('should add a srcset based on the loader', async () => { it('should add a srcset based on the loader', async () => {
expect( expect(
await browser.elementById('basic-image').getAttribute('srcset') await browser.elementById('basic-image').getAttribute('srcset')
).toBe( ).toBe(
'https://example.com/myaccount/foo.jpg?auto=format&fit=max&w=480&q=60 480w' 'https://example.com/myaccount/foo.jpg?auto=format&fit=max&w=480&q=60 1x, https://example.com/myaccount/foo.jpg?auto=format&fit=max&w=1024&q=60 2x'
) )
}) })
it('should add a srcset even with preceding slash in prop', async () => { it('should add a srcset even with preceding slash in prop', async () => {
expect( expect(
await browser.elementById('preceding-slash-image').getAttribute('srcset') await browser.elementById('preceding-slash-image').getAttribute('srcset')
).toBe( ).toBe(
'https://example.com/myaccount/fooslash.jpg?auto=format&fit=max&w=480 480w' 'https://example.com/myaccount/fooslash.jpg?auto=format&fit=max&w=480 1x, https://example.com/myaccount/fooslash.jpg?auto=format&fit=max&w=1024 2x'
) )
}) })
it('should use imageSizes when width matches, not deviceSizes from next.config.js', async () => { it('should use imageSizes when width matches, not deviceSizes from next.config.js', async () => {
expect(await browser.elementById('icon-image-16').getAttribute('src')).toBe( expect(await browser.elementById('icon-image-16').getAttribute('src')).toBe(
'https://example.com/myaccount/icon.png?auto=format&fit=max&w=16' 'https://example.com/myaccount/icon.png?auto=format&fit=max&w=48'
) )
expect( expect(
await browser.elementById('icon-image-16').getAttribute('srcset') await browser.elementById('icon-image-16').getAttribute('srcset')
).toBe( ).toBe(
'https://example.com/myaccount/icon.png?auto=format&fit=max&w=16 16w' 'https://example.com/myaccount/icon.png?auto=format&fit=max&w=16 1x, https://example.com/myaccount/icon.png?auto=format&fit=max&w=32 2x, https://example.com/myaccount/icon.png?auto=format&fit=max&w=48 3x'
) )
expect(await browser.elementById('icon-image-64').getAttribute('src')).toBe( expect(await browser.elementById('icon-image-32').getAttribute('src')).toBe(
'https://example.com/myaccount/icon.png?auto=format&fit=max&w=64' 'https://example.com/myaccount/icon.png?auto=format&fit=max&w=480'
) )
expect( expect(
await browser.elementById('icon-image-64').getAttribute('srcset') await browser.elementById('icon-image-32').getAttribute('srcset')
).toBe( ).toBe(
'https://example.com/myaccount/icon.png?auto=format&fit=max&w=64 64w' 'https://example.com/myaccount/icon.png?auto=format&fit=max&w=32 1x, https://example.com/myaccount/icon.png?auto=format&fit=max&w=64 2x, https://example.com/myaccount/icon.png?auto=format&fit=max&w=480 3x'
) )
}) })
it('should support the unoptimized attribute', async () => { it('should support the unoptimized attribute', async () => {
...@@ -90,10 +90,10 @@ function runTests() { ...@@ -90,10 +90,10 @@ function runTests() {
function lazyLoadingTests() { function lazyLoadingTests() {
it('should have loaded the first image immediately', async () => { it('should have loaded the first image immediately', async () => {
expect(await browser.elementById('lazy-top').getAttribute('src')).toBe( expect(await browser.elementById('lazy-top').getAttribute('src')).toBe(
'https://example.com/myaccount/foo1.jpg?auto=format&fit=max&w=1024' 'https://example.com/myaccount/foo1.jpg?auto=format&fit=max&w=2000'
) )
expect(await browser.elementById('lazy-top').getAttribute('srcset')).toBe( expect(await browser.elementById('lazy-top').getAttribute('srcset')).toBe(
'https://example.com/myaccount/foo1.jpg?auto=format&fit=max&w=480 480w, https://example.com/myaccount/foo1.jpg?auto=format&fit=max&w=1024 1024w' 'https://example.com/myaccount/foo1.jpg?auto=format&fit=max&w=1024 1x, https://example.com/myaccount/foo1.jpg?auto=format&fit=max&w=2000 2x'
) )
}) })
it('should not have loaded the second image immediately', async () => { it('should not have loaded the second image immediately', async () => {
...@@ -121,11 +121,11 @@ function lazyLoadingTests() { ...@@ -121,11 +121,11 @@ function lazyLoadingTests() {
await check(() => { await check(() => {
return browser.elementById('lazy-mid').getAttribute('src') return browser.elementById('lazy-mid').getAttribute('src')
}, 'https://example.com/myaccount/foo2.jpg?auto=format&fit=max&w=480') }, 'https://example.com/myaccount/foo2.jpg?auto=format&fit=max&w=1024')
await check(() => { await check(() => {
return browser.elementById('lazy-mid').getAttribute('srcset') return browser.elementById('lazy-mid').getAttribute('srcset')
}, 'https://example.com/myaccount/foo2.jpg?auto=format&fit=max&w=480 480w') }, 'https://example.com/myaccount/foo2.jpg?auto=format&fit=max&w=480 1x, https://example.com/myaccount/foo2.jpg?auto=format&fit=max&w=1024 2x')
}) })
it('should not have loaded the third image after scrolling down', async () => { it('should not have loaded the third image after scrolling down', async () => {
expect( expect(
...@@ -170,11 +170,11 @@ function lazyLoadingTests() { ...@@ -170,11 +170,11 @@ function lazyLoadingTests() {
await waitFor(200) await waitFor(200)
expect( expect(
await browser.elementById('lazy-without-attribute').getAttribute('src') await browser.elementById('lazy-without-attribute').getAttribute('src')
).toBe('https://example.com/myaccount/foo4.jpg?auto=format&fit=max&w=1024') ).toBe('https://example.com/myaccount/foo4.jpg?auto=format&fit=max&w=2000')
expect( expect(
await browser.elementById('lazy-without-attribute').getAttribute('srcset') await browser.elementById('lazy-without-attribute').getAttribute('srcset')
).toBe( ).toBe(
'https://example.com/myaccount/foo4.jpg?auto=format&fit=max&w=480 480w, https://example.com/myaccount/foo4.jpg?auto=format&fit=max&w=1024 1024w' 'https://example.com/myaccount/foo4.jpg?auto=format&fit=max&w=1024 1x, https://example.com/myaccount/foo4.jpg?auto=format&fit=max&w=1600 2x, https://example.com/myaccount/foo4.jpg?auto=format&fit=max&w=2000 3x'
) )
}) })
...@@ -220,14 +220,14 @@ describe('Image Component Tests', () => { ...@@ -220,14 +220,14 @@ describe('Image Component Tests', () => {
it('should add a preload tag for a priority image', async () => { it('should add a preload tag for a priority image', async () => {
expect( expect(
await hasPreloadLinkMatchingUrl( await hasPreloadLinkMatchingUrl(
'https://example.com/myaccount/withpriority.png?auto=format&fit=max&w=480&q=60' 'https://example.com/myaccount/withpriority.png?auto=format&fit=max&w=1024&q=60'
) )
).toBe(true) ).toBe(true)
}) })
it('should add a preload tag for a priority image with preceding slash', async () => { it('should add a preload tag for a priority image with preceding slash', async () => {
expect( expect(
await hasPreloadLinkMatchingUrl( await hasPreloadLinkMatchingUrl(
'https://example.com/myaccount/fooslash.jpg?auto=format&fit=max&w=480' 'https://example.com/myaccount/fooslash.jpg?auto=format&fit=max&w=1024'
) )
).toBe(true) ).toBe(true)
}) })
...@@ -241,7 +241,7 @@ describe('Image Component Tests', () => { ...@@ -241,7 +241,7 @@ describe('Image Component Tests', () => {
it('should add a preload tag for a priority image, with quality', async () => { it('should add a preload tag for a priority image, with quality', async () => {
expect( expect(
await hasPreloadLinkMatchingUrl( await hasPreloadLinkMatchingUrl(
'https://example.com/myaccount/withpriority.png?auto=format&fit=max&w=480&q=60' 'https://example.com/myaccount/withpriority.png?auto=format&fit=max&w=1024&q=60'
) )
).toBe(true) ).toBe(true)
}) })
...@@ -295,12 +295,12 @@ describe('Image Component Tests', () => { ...@@ -295,12 +295,12 @@ describe('Image Component Tests', () => {
expect( expect(
await browser.elementById('lazy-no-observer').getAttribute('src') await browser.elementById('lazy-no-observer').getAttribute('src')
).toBe( ).toBe(
'https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=1024' 'https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=2000'
) )
expect( expect(
await browser.elementById('lazy-no-observer').getAttribute('srcset') await browser.elementById('lazy-no-observer').getAttribute('srcset')
).toBe( ).toBe(
'https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=480 480w, https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=1024 1024w' 'https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=1024 1x, https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=2000 2x'
) )
}) })
}) })
...@@ -321,12 +321,12 @@ describe('Image Component Tests', () => { ...@@ -321,12 +321,12 @@ describe('Image Component Tests', () => {
expect( expect(
await browser.elementById('lazy-no-observer').getAttribute('src') await browser.elementById('lazy-no-observer').getAttribute('src')
).toBe( ).toBe(
'https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=1024' 'https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=2000'
) )
expect( expect(
await browser.elementById('lazy-no-observer').getAttribute('srcset') await browser.elementById('lazy-no-observer').getAttribute('srcset')
).toBe( ).toBe(
'https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=480 480w, https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=1024 1024w' 'https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=1024 1x, https://example.com/myaccount/foox.jpg?auto=format&fit=max&w=2000 2x'
) )
}) })
}) })
......
...@@ -80,7 +80,7 @@ function runTests(mode) { ...@@ -80,7 +80,7 @@ function runTests(mode) {
expect( expect(
await hasImageMatchingUrl( await hasImageMatchingUrl(
browser, browser,
`http://localhost:${appPort}/_next/image?url=%2Ftest.jpg&w=420&q=75` `http://localhost:${appPort}/_next/image?url=%2Ftest.jpg&w=1200&q=75`
) )
).toBe(true) ).toBe(true)
} finally { } finally {
...@@ -120,10 +120,10 @@ function runTests(mode) { ...@@ -120,10 +120,10 @@ function runTests(mode) {
const delta = 250 const delta = 250
const id = 'fixed1' const id = 'fixed1'
expect(await getSrc(browser, id)).toBe( expect(await getSrc(browser, id)).toBe(
'/_next/image?url=%2Fwide.png&w=1200&q=75' '/_next/image?url=%2Fwide.png&w=3840&q=75'
) )
expect(await browser.elementById(id).getAttribute('srcset')).toBe( expect(await browser.elementById(id).getAttribute('srcset')).toBe(
'/_next/image?url=%2Fwide.png&w=320&q=75 320w, /_next/image?url=%2Fwide.png&w=420&q=75 420w, /_next/image?url=%2Fwide.png&w=768&q=75 768w, /_next/image?url=%2Fwide.png&w=1024&q=75 1024w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w' '/_next/image?url=%2Fwide.png&w=1200&q=75 1x, /_next/image?url=%2Fwide.png&w=3840&q=75 2x'
) )
await browser.setDimensions({ await browser.setDimensions({
width: width + delta, width: width + delta,
...@@ -153,10 +153,10 @@ function runTests(mode) { ...@@ -153,10 +153,10 @@ function runTests(mode) {
const delta = 250 const delta = 250
const id = 'intrinsic1' const id = 'intrinsic1'
expect(await getSrc(browser, id)).toBe( expect(await getSrc(browser, id)).toBe(
'/_next/image?url=%2Fwide.png&w=1200&q=75' '/_next/image?url=%2Fwide.png&w=3840&q=75'
) )
expect(await browser.elementById(id).getAttribute('srcset')).toBe( expect(await browser.elementById(id).getAttribute('srcset')).toBe(
'/_next/image?url=%2Fwide.png&w=320&q=75 320w, /_next/image?url=%2Fwide.png&w=420&q=75 420w, /_next/image?url=%2Fwide.png&w=768&q=75 768w, /_next/image?url=%2Fwide.png&w=1024&q=75 1024w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w' '/_next/image?url=%2Fwide.png&w=1200&q=75 1x, /_next/image?url=%2Fwide.png&w=3840&q=75 2x'
) )
await browser.setDimensions({ await browser.setDimensions({
width: width + delta, width: width + delta,
...@@ -189,10 +189,10 @@ function runTests(mode) { ...@@ -189,10 +189,10 @@ function runTests(mode) {
const delta = 250 const delta = 250
const id = 'responsive1' const id = 'responsive1'
expect(await getSrc(browser, id)).toBe( expect(await getSrc(browser, id)).toBe(
'/_next/image?url=%2Fwide.png&w=1200&q=75' '/_next/image?url=%2Fwide.png&w=3840&q=75'
) )
expect(await browser.elementById(id).getAttribute('srcset')).toBe( expect(await browser.elementById(id).getAttribute('srcset')).toBe(
'/_next/image?url=%2Fwide.png&w=320&q=75 320w, /_next/image?url=%2Fwide.png&w=420&q=75 420w, /_next/image?url=%2Fwide.png&w=768&q=75 768w, /_next/image?url=%2Fwide.png&w=1024&q=75 1024w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w' '/_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'
) )
await browser.setDimensions({ await browser.setDimensions({
width: width + delta, width: width + delta,
...@@ -225,10 +225,10 @@ function runTests(mode) { ...@@ -225,10 +225,10 @@ function runTests(mode) {
const delta = 150 const delta = 150
const id = 'fill1' const id = 'fill1'
expect(await getSrc(browser, id)).toBe( expect(await getSrc(browser, id)).toBe(
'/_next/image?url=%2Fwide.png&w=1200&q=75' '/_next/image?url=%2Fwide.png&w=3840&q=75'
) )
expect(await browser.elementById(id).getAttribute('srcset')).toBe( expect(await browser.elementById(id).getAttribute('srcset')).toBe(
'/_next/image?url=%2Fwide.png&w=320&q=75 320w, /_next/image?url=%2Fwide.png&w=420&q=75 420w, /_next/image?url=%2Fwide.png&w=768&q=75 768w, /_next/image?url=%2Fwide.png&w=1024&q=75 1024w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w' '/_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'
) )
await browser.setDimensions({ await browser.setDimensions({
width: width + delta, width: width + delta,
...@@ -261,10 +261,10 @@ function runTests(mode) { ...@@ -261,10 +261,10 @@ function runTests(mode) {
const height = await getComputed(browser, id, 'height') const height = await getComputed(browser, id, 'height')
await browser.eval(`document.getElementById("${id}").scrollIntoView()`) await browser.eval(`document.getElementById("${id}").scrollIntoView()`)
expect(await getSrc(browser, id)).toBe( expect(await getSrc(browser, id)).toBe(
'/_next/image?url=%2Fwide.png&w=1200&q=75' '/_next/image?url=%2Fwide.png&w=3840&q=75'
) )
expect(await browser.elementById(id).getAttribute('srcset')).toBe( expect(await browser.elementById(id).getAttribute('srcset')).toBe(
'/_next/image?url=%2Fwide.png&w=320&q=75 320w, /_next/image?url=%2Fwide.png&w=420&q=75 420w, /_next/image?url=%2Fwide.png&w=768&q=75 768w, /_next/image?url=%2Fwide.png&w=1024&q=75 1024w, /_next/image?url=%2Fwide.png&w=1200&q=75 1200w' '/_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 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)
......
...@@ -19,7 +19,7 @@ jest.setTimeout(1000 * 60 * 2) ...@@ -19,7 +19,7 @@ jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
const imagesDir = join(appDir, '.next', 'cache', 'images') const imagesDir = join(appDir, '.next', 'cache', 'images')
const nextConfig = new File(join(appDir, 'next.config.js')) const nextConfig = new File(join(appDir, 'next.config.js'))
const largeSize = 1024 const largeSize = 1080 // defaults defined in server/config.ts
let appPort let appPort
let app let app
...@@ -441,7 +441,7 @@ describe('Image Optimizer', () => { ...@@ -441,7 +441,7 @@ describe('Image Optimizer', () => {
const domains = ['localhost', 'example.com'] const domains = ['localhost', 'example.com']
describe('dev support w/o next.config.js', () => { describe('dev support w/o next.config.js', () => {
const size = 320 // defaults defined in server/config.ts const size = 384 // defaults defined in server/config.ts
beforeAll(async () => { beforeAll(async () => {
appPort = await findPort() appPort = await findPort()
app = await launchApp(appDir, appPort) app = await launchApp(appDir, appPort)
...@@ -478,7 +478,7 @@ describe('Image Optimizer', () => { ...@@ -478,7 +478,7 @@ describe('Image Optimizer', () => {
}) })
describe('Server support w/o next.config.js', () => { describe('Server support w/o next.config.js', () => {
const size = 320 // defaults defined in server/config.ts const size = 384 // defaults defined in server/config.ts
beforeAll(async () => { beforeAll(async () => {
await nextBuild(appDir) await nextBuild(appDir)
appPort = await findPort() appPort = await findPort()
...@@ -557,7 +557,8 @@ describe('Image Optimizer', () => { ...@@ -557,7 +557,8 @@ describe('Image Optimizer', () => {
await fs.remove(imagesDir) await fs.remove(imagesDir)
}) })
it('should 404 when loader is not default', async () => { it('should 404 when loader is not default', async () => {
const query = { w: 320, q: 90, url: '/test.svg' } const size = 384 // defaults defined in server/config.ts
const query = { w: size, q: 90, url: '/test.svg' }
const opts = { headers: { accept: 'image/webp' } } const opts = { headers: { accept: 'image/webp' } }
const res = await fetchViaHTTP(appPort, '/_next/image', query, opts) const res = await fetchViaHTTP(appPort, '/_next/image', query, opts)
expect(res.status).toBe(404) expect(res.status).toBe(404)
......
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