📖 Scenario: You are building a simple Astro website that shows a product image. To make the website faster and use less data, you want to optimize the image using Astro's built-in astro:assets feature.
🎯 Goal: Create an Astro component that imports an image file and uses astro:assets to optimize it. Then display the optimized image with a fixed width.
📋 What You'll Learn
Import an image file using astro:assets
Create a variable called optimizedImage to hold the optimized image
Set the image width to 300 pixels during optimization
Render the optimized image in an <img> tag with alt text
💡 Why This Matters
🌍 Real World
Optimizing images helps websites load faster and use less data, improving user experience especially on mobile devices.
💼 Career
Web developers often optimize images to improve site performance and SEO, making this skill valuable in frontend development roles.
Progress0 / 4 steps
1
Import the image file
Create a variable called productImage that imports the image file located at ../assets/product.jpg using import from astro:assets.
Astro
Hint
Use import productImage from 'astro:assets'; and then import the image file.
2
Optimize the image with fixed width
Create a variable called optimizedImage that calls productImage as a function with an object setting width to 300.
Astro
Hint
Call productImage as a function with { width: 300 } to optimize.
3
Render the optimized image in HTML
Add an <img> tag that uses optimizedImage.src as the src attribute and optimizedImage.width as the width attribute. Set the alt attribute to "Product image".
Astro
Hint
Use <img src={optimizedImage.src} width={optimizedImage.width} alt="Product image" /> to display the image.
4
Export the Astro component
Add export default before the component code to export it as the default Astro component.
Astro
Hint
Wrap the <img> in an arrow function and export it as default.
Practice
(1/5)
1. What is the main purpose of using the Image component from astro:assets in an Astro project?
easy
A. To automatically optimize images for size and format
B. To create image galleries manually
C. To write custom CSS for images
D. To upload images to a server
Solution
Step 1: Understand the role of astro:assets
The astro:assets package provides tools to optimize images automatically.
Step 2: Identify the Image component's function
The Image component serves images in optimized sizes and formats without manual work.
Final Answer:
To automatically optimize images for size and format -> Option A
Quick Check:
Image optimization = A [OK]
Hint: Remember: Image auto-optimizes images [OK]
Common Mistakes:
Thinking Image uploads images
Confusing styling with optimization
Assuming manual resizing is needed
2. Which of the following is the correct way to import the Image component from astro:assets in an Astro file?
easy
A. import Image from 'astro:assets';
B. import { Image } from 'astro:assets';
C. import Image from '@astro/image';
D. import { Image } from '@astro/image';
Solution
Step 1: Recall the official import syntax
The Image component is a named export from astro:assets, so it requires curly braces.
Step 2: Match the correct import statement
Only import { Image } from 'astro:assets'; uses named import and correct package name.
Final Answer:
import { Image } from 'astro:assets'; -> Option B
Quick Check:
Named import with braces = C [OK]
Hint: Use curly braces for named imports like Image [OK]
Common Mistakes:
Using default import without braces
Wrong package name '@astro/image'
Confusing default and named imports
3. Given the following Astro code snippet, what will be the rendered output in the browser?
B. A broken image link because the path is incorrect
C. An optimized image of width 400 pixels with alt text 'A mountain'
D. An image with original size ignoring width attribute
Solution
Step 1: Understand Image behavior with src and width
The Image component optimizes the image and respects the width prop to resize it.
Step 2: Check alt text and path usage
The alt text is provided correctly, and the relative path is valid for Astro assets.
Final Answer:
An optimized image of width 400 pixels with alt text 'A mountain' -> Option C
Quick Check:
Image optimization with width and alt = D [OK]
Hint: Width prop resizes image; alt improves accessibility [OK]
Common Mistakes:
Assuming width is ignored
Thinking relative paths break images
Confusing alt text with title
4. Identify the error in this Astro code using Image from astro:assets:
---
import { Image } from 'astro:assets';
const logo = '/images/logo.png';
---
medium
A. The src path should be relative, not absolute
B. Missing width or height props causes a runtime error
C. The alt attribute is required but missing
D. The import statement is incorrect
Solution
Step 1: Check the src path format
The Image component expects a relative path or imported asset, not an absolute URL path starting with '/'.
Step 2: Verify other props and import
The alt attribute is present, and the import is correct; width/height are optional but recommended.
Final Answer:
The src path should be relative, not absolute -> Option A
Quick Check:
Use relative paths for src = B [OK]
Hint: Use relative paths for images, not absolute URLs [OK]
Common Mistakes:
Using absolute paths starting with '/'
Assuming width is mandatory
Confusing import syntax
5. You want to serve an image with a blurred placeholder while it loads using astro:assets. Which of the following code snippets correctly implements this feature?