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

Relax warning for `next/image` loader width (#30562)

Some users reported false hits when using complex loaders that implement Art Direction.

We can relax the warnings so that query string params named width should not warn.
parent ee062aee
...@@ -449,15 +449,20 @@ export default function Image({ ...@@ -449,15 +449,20 @@ export default function Image({
`Image with src "${src}" is using unsupported "style" property. Please use the "className" property instead.` `Image with src "${src}" is using unsupported "style" property. Please use the "className" property instead.`
) )
} }
const rand = Math.floor(Math.random() * 1000) + 100
if ( if (!unoptimized) {
!unoptimized && const rand = Math.floor(Math.random() * 1000) + 100
!loader({ src, width: rand, quality: 75 }).includes(rand.toString()) const url = loader({ src, width: rand, quality: 75 })
) { if (
console.warn( !url.includes(rand.toString()) &&
`Image with src "${src}" has a "loader" property that does not implement width. Please implement it or use the "unoptimized" property instead.` + !url.includes('w=') &&
`\nRead more: https://nextjs.org/docs/messages/next-image-missing-loader-width` !url.includes('width=')
) ) {
console.warn(
`Image with src "${src}" has a "loader" property that does not implement width. Please implement it or use the "unoptimized" property instead.` +
`\nRead more: https://nextjs.org/docs/messages/next-image-missing-loader-width`
)
}
} }
if ( if (
......
import React from 'react'
import Image from 'next/image'
const Page = () => {
return (
<div>
<h1>Warn for this loader that doesnt use width</h1>
<Image
id="no-width"
src="/test.png"
width={100}
height={100}
loader={({ src }) => `${src}`}
/>
<Image
id="width-path"
src="/test.jpg"
width={100}
height={100}
loader={({ src, width }) => `${src}/${width}/file.jpg`}
/>
<Image
id="width-querystring"
src="/test.webp"
width={100}
height={100}
loader={({ src, width }) => `${src}?w=${width / 2}`}
/>
<Image
id="width-querystring-full"
src="/test.gif"
width={100}
height={100}
loader={({ src, width }) => `${src}?width=${width * 2}`}
/>
<footer>footer</footer>
</div>
)
}
export default Page
...@@ -702,6 +702,27 @@ function runTests(mode) { ...@@ -702,6 +702,27 @@ function runTests(mode) {
} }
} }
}) })
it('should warn when loader is missing width', async () => {
const browser = await webdriver(appPort, '/invalid-loader')
await browser.eval(`document.querySelector("footer").scrollIntoView()`)
const warnings = (await browser.log('browser'))
.map((log) => log.message)
.join('\n')
expect(await hasRedbox(browser)).toBe(false)
expect(warnings).toMatch(
/Image with src (.*)png(.*) has a "loader" property that does not implement width/gm
)
expect(warnings).not.toMatch(
/Image with src (.*)jpg(.*) has a "loader" property that does not implement width/gm
)
expect(warnings).not.toMatch(
/Image with src (.*)webp(.*) has a "loader" property that does not implement width/gm
)
expect(warnings).not.toMatch(
/Image with src (.*)gif(.*) has a "loader" property that does not implement width/gm
)
})
} else { } else {
//server-only tests //server-only tests
it('should not create an image folder in server/chunks', async () => { it('should not create an image folder in server/chunks', async () => {
......
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