Created
December 20, 2024 05:51
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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