Skip to content

Commit 142af81

Browse files
authoredOct 29, 2021
Relax warning for next/image loader width even more (#30624)
1 parent 842a130 commit 142af81

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed
 

‎packages/next/client/image.tsx

+10-7
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,16 @@ export default function Image({
451451
}
452452

453453
if (!unoptimized) {
454-
const rand = Math.floor(Math.random() * 1000) + 100
455-
const url = loader({ src, width: rand, quality: 75 })
456-
if (
457-
!url.includes(rand.toString()) &&
458-
!url.includes('w=') &&
459-
!url.includes('width=')
460-
) {
454+
const urlStr = loader({
455+
src,
456+
width: widthInt || 400,
457+
quality: qualityInt || 75,
458+
})
459+
let url: URL | undefined
460+
try {
461+
url = new URL(urlStr)
462+
} catch (err) {}
463+
if (urlStr === src || (url && url.pathname === src && !url.search)) {
461464
console.warn(
462465
`Image with src "${src}" has a "loader" property that does not implement width. Please implement it or use the "unoptimized" property instead.` +
463466
`\nRead more: https://nextjs.org/docs/messages/next-image-missing-loader-width`

‎test/integration/image-component/default/pages/invalid-loader.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,27 @@ const Page = () => {
2020
loader={({ src, width }) => `${src}/${width}/file.jpg`}
2121
/>
2222
<Image
23-
id="width-querystring"
23+
id="width-querystring-w"
2424
src="/test.webp"
2525
width={100}
2626
height={100}
2727
loader={({ src, width }) => `${src}?w=${width / 2}`}
2828
/>
2929
<Image
30-
id="width-querystring-full"
30+
id="width-querystring-width"
3131
src="/test.gif"
3232
width={100}
3333
height={100}
34-
loader={({ src, width }) => `${src}?width=${width * 2}`}
34+
loader={({ src, width }) =>
35+
`https://example.com${src}?width=${width * 2}`
36+
}
37+
/>
38+
<Image
39+
id="width-querystring-size"
40+
src="/test.tiff"
41+
width={100}
42+
height={100}
43+
loader={({ src }) => `https://example.com${src}?size=medium`}
3544
/>
3645
<footer>footer</footer>
3746
</div>

‎test/integration/image-component/default/test/index.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,9 @@ function runTests(mode) {
722722
expect(warnings).not.toMatch(
723723
/Image with src (.*)gif(.*) has a "loader" property that does not implement width/gm
724724
)
725+
expect(warnings).not.toMatch(
726+
/Image with src (.*)tiff(.*) has a "loader" property that does not implement width/gm
727+
)
725728
})
726729
} else {
727730
//server-only tests

0 commit comments

Comments
 (0)
Please sign in to comment.