Skip to content

[Avatar] Adding support for Framework image#3154

Closed
Udhayarajan wants to merge 5 commits into
radix-ui:mainfrom
Udhayarajan:fix/avatar-image-loading-next-js
Closed

[Avatar] Adding support for Framework image#3154
Udhayarajan wants to merge 5 commits into
radix-ui:mainfrom
Udhayarajan:fix/avatar-image-loading-next-js

Conversation

@Udhayarajan

@Udhayarajan Udhayarajan commented Sep 28, 2024

Copy link
Copy Markdown

Description

Hi Team,

I'm proposing a new suggestion to address issue #2230 on top of existing PR #2999

I've implemented this solution by removing the prefetch image which helps to determine the image loading error.

Since Primitive.img already handles OnLoad and OnError events, it makes sense to leverage these callbacks for handling image load failures in a single fetch.

Changes Introduced

  • On the first render, Primitive.img is displayed, and its OnLoad and OnError callbacks are subscribed through an intermediate handler:
    • Loading: The image’s style.display is set to none.
    • Success: The display property is updated to the user-specified value.
    • Failure: The component re-renders, returning null to display the fallback.
  • Removed the useImageLoadingStatus custom hook, simplifying the image loading process:
    function useImageLoadingStatus(src?: string, referrerPolicy?: React.HTMLAttributeReferrerPolicy) {

NOTE: I don't find any other use-case of useImageLoadingStatus hook, other than tracking image loading status

What This Solves

  • Enables better integration with framework-specific optimizations (e.g., Next.js’s Image component).

Visual Comparison

Before:

before.mp4

After:

after.mp4

EDIT:
Upon toggling Disable Cache off It makes only one request

@Udhayarajan Udhayarajan changed the title [Avatar] Optimize Image Loading by Removing Duplicated Fetch and Adding support for Framework image [Avatar] Adding support for Framework image Sep 28, 2024
@Udhayarajan Udhayarajan marked this pull request as draft September 28, 2024 09:43
@Udhayarajan Udhayarajan marked this pull request as ready for review September 28, 2024 09:58
@chaance chaance added the Resolution: Needs Investigation This issue needs more investigation label Oct 1, 2024
@chaance

chaance commented Jun 7, 2026

Copy link
Copy Markdown
Member

Planning to handle this a little differently #3943

@chaance chaance closed this Jun 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Resolution: Needs Investigation This issue needs more investigation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants