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 component makes 2 calls to the image src #70

Open
0xdhrv opened this issue Feb 9, 2023 · 4 comments
Open

Avatar component makes 2 calls to the image src #70

0xdhrv opened this issue Feb 9, 2023 · 4 comments

Comments

@0xdhrv
Copy link

0xdhrv commented Feb 9, 2023

The Avatar component makes 2 calls in every render to the image src

image

@dBianchii
Copy link

Sorry, isn't this related to React Strictmode?

@0xdhrv
Copy link
Author

0xdhrv commented Apr 10, 2023

The screenshot depicts the live version of the ui.shadcn.com also, trying pure radix avatar doesn't seem to have this effect of repeated calls

@dBianchii
Copy link

Oh I see. Thought you were using it in your own app @0xdhrv

@marcusarnfast
Copy link

marcusarnfast commented Aug 5, 2023

When trying the pure radix avatar in next.js i still get the double fetch. But if you change the radix <AvatarPrimitive.Image /> to the <Image /> component from next.js then the issue resolves... This also seems to have a dramatic effect on performance due to the image optimization.

But anyway this is probably an issue with radix..

FYI. i have also tried to run the <AvatarPrimitive.Image asChild /> with the image component. I get the same issue..

Here is my Avatar component, not perfect.

"use client";

import * as React from "react";
import * as AvatarPrimitive from "@radix-ui/react-avatar";

import { cn } from "@/lib/utils";
import Image from "next/image";

const Avatar = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Root>,
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root>
>(({ className, ...props }, ref) => (
  <AvatarPrimitive.Root
    ref={ref}
    className={cn(
      "relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full",
      className
    )}
    {...props}
  />
));
Avatar.displayName = AvatarPrimitive.Root.displayName;

const AvatarImage = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Image>,
  React.ComponentPropsWithoutRef<typeof Image>
>(({ className, src, alt, ...props }, ref) => (
  <Image
    src={src}
    alt={alt}
    {...props}
    className={cn("aspect-square h-full w-full", className)}
  />
));
AvatarImage.displayName = AvatarPrimitive.Image.displayName;

const AvatarFallback = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Fallback>,
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback>
>(({ className, ...props }, ref) => (
  <AvatarPrimitive.Fallback
    ref={ref}
    className={cn(
      "flex h-full w-full items-center justify-center rounded-full bg-neutral-100 dark:bg-neutral-800",
      className
    )}
    {...props}
  />
));
AvatarFallback.displayName = AvatarPrimitive.Fallback.displayName;

export { Avatar, AvatarImage, AvatarFallback };

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

No branches or pull requests

3 participants