Skip to content

Commit eca4c8d

Browse files
authored
fix(next/image): improve error message when src={null} (#71398)
There is a React error that suggests the user change `src=""` to `src={null}`. > An empty string ("") was passed to the src attribute. This may cause the browser to download the whole page again over the network. To fix this, either do not render the element at all or pass null to src instead of an empty string. Even though `next/image` treats those both the same way, a user attempting to use `src={null}` would cause the build to crash with a confusing error message (JS only problem since TS would catch it much earlier since src is a required prop). ``` ⨯ TypeError: Cannot read properties of null (reading 'default') at isStaticRequire (.next/server/chunks/ssr/node_modules__pnpm_1b81c1._.js:142:16) at isStaticImport (.next/server/chunks/ssr/node_modules__pnpm_1b81c1._.js:148:40) at getImgProps (.next/server/chunks/ssr/node_modules__pnpm_1b81c1._.js:320:9) at .next/server/chunks/ssr/node_modules__pnpm_1b81c1._.js:2839:82 digest: "1613180311" ``` This PR ensures that `src={null}` will print the expected error that was added back in a previous PR: - #38847
1 parent efec3f5 commit eca4c8d

File tree

6 files changed

+54
-0
lines changed

6 files changed

+54
-0
lines changed

packages/next/src/shared/lib/get-img-props.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ function isStaticImageData(
111111

112112
function isStaticImport(src: string | StaticImport): src is StaticImport {
113113
return (
114+
!!src &&
114115
typeof src === 'object' &&
115116
(isStaticRequire(src as StaticImport) ||
116117
isStaticImageData(src as StaticImport))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react'
2+
import Image from 'next/image'
3+
4+
const Page = () => {
5+
return (
6+
<div>
7+
<p>Invalid Source Null</p>
8+
<Image alt="alt" id="id" src={null} width={400} height={400} />
9+
</div>
10+
)
11+
}
12+
13+
export default Page

test/integration/next-image-new/app-dir/test/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,18 @@ function runTests(mode: 'dev' | 'server') {
900900
}, /Image is missing required "src" property/gm)
901901
})
902902

903+
it('should show null src error', async () => {
904+
const browser = await webdriver(appPort, '/invalid-src-null')
905+
906+
await assertNoRedbox(browser)
907+
908+
await retry(async () => {
909+
expect(
910+
(await browser.log()).map((log) => log.message).join('\n')
911+
).toMatch(/Image is missing required "src" property/gm)
912+
})
913+
})
914+
903915
it('should show invalid src error', async () => {
904916
const browser = await webdriver(appPort, '/invalid-src')
905917

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react'
2+
import Image from 'next/image'
3+
4+
const Page = () => {
5+
return (
6+
<div>
7+
<p>Invalid Source Null</p>
8+
<Image alt="alt" id="id" src={null} width={400} height={400} />
9+
</div>
10+
)
11+
}
12+
13+
export default Page

test/integration/next-image-new/default/test/index.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
nextBuild,
1515
nextStart,
1616
renderViaHTTP,
17+
retry,
1718
waitFor,
1819
} from 'next-test-utils'
1920
import webdriver from 'next-webdriver'
@@ -901,6 +902,18 @@ function runTests(mode) {
901902
}, /Image is missing required "src" property/gm)
902903
})
903904

905+
it('should show null src error', async () => {
906+
const browser = await webdriver(appPort, '/invalid-src-null')
907+
908+
await assertNoRedbox(browser)
909+
910+
await retry(async () => {
911+
expect(
912+
(await browser.log()).map((log) => log.message).join('\n')
913+
).toMatch(/Image is missing required "src" property/gm)
914+
})
915+
})
916+
904917
it('should show invalid src error', async () => {
905918
const browser = await webdriver(appPort, '/invalid-src')
906919

test/integration/next-image-new/typescript/pages/invalid.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ const Invalid = () => {
5151
width={500}
5252
height={500}
5353
/>
54+
<Image id="missing-src" alt="missing-src" width={500} height={500} />
55+
<Image id="null-src" alt="null-src" src={null} width={500} height={500} />
5456
<p id="stubtext">This is the invalid usage</p>
5557
</div>
5658
)

0 commit comments

Comments
 (0)