Open ahkhanjani opened 9 months ago
@ahkhanjani
Hi.
In Next.js image component source code, they compare img.height
with img.getAttribute("height")
.
And Although your height calculation function always calculate after the decimal point for height (img.getAttribute("height")
), img.height
somehow only get rounded number of height, and comparison always fails and warn occurs.
Therefore for easy fix, you can just add Math.round
or Math.ceil
in your height calculation function and warn would be disappeared.
const aspectRatioHeight = Math.round((+realHeight / +realWidth) * 200);
Thank you @coffeecupjapan.
And Although your height calculation function always calculate after the decimal point for height (
img.getAttribute("height")
),img.height
somehow only get rounded number of height, and comparison always fails and warn occurs.
So the error is completely wrong. It should show an accurate error.
@coffeecupjapan I tried rounding the height and wow, some images work with Math.floor()
, some with Math.ceil()
. There is probably some weird internal rounding going on. Any ideas?
Link to the code that reproduces this issue
https://codesandbox.io/p/devbox/charming-field-hlgq68?file=%2Fapp%2Fpage.tsx%3A9%2C2
To Reproduce
next/image
with both width and height set as numbers.Current vs. Expected behavior
I'm setting both width and height. This warning shouldn't appear.
Provide environment information
Which area(s) are affected? (Select all that apply)
Image optimization (next/image, next/legacy/image)
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
The problem is not new and it's been like this since early next 13 when I bootstrapped the project so I don't think it's version specific.
What I'm doing is basically get the image URL and its real dimensions from Hygraph, hardcode the width that I want, and calculate the corresponding height of the image and pass it to the
<Image>
component. Here's how it looks:width
andheight
are string values. I'm converting them to numbers using the plus sign.I've tried setting height to
auto
and even0
using thestyle
attribute and Tailwind but it didn't work.