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

[BUG] - fallbackSrc not working when the src image returns a 404 #4756

Open
ryankashi opened this issue Jan 29, 2025 · 5 comments
Open

[BUG] - fallbackSrc not working when the src image returns a 404 #4756

ryankashi opened this issue Jan 29, 2025 · 5 comments
Labels
📦 Scope : Components Related to the components 🐛 Type: Bug Something isn't working

Comments

@ryankashi
Copy link

HeroUI Version

2.2.6

Describe the bug

When I try to load an image with a fallbackSrc, the image at the fallbackSrc does not load. For example:

const fallbackSrc = '/images/image_that_does_exist.jpg';
const src = '/images/image_that_will_404.jpg'

<Image src={src} fallbackSrc={fallbackSrc} />

Instead of rendering fallbackSrc with my fallback Image, instead the <Image/> component will stay blank.

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

Create a nextjs app and put in the following code:

const fallbackSrc = '/images/image_that_does_exist.jpg';
const src = '/images/image_that_will_404.jpg'

<Image src={src} fallbackSrc={fallbackSrc} />

This is tested both in Chrome as well as Electron

Expected behavior

The image at /images/image_that_will_404.jpg will return a 404 in my console. Then, the <Image/> component should use the fallbackSrc and render this image instead

Screenshots or Videos

N/A

Operating System Version

Windows 10

Browser

Chrome

Copy link

linear bot commented Jan 29, 2025

@wingkwong wingkwong added 🐛 Type: Bug Something isn't working 📦 Scope : Components Related to the components labels Jan 29, 2025
@theresasogunle
Copy link

@ryankashi have you considered passing a height prop to the image? i think it should work after you do that

@ryankashi
Copy link
Author

Just tested. The way how height affects src vs. fallbackSrc are different, even if the image passed into it is the same. In addition, the height prop is improperly affecting how the image should be rendered, especially when the component itself is responsive.

For example:

No Height specified, via src:

<Image src={fallbackSrc} fallbackSrc={fallbackSrc} isZoomed={true} alt="" className="object-cover aspect-square" />

Image

No height specified, via fallbackSrc:

<Image src={'/image/fake_image.jpg'} fallbackSrc={fallbackSrc} isZoomed={true} alt="" className="object-cover aspect-square" />

Image

Height specified, via src:

<Image src={fallbackSrc} fallbackSrc={fallbackSrc} height={720} isZoomed={true} alt="" className="object-cover aspect-square" />

Image

No height specified, via fallbackSrc:

<Image src={'/images/fake_image.jpg'} fallbackSrc={fallbackSrc} height={720} isZoomed={true} alt="" className="object-cover aspect-square" />

Image

@ryankashi
Copy link
Author

ryankashi commented Jan 30, 2025

Did some additional digging. This could be due to the component not rerendering when the image should switch to the fallback source.

For example, doing something like:

function TestFallbackImage(){
    const [key, setKey] = useState(0);
    const onError = function(){ setKey(key+1) }

    const src = '/images/fake_image.jpg';
    const fallbackSrc = '/images/fallback_image.jpg';

    return(
        <Image key={key} src={src} fallbackSrc={fallbackSrc} onError={onError}/>
    )
}

Will render SOMETHING, however the image itself does not fit correctly, and does not stay consistent with how it should render if it was passed directly into src.

Also to note, passing in a height and width should be optional for this component as well - you shouldn't have to pass these props in just to get a fallback image to render properly

Has anybody else been able to take a closer look into any of this?

Thanks!

@theresasogunle
Copy link

it could be because the fallbackSrc is rendered as a background-image of the image wrapper(a div element) which in turn means that without passing a height, the div will have no content thus, have a height of 0 which makes it not visible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 Scope : Components Related to the components 🐛 Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants