Unverified Commit 51022d58 authored by Taehun Jeong's avatar Taehun Jeong Committed by GitHub

Fix Show error when user put wrong values in width or height (#26166)



## Bug

- [x] Related issues linked using `fixes #26135 `
- [x] Integration tests added

fixes #26135

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #26135 `
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.

## Documentation / Examples

- [ ] Make sure the linting passes
parent 8a76cad9
......@@ -360,6 +360,14 @@ export default function Image({
).join(',')}.`
)
}
if (
(typeof widthInt !== 'undefined' && isNaN(widthInt)) ||
(typeof heightInt !== 'undefined' && isNaN(heightInt))
) {
throw new Error(
`Image with src "${src}" has invalid "width" or "height" property. These should be numeric values.`
)
}
if (!VALID_LOADING_VALUES.includes(loading)) {
throw new Error(
`Image with src "${src}" has invalid "loading" property. Provided "${loading}" should be one of ${VALID_LOADING_VALUES.map(
......@@ -373,7 +381,7 @@ export default function Image({
)
}
if (placeholder === 'blur') {
if ((widthInt || 0) * (heightInt || 0) < 1600) {
if (layout !== 'fill' && (widthInt || 0) * (heightInt || 0) < 1600) {
console.warn(
`Image with src "${src}" is smaller than 40x40. Consider removing the "placeholder='blur'" property to improve performance.`
)
......
......@@ -4,7 +4,13 @@ import Image from 'next/image'
const Page = () => {
return (
<div>
<Image id="invalid-placeholder-blur" src="/test.png" placeholder="blur" />
<Image
id="invalid-placeholder-blur"
src="/test.png"
placeholder="blur"
width={200}
height={200}
/>
</div>
)
}
......
import React from 'react'
import Image from 'next/image'
export default function Page() {
return (
<div>
<p>Invalid width or height</p>
<Image
src="/test.jpg"
width="fill"
height="fill"
layout="responsive"
placeholder="blur"
blurDataURL="/test.jpg"
/>
</div>
)
}
......@@ -494,6 +494,15 @@ function runTests(mode) {
)
})
it('should show error when not numeric string width or height', async () => {
const browser = await webdriver(appPort, '/invalid-width-or-height')
expect(await hasRedbox(browser)).toBe(true)
expect(await getRedboxHeader(browser)).toContain(
`Image with src "/test.jpg" has invalid "width" or "height" property. These should be numeric values.`
)
})
it('should show error when static import and placeholder=blur and blurDataUrl is missing', async () => {
const browser = await webdriver(
appPort,
......
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