Skip to content

Instantly share code, notes, and snippets.

@vnphanquang
Created December 20, 2024 05:51
Show Gist options
  • Save vnphanquang/c8c440e0f77625f5f14f8532e5869b2e to your computer and use it in GitHub Desktop.
Save vnphanquang/c8c440e0f77625f5f14f8532e5869b2e to your computer and use it in GitHub Desktop.
Get the enhanced image source for imgs loaded via `@sveltejs/enhanced-img` for preloading
import type { Picture } from 'vite-imagetools';
/**
* Get the enhanced image source for imgs loaded via `@sveltejs/enhanced-img`,
* often for preloading. Use sparingly!
*/
export function getEnhancedImgUrls(picture: Picture, type: 'avif' | 'png' | 'webp'): string[] {
if (!(type in picture.sources)) return [];
const srcset = picture.sources[type];
const urls: string[] = [];
for (const src of srcset.split(',')) {
const [path] = src.trim().split(' ');
if (!path) continue;
urls.push(path);
};
return urls;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment