Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Practical Astro
Lesson Plan
  1. Content Systems
    1. Introduction
    2. An import alias
    3. Using Markdown
    4. Markdown extended
    5. Markdown component
    6. Markdown component in frameworks
    7. Content collections made easy
    8. Basic blogs
    9. Blogs made easy
    10. Prev/Next navigation
    11. Pagination made easy
    12. Better tag pages
    13. Paginated tag pages
    14. Markdown layouts
    15. Partials
    16. JS over JSON Data
    17. Dynamic 404 pages
    18. Putting everything together
    19. Other useful systems
  2. Image Systems
    1. Introduction
    2. Images are complicated because of this
    3. Astro's Image component
    4. Astro's Picture component
    5. Improvements over Astro's Image and Picture Components
    6. Images in Markdown and MDX
    7. Images in framework components
    8. Background images
    9. Art Direction with Astro
    10. Videos without layout shifts
    11. GIF Replacements and background video
    12. SVGs and icons
    13. SVG backgrounds
    14. Adding noise to images
    15. Asset caching on GitHub Actions
    16. Putting everything together
    17. Other useful systems

Images in framework components

2m:30s

You can’t use Astro components inside React, Vue, Svelte, or other framework components. So Astro’s Image and Picture won’t work. Splendid Astro’s Image component won’t work either.

Component.svelte
<script>
// Cannot import any Astro stuff directly into frameworks
import { Image } from 'astro:assets'
</script>
<Image ... />

Inserting images into framework components

There are a few ways to insert images into framework components.

  1. Import the image (without optimization)
  2. Pass the image via a slot
  3. Pass the image via a prop

The second one is my personal favourite.

Import the image directly

You can import an image directly inside a framework component.

Component.svelte
<script>
import stars from '../assets/stars.png'
</script>
<img
src={stars.src}
width={stars.width}
height={stars.height}
alt="A starry night sky."
/>

Two important notes here:

  1. This image will not be optimized
  2. You must include width and height to stop layout shifts. Astro’s docs show this pattern but they left out width and height.

The image is not optimized because it doesn’t pass through Astro’s image pipeline. Because of this, I don’t consider this to be a viable option. But, it’s written here because some people think it’s passable.

On to the real solutions next.

Passing images through a slot

The best option is to build the Image component in your Astro file and pass it into your framework component through a slot. You can usually secure the right placement with a named slot.

index.astro
---
import { Image } from '@splendidlabz/astro'
import SvelteComponent from '@/components/YourComponent.svelte'
---
<SvelteComponent>
<Image slot="image" src="/src/assets/image.jpg" alt="..." />
</SvelteComponent>

Use this whenever you can.

Passing images through a prop

There’s a third option that’s rather cumbersome.

In this case, you can use Astro’s getImage utility to optimize the image, get the attributes, then pass it into your framework component.

index.astro
---
import { getImage } from 'astro:assets'
import SvelteComponent from '@/components/Component.svelte'
import hero from '../assets/hero.jpg'
const image = await getImage({
src: hero,
widths: [400, 800, 1200],
// ...other options
})
---
<SvelteComponent {image} alt="..." />

Inside the framework component, you need to unpack the attributes and write the img tag yourself.

Component.svelte
<script>
const { image, alt } = $props()
const { src, attributes, srcSet } = image
</script>
<img {...attributes} {src} {alt} srcset={srcSet.attribute} />

Now, Astro’s getImage contains the standard features built for Astro’s Image and Picture components. It doesn’t contain the improvements we made.

If you prefer this method, you can also use the img utility from Splendid Astro to include the improvements we’ve made.

index.astro
---
import { img } from '@splendidlabz/astro/utils'
import SvelteComponent from '@/components/Component.svelte'
const image = await img('/src/assets/hero.jpg', {
widths: [400, 800, 1200],
// ...other options
})
---
<SvelteComponent {image} alt="..." />

Code to copy

The best method — hands down — is to pass the image through a slot.

index.astro
---
import { Image } from '@splendidlabz/astro'
import SvelteComponent from '@/components/YourComponent.svelte'
---
<SvelteComponent>
<Image slot="image" src="/src/assets/image.jpg" alt="..." />
</SvelteComponent>