· 1 min read
Self-hosted avatar with enhanced-img
Downloaded my GitHub avatar (460px JPEG, 24 KB) into src/lib/assets/avatar.jpg, rather than linking to GitHub.
What we did
- Downloaded my GitHub avatar (460px JPEG, 24 KB) into
src/lib/assets/avatar.jpg, rather than linking to GitHub. - Imported it with
?enhancedand rendered it with<enhanced:img>in Hero.
What I learned
?enhancedproduces aPictureobject. At build time it creates AVIF, WebP and JPEG versions at 1x and 2x, and the browser picks the best format it supports.- It also sets
widthandheight, so the layout doesn't shift while the image loads. - When the image is a prop, the type is
Picturefrom@sveltejs/enhanced-img. - For a small image with a fixed display size, density descriptors (
1x,2x) are right, and browsers ignoresizeswith them.sizesonly matters with width descriptors (400w). - The photo has
alt=""because my name is right next to it. Repeating it would be noise for screen reader users.
Problems & fixes
- I first added a
sizesattribute. The output used1x/2x, sosizesdid nothing. I removed it.
Links
Next
- Use enhanced-img for project screenshots in Phase 8.