Unverified Commit f773a1a4 authored by Steven's avatar Steven Committed by GitHub

Rename iconSizes to imageSizes, remove size limitation (#18294)

This does two things:

- Rename `iconSizes` to `imageSizes`.
- Give priority to `imageSizes` regardless of `deviceSizes` as a means to opt-out of the srcset behavior.
parent 774286fc
...@@ -62,14 +62,14 @@ module.exports = { ...@@ -62,14 +62,14 @@ module.exports = {
} }
``` ```
### Icon Sizes ### Image Sizes
You can specify a list of icon image widths using the `iconSizes` property. These widths should be smaller than the smallest value in `deviceSizes`. The purpose is for images that don't scale with the browser window, such as icons or badges. If `iconSizes` is not defined, then `deviceSizes` will be used. 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.
```js ```js
module.exports = { module.exports = {
images: { images: {
iconSizes: [16, 32, 64], imageSizes: [16, 32, 64],
}, },
} }
``` ```
......
...@@ -1111,8 +1111,8 @@ export default async function build( ...@@ -1111,8 +1111,8 @@ export default async function build(
} }
const images = { ...config.images } const images = { ...config.images }
const { deviceSizes, iconSizes } = images const { deviceSizes, imageSizes } = images
images.sizes = [...deviceSizes, ...iconSizes] images.sizes = [...deviceSizes, ...imageSizes]
await promises.writeFile( await promises.writeFile(
path.join(distDir, IMAGES_MANIFEST), path.join(distDir, IMAGES_MANIFEST),
......
...@@ -992,7 +992,7 @@ export default async function getBaseWebpackConfig( ...@@ -992,7 +992,7 @@ export default async function getBaseWebpackConfig(
), ),
'process.env.__NEXT_IMAGE_OPTS': JSON.stringify({ 'process.env.__NEXT_IMAGE_OPTS': JSON.stringify({
deviceSizes: config.images.deviceSizes, deviceSizes: config.images.deviceSizes,
iconSizes: config.images.iconSizes, imageSizes: config.images.imageSizes,
path: config.images.path, path: config.images.path,
loader: config.images.loader, loader: config.images.loader,
...(dev ...(dev
......
...@@ -15,7 +15,7 @@ type LoaderKey = 'imgix' | 'cloudinary' | 'akamai' | 'default' ...@@ -15,7 +15,7 @@ type LoaderKey = 'imgix' | 'cloudinary' | 'akamai' | 'default'
type ImageData = { type ImageData = {
deviceSizes: number[] deviceSizes: number[]
iconSizes: number[] imageSizes: number[]
loader: LoaderKey loader: LoaderKey
path: string path: string
domains?: string[] domains?: string[]
...@@ -38,14 +38,14 @@ type ImageProps = Omit< ...@@ -38,14 +38,14 @@ type ImageProps = Omit<
const imageData: ImageData = process.env.__NEXT_IMAGE_OPTS as any const imageData: ImageData = process.env.__NEXT_IMAGE_OPTS as any
const { const {
deviceSizes: configDeviceSizes, deviceSizes: configDeviceSizes,
iconSizes: configIconSizes, imageSizes: configImageSizes,
loader: configLoader, loader: configLoader,
path: configPath, path: configPath,
domains: configDomains, domains: configDomains,
} = imageData } = imageData
// sort smallest to largest // sort smallest to largest
configDeviceSizes.sort((a, b) => a - b) configDeviceSizes.sort((a, b) => a - b)
configIconSizes.sort((a, b) => a - b) configImageSizes.sort((a, b) => a - b)
let cachedObserver: IntersectionObserver let cachedObserver: IntersectionObserver
const IntersectionObserver = const IntersectionObserver =
...@@ -87,8 +87,7 @@ function getDeviceSizes(width: number | undefined): number[] { ...@@ -87,8 +87,7 @@ function getDeviceSizes(width: number | undefined): number[] {
if (typeof width !== 'number') { if (typeof width !== 'number') {
return configDeviceSizes return configDeviceSizes
} }
const smallest = configDeviceSizes[0] if (configImageSizes.includes(width)) {
if (width < smallest && configIconSizes.includes(width)) {
return [width] return [width]
} }
const widths: number[] = [] const widths: number[] = []
......
...@@ -26,7 +26,7 @@ const defaultConfig: { [key: string]: any } = { ...@@ -26,7 +26,7 @@ const defaultConfig: { [key: string]: any } = {
analyticsId: process.env.VERCEL_ANALYTICS_ID || '', analyticsId: process.env.VERCEL_ANALYTICS_ID || '',
images: { images: {
deviceSizes: [320, 420, 768, 1024, 1200], deviceSizes: [320, 420, 768, 1024, 1200],
iconSizes: [], imageSizes: [],
domains: [], domains: [],
path: '/_next/image', path: '/_next/image',
loader: 'default', loader: 'default',
...@@ -280,27 +280,27 @@ function assignDefaults(userConfig: { [key: string]: any }) { ...@@ -280,27 +280,27 @@ function assignDefaults(userConfig: { [key: string]: any }) {
) )
} }
} }
if (images.iconSizes) { if (images.imageSizes) {
const { iconSizes } = images const { imageSizes } = images
if (!Array.isArray(iconSizes)) { if (!Array.isArray(imageSizes)) {
throw new Error( throw new Error(
`Specified images.iconSizes should be an Array received ${typeof iconSizes}` `Specified images.imageSizes should be an Array received ${typeof imageSizes}`
) )
} }
if (iconSizes.length > 25) { if (imageSizes.length > 25) {
throw new Error( throw new Error(
`Specified images.iconSizes exceeds length of 25, received length (${iconSizes.length}), please reduce the length of the array to continue` `Specified images.imageSizes exceeds length of 25, received length (${imageSizes.length}), please reduce the length of the array to continue`
) )
} }
const invalid = iconSizes.filter((d: unknown) => { const invalid = imageSizes.filter((d: unknown) => {
return typeof d !== 'number' || d < 1 || d > 10000 return typeof d !== 'number' || d < 1 || d > 10000
}) })
if (invalid.length > 0) { if (invalid.length > 0) {
throw new Error( throw new Error(
`Specified images.iconSizes should be an Array of numbers that are between 1 and 10000, received invalid values (${invalid.join( `Specified images.imageSizes should be an Array of numbers that are between 1 and 10000, received invalid values (${invalid.join(
', ' ', '
)})` )})`
) )
......
...@@ -25,7 +25,7 @@ const VECTOR_TYPES = [SVG] ...@@ -25,7 +25,7 @@ const VECTOR_TYPES = [SVG]
type ImageData = { type ImageData = {
deviceSizes: number[] deviceSizes: number[]
iconSizes: number[] imageSizes: number[]
loader: string loader: string
path: string path: string
domains?: string[] domains?: string[]
...@@ -39,8 +39,8 @@ export async function imageOptimizer( ...@@ -39,8 +39,8 @@ export async function imageOptimizer(
) { ) {
const { nextConfig, distDir } = server const { nextConfig, distDir } = server
const imageData: ImageData = nextConfig.images const imageData: ImageData = nextConfig.images
const { deviceSizes = [], iconSizes = [], domains = [], loader } = imageData const { deviceSizes = [], imageSizes = [], domains = [], loader } = imageData
const sizes = [...deviceSizes, ...iconSizes] const sizes = [...deviceSizes, ...imageSizes]
if (loader !== 'default') { if (loader !== 'default') {
await server.render404(req, res, parsedUrl) await server.render404(req, res, parsedUrl)
......
module.exports = { module.exports = {
images: { images: {
deviceSizes: [480, 1024, 1600, 2000], deviceSizes: [480, 1024, 1600, 2000],
iconSizes: [16, 64], imageSizes: [16, 64],
path: 'https://example.com/myaccount/', path: 'https://example.com/myaccount/',
loader: 'imgix', loader: 'imgix',
}, },
......
...@@ -51,7 +51,7 @@ function runTests() { ...@@ -51,7 +51,7 @@ function runTests() {
await browser.elementById('preceding-slash-image').getAttribute('srcset') await browser.elementById('preceding-slash-image').getAttribute('srcset')
).toBe('https://example.com/myaccount/fooslash.jpg?auto=format&w=480 480w') ).toBe('https://example.com/myaccount/fooslash.jpg?auto=format&w=480 480w')
}) })
it('should use iconSizes 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&w=16' 'https://example.com/myaccount/icon.png?auto=format&w=16'
) )
......
...@@ -398,12 +398,12 @@ describe('Image Optimizer', () => { ...@@ -398,12 +398,12 @@ describe('Image Optimizer', () => {
) )
}) })
it('should error when iconSizes contains invalid widths', async () => { it('should error when imageSizes contains invalid widths', async () => {
await nextConfig.replace( await nextConfig.replace(
'{ /* replaceme */ }', '{ /* replaceme */ }',
JSON.stringify({ JSON.stringify({
images: { images: {
iconSizes: [0, 16, 64, 12000], imageSizes: [0, 16, 64, 12000],
}, },
}) })
) )
...@@ -419,7 +419,7 @@ describe('Image Optimizer', () => { ...@@ -419,7 +419,7 @@ describe('Image Optimizer', () => {
await nextConfig.restore() await nextConfig.restore()
expect(stderr).toContain( expect(stderr).toContain(
'Specified images.iconSizes should be an Array of numbers that are between 1 and 10000, received invalid values (0, 12000)' 'Specified images.imageSizes should be an Array of numbers that are between 1 and 10000, received invalid values (0, 12000)'
) )
}) })
}) })
...@@ -447,7 +447,7 @@ describe('Image Optimizer', () => { ...@@ -447,7 +447,7 @@ describe('Image Optimizer', () => {
const json = JSON.stringify({ const json = JSON.stringify({
images: { images: {
deviceSizes: [largeSize], deviceSizes: [largeSize],
iconSizes: [size], imageSizes: [size],
domains, domains,
}, },
}) })
......
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