Merge robust avatar loading (no-referrer + fallback)

This commit is contained in:
npeter83 2026-06-12 18:01:43 +02:00
commit 6d9a485c21
6 changed files with 77 additions and 29 deletions

View file

@ -0,0 +1,48 @@
import { useEffect, useState } from "react";
// Avatar image with a graceful fallback.
//
// Uses referrerPolicy="no-referrer" because Google's avatar CDNs
// (yt3.ggpht.com / lh3.googleusercontent.com) rate-limit bursts of
// referrer-bearing requests — when a feed page fires dozens of avatar loads at
// once, a random subset gets throttled (HTTP 429) and the browser shows its
// broken-image icon. Dropping the referrer makes those CDNs serve the images.
// If a load still fails we render a neutral placeholder (optionally an initial)
// instead of the broken-image icon.
export default function Avatar({
src,
alt = "",
fallback,
className = "",
}: {
src?: string | null;
alt?: string;
fallback?: string; // a name/title; its first letter is shown when there's no image
className?: string;
}) {
const [failed, setFailed] = useState(false);
useEffect(() => setFailed(false), [src]);
if (src && !failed) {
return (
<img
src={src}
alt={alt}
loading="lazy"
referrerPolicy="no-referrer"
onError={() => setFailed(true)}
className={className}
/>
);
}
const initial = fallback?.trim()?.[0]?.toUpperCase() ?? "";
return (
<div
className={`bg-card grid place-items-center font-semibold text-muted select-none ${className}`}
aria-hidden
>
{initial}
</div>
);
}

View file

