Avatar
TheAvatar component is a flexible UI element used to display user profile pictures or initials. It includes built-in support for proxying Google avatar URLs to avoid 429 Too Many Requests errors and handles fallback states gracefully.
Overview
When users sign in with Google, their profile pictures are hosted ongoogleusercontent.com. Repeatedly loading these images across different client instances can trigger rate limits.
The system mitigates this by:
- Backend Proxying: A Bun backend service (
/api/v1/users/avatar) fetches and caches the image bytes. - Frontend Rewriting: A utility rewrites Google avatar URLs to point to our proxy.
- Graceful Fallback: If the image fails to load, it falls back to user initials.
Usage
Basic Usage
Sizes
The component supports multiple pre-defined sizes using thesize prop:
| Size | Class | Description |
|---|---|---|
xs | h-6 w-6 | Extra small (sidebar, inline) |
sm | h-8 w-8 | Small |
md | h-10 w-10 | Medium (Default) |
lg | h-12 w-12 | Large |
xl | h-16 w-16 | Extra large (profile pages) |
Proxy System
ThegetAvatarUrl utility identifies Google-hosted images and rewrites them to use the backend proxy.
How it works
- Checks if the URL ends with
.googleusercontent.comor.ggpht.com. - Encodes the original URL as a query parameter.
- Returns a URL pointing to
${API_URL}/api/v1/users/avatar?url=....
Backend Implementation
The backend proxy:- Validates that the requested URL is a Google-owned host.
- Fetches the image once from Google.
- Caches the bytes in-memory for 6 hours.
- Returns appropriate
Cache-ControlandETagheaders for browser-level caching.
Component API
Props
| Prop | Type | Default | Description |
|---|---|---|---|
src | string | undefined | The image URL. Use getAvatarUrl() to enable proxying. |
alt | string | "Avatar" | Alt text for the image. |
fallback | string | undefined | Content to show if the image is missing or fails to load. |
size | "xs" "sm" "md" "lg" "xl" | "md" | The visual size of the avatar. |
class | string | undefined | Additional CSS classes for the container. |
Related Files
frontend/solidstart/packages/ui/src/components/ui/Avatar.tsxfrontend/solidstart/packages/core/src/utils/avatar.tsbackend/bun/apps/monolith/src/services/users/api.ts