Added retry
This commit is contained in:
		@@ -58,5 +58,41 @@
 | 
			
		||||
 | 
			
		||||
</body>
 | 
			
		||||
 | 
			
		||||
<script>
 | 
			
		||||
    const img = document.querySelector('.cat-image');
 | 
			
		||||
    const link = document.getElementById('mastodonLink');
 | 
			
		||||
 | 
			
		||||
    img.onerror = () => {
 | 
			
		||||
        console.warn("failed to load the image - retrying...");
 | 
			
		||||
        fetch('/api/post/random')
 | 
			
		||||
            .then(response => {
 | 
			
		||||
                if (!response.ok) throw new Error("Network error");
 | 
			
		||||
                return response.json();
 | 
			
		||||
            })
 | 
			
		||||
            .then(data => {
 | 
			
		||||
                if (!data || !data.account || !data.media_attachments || data.media_attachments.length === 0) {
 | 
			
		||||
                    throw new Error("Invalid data format");
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                let imageUrl = data.media_attachments[0].remote_url;
 | 
			
		||||
                if (imageUrl) {
 | 
			
		||||
                    imageUrl = imageUrl.replace('/original/', '/small/');
 | 
			
		||||
                } else if (data.media_attachments[0].preview_url) {
 | 
			
		||||
                    imageUrl = data.media_attachments[0].preview_url;
 | 
			
		||||
                } else {
 | 
			
		||||
                    throw new Error("No image URL found");
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                img.src = imageUrl;
 | 
			
		||||
                link.href = data.url;
 | 
			
		||||
            })
 | 
			
		||||
            .catch(error => {
 | 
			
		||||
                console.error("Failed to load fallback image:", error);
 | 
			
		||||
                img.src = "https://s6.uupload.ir/files/im_sad_0zg9.jpg";
 | 
			
		||||
            });
 | 
			
		||||
    };
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
</html>
 | 
			
		||||
{{ end }}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user