@ -16,6 +16,7 @@ import { api, type ManagedChannel, type Tag } from "../lib/api";
import { formatEta } from "../lib/format"; import { formatEta } from "../lib/format";
import { notify } from "../lib/notifications"; import { notify } from "../lib/notifications";
import Tooltip from "./Tooltip"; import Tooltip from "./Tooltip";
import Avatar from "./Avatar";
export default function Channels({ export default function Channels({
canWrite, canWrite,
@ -332,11 +333,11 @@ function ChannelRow({
</div> </div>
</Tooltip> </Tooltip>
{c.thumbnail_url ? ( <Avatar
<img src={c.thumbnail_url} alt="" className="w-10 h-10 rounded-full shrink-0" /> src={c.thumbnail_url}
) : ( fallback={c.title ?? ""}
<div className="w-10 h-10 rounded-full bg-card shrink-0" /> className="w-10 h-10 rounded-full shrink-0"
)} />
<div className="min-w-0 flex-1"> <div className="min-w-0 flex-1">
<button onClick={onView} className="text-sm font-semibold truncate hover:text-accent block max-w-full text-left"> <button onClick={onView} className="text-sm font-semibold truncate hover:text-accent block max-w-full text-left">

View file

@ -4,6 +4,7 @@ import type { FeedFilters, Me } from "../lib/api";
import type { Page } from "../lib/urlState"; import type { Page } from "../lib/urlState";
import SyncStatus from "./SyncStatus"; import SyncStatus from "./SyncStatus";
import NotificationCenter from "./NotificationCenter"; import NotificationCenter from "./NotificationCenter";
import AvatarImg from "./Avatar";
export default function Header({ export default function Header({
me, me,
@ -70,12 +71,12 @@ export default function Header({
} }
function Avatar({ me, className = "" }: { me: Me; className?: string }) { function Avatar({ me, className = "" }: { me: Me; className?: string }) {
return me.avatar_url ? ( return (
<img src={me.avatar_url} alt="" className={`rounded-full ${className}`} /> <AvatarImg
) : ( src={me.avatar_url}
<div className={`rounded-full bg-card grid place-items-center font-semibold ${className}`}> fallback={me.display_name ?? me.email}
{(me.display_name?.[0] ?? me.email[0] ?? "?").toUpperCase()} className={`rounded-full ${className}`}
</div> />
); );
} }

View file

@ -2,6 +2,7 @@ import { useEffect, useRef, useState, type ReactNode } from "react";
import { createPortal } from "react-dom"; import { createPortal } from "react-dom";
import { useQuery } from "@tanstack/react-query"; import { useQuery } from "@tanstack/react-query";
import { ArrowLeft, Check, CheckCheck, X } from "lucide-react"; import { ArrowLeft, Check, CheckCheck, X } from "lucide-react";
import Avatar from "./Avatar";
import { api, type Video } from "../lib/api"; import { api, type Video } from "../lib/api";
import { formatDuration, formatViews, relativeTime } from "../lib/format"; import { formatDuration, formatViews, relativeTime } from "../lib/format";
@ -454,10 +455,10 @@ export default function PlayerModal({
When navigated to a linked video we only have its author (from the player), When navigated to a linked video we only have its author (from the player),
so we show that as plain text and hide feed-video-specific bits. */} so we show that as plain text and hide feed-video-specific bits. */}
<div className="flex items-center gap-3 mt-3"> <div className="flex items-center gap-3 mt-3">
{!navigated && video.channel_thumbnail && ( {!navigated && (
<img <Avatar
src={video.channel_thumbnail} src={video.channel_thumbnail}
alt="" fallback={video.channel_title ?? ""}
className="w-9 h-9 rounded-full shrink-0" className="w-9 h-9 rounded-full shrink-0"
/> />
)} )}

View file

@ -4,6 +4,7 @@ import { Bell, Check, History, Monitor, Pause, Play, RefreshCw, User, UserPlus,
import { SCHEMES, type Scheme, type ThemePrefs } from "../lib/theme"; import { SCHEMES, type Scheme, type ThemePrefs } from "../lib/theme";
import { api, type Invite, type Me } from "../lib/api"; import { api, type Invite, type Me } from "../lib/api";
import { formatEta, quotaActionLabel } from "../lib/format"; import { formatEta, quotaActionLabel } from "../lib/format";
import Avatar from "./Avatar";
import { import {
configureNotifications, configureNotifications,
getNotifSettings, getNotifSettings,
@ -468,13 +469,11 @@ function Account({ me }: { me: Me }) {
<> <>
<Section title="Account"> <Section title="Account">
<div className="flex items-center gap-3 mb-3"> <div className="flex items-center gap-3 mb-3">
{me.avatar_url ? ( <Avatar
<img src={me.avatar_url} alt="" className="w-12 h-12 rounded-full" /> src={me.avatar_url}
) : ( fallback={me.display_name ?? me.email}
<div className="w-12 h-12 rounded-full bg-card grid place-items-center font-semibold"> className="w-12 h-12 rounded-full"
{(me.display_name?.[0] ?? me.email[0] ?? "?").toUpperCase()} />
</div>
)}
<div className="min-w-0"> <div className="min-w-0">
<div className="font-semibold truncate">{me.display_name ?? me.email.split("@")[0]}</div> <div className="font-semibold truncate">{me.display_name ?? me.email.split("@")[0]}</div>
<div className="text-xs text-muted truncate">{me.email}</div> <div className="text-xs text-muted truncate">{me.email}</div>

View file

@ -1,4 +1,5 @@
import { Bookmark, Check, CheckCheck, Eye, EyeOff, ListFilter } from "lucide-react"; import { Bookmark, Check, CheckCheck, Eye, EyeOff, ListFilter } from "lucide-react";
import Avatar from "./Avatar";
import clsx from "clsx"; import clsx from "clsx";
import type { Video } from "../lib/api"; import type { Video } from "../lib/api";
import { formatDuration, formatViews, relativeTime } from "../lib/format"; import { formatDuration, formatViews, relativeTime } from "../lib/format";
@ -203,14 +204,11 @@ export default function VideoCard({
> >
<Thumb video={video} className="aspect-video" onOpen={onOpen} /> <Thumb video={video} className="aspect-video" onOpen={onOpen} />
<div className="flex gap-3 mt-2.5 px-0.5 pb-0.5"> <div className="flex gap-3 mt-2.5 px-0.5 pb-0.5">
{video.channel_thumbnail && ( <Avatar
<img src={video.channel_thumbnail}
src={video.channel_thumbnail} fallback={video.channel_title ?? ""}
alt="" className="w-9 h-9 rounded-full shrink-0 mt-0.5"
loading="lazy" />
className="w-9 h-9 rounded-full shrink-0 mt-0.5"
/>
)}
<div className="min-w-0 flex-1"> <div className="min-w-0 flex-1">
{title} {title}
<a <a