feat: implement automatic retry mechanism for image loading in CoverClient, enhancing error handling and user experience
This commit is contained in:
@@ -20,10 +20,22 @@ export const CoverClient = ({
|
|||||||
}: CoverClientProps) => {
|
}: CoverClientProps) => {
|
||||||
const [imageError, setImageError] = useState(false);
|
const [imageError, setImageError] = useState(false);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [retryCount, setRetryCount] = useState(0);
|
||||||
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
|
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
|
|
||||||
|
// Reset état quand l'URL change
|
||||||
|
useEffect(() => {
|
||||||
|
setImageError(false);
|
||||||
|
setIsLoading(true);
|
||||||
|
setRetryCount(0);
|
||||||
|
}, [imageUrl]);
|
||||||
|
|
||||||
// Timeout de sécurité : si l'image ne se charge pas en 30 secondes, on arrête le loading
|
// Timeout de sécurité : si l'image ne se charge pas en 30 secondes, on arrête le loading
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (timeoutRef.current) {
|
||||||
|
clearTimeout(timeoutRef.current);
|
||||||
|
}
|
||||||
|
|
||||||
timeoutRef.current = setTimeout(() => {
|
timeoutRef.current = setTimeout(() => {
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
@@ -36,13 +48,27 @@ export const CoverClient = ({
|
|||||||
clearTimeout(timeoutRef.current);
|
clearTimeout(timeoutRef.current);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}, [imageUrl, isLoading]);
|
}, [imageUrl, isLoading, retryCount]);
|
||||||
|
|
||||||
|
// Si en erreur, réessayer automatiquement après 2 secondes
|
||||||
|
useEffect(() => {
|
||||||
|
if (imageError) {
|
||||||
|
const timer = setTimeout(() => {
|
||||||
|
setImageError(false);
|
||||||
|
setIsLoading(true);
|
||||||
|
setRetryCount(prev => prev + 1);
|
||||||
|
}, 2000);
|
||||||
|
|
||||||
|
return () => clearTimeout(timer);
|
||||||
|
}
|
||||||
|
}, [imageError]);
|
||||||
|
|
||||||
const handleLoad = () => {
|
const handleLoad = () => {
|
||||||
if (timeoutRef.current) {
|
if (timeoutRef.current) {
|
||||||
clearTimeout(timeoutRef.current);
|
clearTimeout(timeoutRef.current);
|
||||||
}
|
}
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
|
setImageError(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleError = () => {
|
const handleError = () => {
|
||||||
@@ -53,6 +79,11 @@ export const CoverClient = ({
|
|||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Ajouter un timestamp pour forcer le rechargement en cas de retry
|
||||||
|
const imageUrlWithRetry = retryCount > 0
|
||||||
|
? `${imageUrl}${imageUrl.includes('?') ? '&' : '?'}retry=${retryCount}`
|
||||||
|
: imageUrl;
|
||||||
|
|
||||||
if (imageError) {
|
if (imageError) {
|
||||||
return (
|
return (
|
||||||
<div className="w-full h-full flex items-center justify-center bg-muted/80 backdrop-blur-md rounded-lg">
|
<div className="w-full h-full flex items-center justify-center bg-muted/80 backdrop-blur-md rounded-lg">
|
||||||
@@ -66,7 +97,7 @@ export const CoverClient = ({
|
|||||||
<ImageLoader isLoading={isLoading} />
|
<ImageLoader isLoading={isLoading} />
|
||||||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||||
<img
|
<img
|
||||||
src={imageUrl}
|
src={imageUrlWithRetry}
|
||||||
alt={alt}
|
alt={alt}
|
||||||
loading="lazy"
|
loading="lazy"
|
||||||
className={cn(
|
className={cn(
|
||||||
|
|||||||
Reference in New Issue
Block a user