Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error when image width={Infinity} #48855

Merged
merged 20 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/next/src/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -717,19 +717,27 @@ const Image = forwardRef<HTMLImageElement | null, ImageProps>(
throw new Error(
`Image with src "${src}" is missing required "width" property.`
)
} else if (isNaN(widthInt)) {
} else if (Number.isNaN(widthInt)) {
throw new Error(
`Image with src "${src}" has invalid "width" property. Expected a numeric value in pixels but received "${width}".`
)
} else if (!Number.isFinite(widthInt)) {
throw new Error(
`Image with src "${src}" has invalid "width" property. Expected a numeric value in pixels but received not isFinite.`
)
}
if (typeof heightInt === 'undefined') {
throw new Error(
`Image with src "${src}" is missing required "height" property.`
)
} else if (isNaN(heightInt)) {
} else if (Number.isNaN(heightInt)) {
throw new Error(
`Image with src "${src}" has invalid "height" property. Expected a numeric value in pixels but received "${height}".`
)
} else if (!Number.isFinite(heightInt)) {
throw new Error(
`Image with src "${src}" has invalid "height" property. Expected a numeric value in pixels but received not isFinite.`
)
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/integration/next-image-new/app-dir/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,15 @@ function runTests(mode) {
)
})

it('should show error when Infinity width prop', async () => {
const browser = await webdriver(appPort, '/invalid-notFinite-width')

expect(await hasRedbox(browser, true)).toBe(true)
expect(await getRedboxHeader(browser)).toContain(
`Image with src "/test.jpg" has invalid "width" property. Expected a numeric value in pixels but received not isFinite.`
)
})

it('should show error when invalid height prop', async () => {
const browser = await webdriver(appPort, '/invalid-height')

Expand All @@ -833,6 +842,15 @@ function runTests(mode) {
)
})

it('should show error when Infinity height prop', async () => {
const browser = await webdriver(appPort, '/invalid-notFinite-height')

expect(await hasRedbox(browser, true)).toBe(true)
expect(await getRedboxHeader(browser)).toContain(
`Image with src "/test.jpg" has invalid "height" property. Expected a numeric value in pixels but received not isFinite.`
)
})

it('should show missing alt error', async () => {
const browser = await webdriver(appPort, '/missing-alt')

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'
import Image from 'next/image'

export default function Page() {
return (
<div>
<p>Infinity height</p>
<Image src="/test.jpg" width={400} height={Infinity} />
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'
import Image from 'next/image'

export default function Page() {
return (
<div>
<p>Infinity width</p>
<Image src="/test.jpg" width={Infinity} height={300} />
</div>
)
}