Email for professionals and teams
improve profile image loading
Hieu Nguyen (Jack)f9839770f468
diff --git a/src/components/progressive-avatar-image-utils.ts b/src/components/progressive-avatar-image-utils.tsindex 5ca30fc..11d135d 100644--- a/src/components/progressive-avatar-image-utils.ts+++ b/src/components/progressive-avatar-image-utils.ts@@ -3,3 +3,45 @@ export function getAvatarPreviewUrl(src: string): string {url.searchParams.set("variant", "preview");return `${url.pathname}${url.search}`;}++const LOADED_STORAGE_KEY = "mailflare:avatars-loaded";+const MAX_REMEMBERED = 200;+/** Matches the Cache-Control the avatar route sends: versioned URLs are immutable, others live five minutes. */+const VERSIONED_TTL_MS = 30 * 24 * 60 * 60 * 1000;+const UNVERSIONED_TTL_MS = 5 * 60 * 1000;++let loadedFull: Map<string, number> | null = null;++function readLoaded(): Map<string, number> {+ if (loadedFull) return loadedFull;+ loadedFull = new Map();+ try {+ const stored = JSON.parse(window.localStorage.getItem(LOADED_STORAGE_KEY) || "[]") as Array<[string, number]>;+ const now = Date.now();+ for (const [src, expires] of stored) if (typeof src === "string" && expires > now) loadedFull.set(src, expires);+ } catch {+ // Storage can be unavailable or hold unreadable data; fall back to this page's memory.+ }+ return loadedFull;+}++/** Whether the full-size avatar finished loading before and should still be in the browser cache. */+export function wasAvatarLoaded(src: string): boolean {+ if (typeof window === "undefined") return false;+ const expires = readLoaded().get(src);+ return expires !== undefined && expires > Date.now();+}++export function rememberAvatarLoaded(src: string): void {+ if (typeof window === "undefined") return;+ const loaded = readLoaded();+ const versioned = new URL(src, "http://localhost").searchParams.has("v");+ loaded.delete(src);+ loaded.set(src, Date.now() + (versioned ? VERSIONED_TTL_MS : UNVERSIONED_TTL_MS));+ while (loaded.size > MAX_REMEMBERED) loaded.delete(loaded.keys().next().value as string);+ try {+ window.localStorage.setItem(LOADED_STORAGE_KEY, JSON.stringify([...loaded]));+ } catch {+ // Remembering across reloads is optional.+ }+}diff --git a/src/components/progressive-avatar-image.tsx b/src/components/progressive-avatar-image.tsxindex 1fd1e73..45c6bf6 100644--- a/src/components/progressive-avatar-image.tsx+++ b/src/components/progressive-avatar-image.tsx@@ -1,18 +1,24 @@"use client";-import { useEffect, useRef, useState } from "react";+import { useEffect, useLayoutEffect, useRef, useState } from "react";import type { ProgressiveAvatarImageProps } from "./progressive-avatar-image-types";-import { getAvatarPreviewUrl } from "./progressive-avatar-image-utils";+import { getAvatarPreviewUrl, rememberAvatarLoaded, wasAvatarLoaded } from "./progressive-avatar-image-utils";export function ProgressiveAvatarImage({ src, alt, className, onError }: ProgressiveAvatarImageProps) {const [previewReadyFor, setPreviewReadyFor] = useState<string | null>(null);const [previewFailedFor, setPreviewFailedFor] = useState<string | null>(null);const [fullReadyFor, setFullReadyFor] = useState<string | null>(null);+ // Set after mount so server and first client render agree; the effect below then skips the preview.+ const [cachedFor, setCachedFor] = useState<string | null>(null);const [failedFor, setFailedFor] = useState<string | null>(null);const imageRef = useRef<HTMLImageElement>(null);const onErrorRef = useRef(onError);onErrorRef.current = onError;- const showFull = previewFailedFor === src || fullReadyFor === src;+ const showFull = previewFailedFor === src || fullReadyFor === src || cachedFor === src;++ useLayoutEffect(() => {+ if (wasAvatarLoaded(src)) setCachedFor(src);+ }, [src]);useEffect(() => {const image = imageRef.current;@@ -26,7 +32,9 @@ export function ProgressiveAvatarImage({ src, alt, className, onError }: Progreslet cancelled = false;const image = new Image();image.onload = () => {- if (!cancelled) setFullReadyFor(src);+ if (cancelled) return;+ rememberAvatarLoaded(src);+ setFullReadyFor(src);};image.onerror = () => {if (!cancelled) {@@ -54,6 +62,7 @@ export function ProgressiveAvatarImage({ src, alt, className, onError }: Progresstyle={showFull ? undefined : { filter: "blur(4px)" }}onLoad={() => {if (!showFull) setPreviewReadyFor(src);+ else rememberAvatarLoaded(src);}}onError={() => {if (showFull) {