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

feat(next/image): warn when `images.qualities` is undefined (#81690)

Add a warning to Next.js 15.5 to prepare for Next.js 16 when
`images.qualities` is undefined and the user attempted to use set
`quality` prop to something other than 75.
parent 8b3c9337
......@@ -859,6 +859,13 @@ export default function Image({
)
}
if (qualityInt && qualityInt !== 75 && !config.qualities) {
warnOnce(
`Image with src "${src}" is using quality "${qualityInt}" which is not configured in images.qualities. This config will be required starting in Next.js 16.` +
`\nRead more: https://nextjs.org/docs/messages/next-image-unconfigured-qualities`
)
}
if (!unoptimized && loader !== defaultImageLoader) {
const urlStr = loader({
config,
......
......@@ -532,6 +532,12 @@ export function getImgProps(
)
}
}
if (qualityInt && qualityInt !== 75 && !config.qualities) {
warnOnce(
`Image with src "${src}" is using quality "${qualityInt}" which is not configured in images.qualities. This config will be required starting in Next.js 16.` +
`\nRead more: https://nextjs.org/docs/messages/next-image-unconfigured-qualities`
)
}
if (placeholder === 'blur' && !blurDataURL) {
const VALID_BLUR_EXT = ['jpeg', 'png', 'webp', 'avif'] // should match next-image-loader
......
import React from 'react'
import Image from 'next/image'
const Page = () => {
return (
<Image
id="quality-50"
alt="quality-50"
src="/test.jpg"
width="400"
height="400"
quality="50"
/>
)
}
export default Page
......@@ -915,6 +915,18 @@ function runTests(mode) {
)
})
it('should warn when quality is 50', async () => {
const browser = await webdriver(appPort, '/quality-50')
const warnings = (await browser.log())
.map((log) => log.message)
.join('\n')
await assertNoRedbox(browser)
expect(warnings).toMatch(
/Image with src (.*)jpg(.*) is using quality "50" which is not configured in images.qualities. This config will be required starting in Next.js 16./gm
)
})
it('should not warn when Image is child of p', async () => {
const browser = await webdriver(appPort, '/inside-paragraph')
......
import React from 'react'
import Image from 'next/image'
const Page = () => {
return (
<Image
id="quality-50"
alt="quality-50"
src="/test.jpg"
width="400"
height="400"
quality="50"
/>
)
}
export default Page
......@@ -1052,6 +1052,18 @@ function runTests(mode: 'dev' | 'server') {
)
})
it('should warn when quality is 50', async () => {
const browser = await webdriver(appPort, '/quality-50')
const warnings = (await browser.log())
.map((log) => log.message)
.join('\n')
await assertNoRedbox(browser)
expect(warnings).toMatch(
/Image with src (.*)jpg(.*) is using quality "50" which is not configured in images.qualities. This config will be required starting in Next.js 16./gm
)
})
it('should not warn when Image is child of p', async () => {
const browser = await webdriver(appPort, '/inside-paragraph')
......
import React from 'react'
import Image from 'next/image'
const Page = () => {
return (
<Image
id="quality-50"
alt="quality-50"
src="/test.jpg"
width="400"
height="400"
quality="50"
/>
)
}
export default Page
......@@ -1054,6 +1054,18 @@ function runTests(mode) {
)
})
it('should warn when quality is 50', async () => {
const browser = await webdriver(appPort, '/quality-50')
const warnings = (await browser.log())
.map((log) => log.message)
.join('\n')
await assertNoRedbox(browser)
expect(warnings).toMatch(
/Image with src (.*)jpg(.*) is using quality "50" which is not configured in images.qualities. This config will be required starting in Next.js 16./gm
)
})
it('should not warn when Image is child of p', async () => {
const browser = await webdriver(appPort, '/inside-paragraph')
......
......@@ -120,7 +120,9 @@ describe('getImageProps()', () => {
height: 200,
quality: 50,
})
expect(warningMessages).toStrictEqual([])
expect(warningMessages).toStrictEqual([
'Image with src "/test.png" is using quality "50" which is not configured in images.qualities. This config will be required starting in Next.js 16.\nRead more: https://nextjs.org/docs/messages/next-image-unconfigured-qualities',
])
expect(Object.entries(props)).toStrictEqual([
['alt', 'a nice desc'],
['id', 'my-image'],
......@@ -136,6 +138,31 @@ describe('getImageProps()', () => {
['src', '/_next/image?url=%2Ftest.png&w=256&q=50'],
])
})
it('should handle quality as a string and not warn', async () => {
const { props } = getImageProps({
alt: 'a nice desc',
id: 'my-image',
src: '/test.png',
width: 100,
height: 200,
quality: '75',
})
expect(warningMessages).toStrictEqual([])
expect(Object.entries(props)).toStrictEqual([
['alt', 'a nice desc'],
['id', 'my-image'],
['loading', 'lazy'],
['width', 100],
['height', 200],
['decoding', 'async'],
['style', { color: 'transparent' }],
[
'srcSet',
'/_next/image?url=%2Ftest.png&w=128&q=75 1x, /_next/image?url=%2Ftest.png&w=256&q=75 2x',
],
['src', '/_next/image?url=%2Ftest.png&w=256&q=75'],
])
})
it('should handle loading eager', async () => {
const { props } = getImageProps({
alt: 'a nice desc',
......
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