fix: make image loading more robust
All checks were successful
Deploy to SFTP Server / build (push) Successful in 6m38s

This commit is contained in:
Max Richter
2025-10-22 14:30:49 +02:00
parent 0db489269b
commit 7048db9d76

View File

@@ -2,6 +2,7 @@
import type { ImageMetadata } from "astro"; import type { ImageMetadata } from "astro";
import { Picture as AstroImage } from "astro:assets"; import { Picture as AstroImage } from "astro:assets";
import { generateThumbHash, getExifData } from "@helpers/image"; import { generateThumbHash, getExifData } from "@helpers/image";
import sharp from "sharp";
interface Props { interface Props {
src: ImageMetadata & { fsPath?: string }; src: ImageMetadata & { fsPath?: string };
alt: string; alt: string;
@@ -16,12 +17,12 @@ interface Props {
async function checkImage(src: string) { async function checkImage(src: string) {
try { try {
if (src.startsWith("/@fs") || src.startsWith("/_astro")) return true; if (src.startsWith("/@fs") || src.startsWith("/_astro")) return true;
const res = await fetch(src); const res = await sharp(src).metadata();
if (res.ok) { if (res.format) {
return true; return true;
}else { }else {
console.log("Failed to fetch: ", src); console.log("Failed to fetch: ", src);
} }
return false; return false;
} catch (err) { } catch (err) {
console.log("Failed to fetch: ", src); console.log("Failed to fetch: ", src);