Skip to content

Commit

Permalink
Relax warning for next/image loader width (#30562)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
styfle authored Oct 28, 2021
1 parent ee062ae commit c4ee78f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,20 @@ export default function Image({
`Image with src "${src}" is using unsupported "style" property. Please use the "className" property instead.`
)
}
const rand = Math.floor(Math.random() * 1000) + 100
if (
!unoptimized &&
!loader({ src, width: rand, quality: 75 }).includes(rand.toString())
) {
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 (!unoptimized) {
const rand = Math.floor(Math.random() * 1000) + 100
const url = loader({ src, width: rand, quality: 75 })
if (
!url.includes(rand.toString()) &&
!url.includes('w=') &&
!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 (
Expand Down
41 changes: 41 additions & 0 deletions test/integration/image-component/default/pages/invalid-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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
21 changes: 21 additions & 0 deletions test/integration/image-component/default/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
//server-only tests
it('should not create an image folder in server/chunks', async () => {
Expand Down

0 comments on commit c4ee78f

Please sign in to comment.