master
1import { inferRemoteSize } from 'astro:assets';
2
3const cache = new Map<string, { width: number; height: number }>();
4
5export async function getCachedRemoteImageSize(
6 src: string
7): Promise<{ width: number; height: number } | null> {
8 if (cache.has(src)) {
9 return cache.get(src)!;
10 }
11
12 try {
13 const metadata = await inferRemoteSize(src);
14 const dimensions = { width: metadata.width, height: metadata.height };
15
16 cache.set(src, dimensions);
17
18 return dimensions;
19 } catch (error) {
20 console.warn(
21 `[WARN] Could not infer size for image "${src}": ${(error as Error).message}.`
22 );
23 return null;
24 }
25}