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

[Avatar] Fix flashing when image is already cached #3008

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

rkkautsar
Copy link

Description

Fixes #2044 by initializing loadingStatus with loaded if the image.complete is true, based on @lauri865 suggestion in the linked issue.

Based on MDN, image.complete will be true if:

  • Neither the src nor the srcset attribute is specified.
  • The srcset attribute is absent and the src attribute, while specified, is the empty string ("").
  • The image resource has been fully fetched and has been queued for rendering/compositing.
  • The image element has previously determined that the image is fully available and ready for use.
  • The image is "broken;" that is, the image failed to load due to an error or because image loading is disabled.

I added test case for all the cases, although I imagine the last case might be different from browser to browser. In Chrome, when image loading is disabled the load event will never fire and image.complete will always be false. To be safe I added another check for image.naturalWidth > 0 since image with no data will have naturalWidth = 0.

I also added test and story for the case when image src changing to make sure my change can handle the changing src.

Tests added:
✓ does not leak event listeners (7 ms)
✓ can handle changing src (627 ms)
✓ should render the image immediately after it is cached (327 ms)
✓ should not render image with no src (5 ms)
✓ should not render image with empty string as src (3 ms)
✓ should show fallback if image has no data (4 ms)

Storybook added:

  • Changing image src
Before After
fix-flashing-avatar-before fix-flashing-avatar-after

@LeakedDave
Copy link

LeakedDave commented Jul 9, 2024

Awesome, I've been waiting for this I was actually about to hack my own Avatar together myself... Will this be useable directly into Shadcn and NextUI or will we need an update from NextUI directly?

@rkkautsar
Copy link
Author

@LeakedDave the same avatar flicker issue in shadcn/ui should be fixed just by bumping the radix version if this fix is released. For nextui I think the same idea can be implemented to this hook: https://github.com/nextui-org/nextui/blob/canary/packages/hooks/use-image/src/index.ts

Copy link
Member

@chaance chaance left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor change and I'll merge 👍

@@ -116,35 +119,47 @@ AvatarFallback.displayName = FALLBACK_NAME;

/* -----------------------------------------------------------------------------------------------*/

function setImageSrcAndGetInitialState(image: HTMLImageElement, src?: string): ImageLoadingStatus {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this to resolveLoadingStatus?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chaance done! I also rebased with main since there's some conflict with the referrerPolicy change

@rkkautsar rkkautsar force-pushed the fix-avatar-cached-image-flashing branch from dc693e7 to 308d236 Compare August 19, 2024 06:44
@rkkautsar rkkautsar requested a review from chaance August 19, 2024 06:46
function useImageLoadingStatus(src?: string, referrerPolicy?: React.HTMLAttributeReferrerPolicy) {
const [loadingStatus, setLoadingStatus] = React.useState<ImageLoadingStatus>('idle');
const image = React.useRef(new window.Image());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, one more thing I noticed. This will error in SSR. We want to ensure that hydration is complete before initializing the image.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's my suggested fix:

  • Check if the app is hydrated on the client (this can either be a new function in the utils package or included in this file for now)
  • If the app is not hydrated (SSR + first client render) initialize the image ref as null
  • Initialize the state as "loading" pre-hydration to prevent client/server mismatch
function resolveLoadingStatus(image: HTMLImageElement | null, src?: string): ImageLoadingStatus {
  if (!src) {
    return 'error';
  }

  if (!image) {
    // image is not initialized during SSR. Ensures the initial state is
    // 'loading' on both the server and the client to prevent hydration errors.
    return 'loading';
  }

  if (image.src !== src) {
    image.src = src;
  }
  return image.complete && image.naturalWidth > 0 ? 'loaded' : 'loading';
}

function useImageLoadingStatus(src?: string, referrerPolicy?: React.HTMLAttributeReferrerPolicy) {
  const isHydrated = useIsHydrated();
  const image = React.useRef<HTMLImageElement | null>(isHydrated ? new window.Image() : null);
  const [loadingStatus, setLoadingStatus] = React.useState<ImageLoadingStatus>(() =>
    resolveLoadingStatus(image.current, src)
  );

  useLayoutEffect(() => {
    setLoadingStatus(resolveLoadingStatus(image.current, src));
  }, [src]);

  useLayoutEffect(() => {
    // We can safely assume that image.current is not null because hydration is
    // complete by the time this effect runs
    const img = image.current!;

    const updateStatus = (status: ImageLoadingStatus) => () => {
      setLoadingStatus(status);
    };

    const handleLoad = updateStatus('loaded');
    const handleError = updateStatus('error');
    img.addEventListener('load', handleLoad);
    img.addEventListener('error', handleError);
    if (referrerPolicy) {
      img.referrerPolicy = referrerPolicy;
    }

    return () => {
      img.removeEventListener('load', handleLoad);
      img.removeEventListener('error', handleError);
    };
  }, [referrerPolicy]);

  return loadingStatus;
}

function subscribe() {
  return () => {};
}

function useIsHydrated() {
  return React.useSyncExternalStore(
    subscribe,
    () => true,
    () => false
  );
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! also TIL useSyncExternalStore is the recommended way to detect hydration. I'll make the change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chaance done, I added a test for SSR and make sure it works in the ssr-testing app too. Need to make some change from your suggestion but overall it works. One thing is that the SSR testing will log this error:

 Warning: useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format. This will lead to a mismatch between the initial, non-hydrated UI and the intended UI. To avoid this, useLayoutEffect should only be used in components that render exclusively on the client. See https://reactjs.org/link/uselayouteffect-ssr for common fixes.

since we use useLayoutEffect for Avatar. It's just a warning from jest though, and the Avatar component server rendered to its fallback component just fine as tested with unit test and manually checked in the ssr-testing app

@rkkautsar rkkautsar requested a review from chaance August 23, 2024 08:32
@mackermans
Copy link

Hey @chaance @rkkautsar, is this PR ready to be merged? 🙏

@quachthientai
Copy link

Hello, I just want to follow if this PR is ready?

@rkkautsar
Copy link
Author

@mackermans @quachthientai need maintainer to review the change 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avatars flash on mount even if cached
5 participants