Added retry

This commit is contained in:
2025-07-11 17:59:28 +03:30
parent 0c6160b756
commit 09772a423c

View File

@@ -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 }}