📖 Scenario: You are building a simple website using Astro. Astro is known for its great performance because it only sends the necessary HTML and minimal JavaScript to the browser.
🎯 Goal: Build a small Astro component that shows a list of features explaining why Astro is fast. You will create the data, configure a threshold for feature importance, filter the features, and render the final list.
📋 What You'll Learn
Create an array of feature objects with exact keys and values
Add a configuration variable to filter important features
Use array filtering to select features above the importance threshold
Render the filtered features in an Astro component
💡 Why This Matters
🌍 Real World
Astro is used to build fast websites by sending minimal JavaScript to browsers. Filtering and rendering data efficiently helps keep pages lightweight and quick.
💼 Career
Understanding how to manage data and render components in Astro is valuable for frontend developers focused on performance and modern web frameworks.
Progress0 / 4 steps
1
Create the features data array
Create a constant array called features with these exact objects: { name: 'Partial Hydration', importance: 9 }, { name: 'Zero JavaScript by default', importance: 10 }, { name: 'Server-side rendering', importance: 8 }, { name: 'Island architecture', importance: 7 }
Astro
Hint
Use const to create an array of objects with name and importance keys.
2
Add importance threshold configuration
Create a constant called importanceThreshold and set it to 8 to filter important features
Astro
Hint
Use const importanceThreshold = 8; to set the filter value.
3
Filter features by importance
Create a constant called importantFeatures by filtering features to include only objects where importance is greater than or equal to importanceThreshold
Astro
Hint
Use features.filter() with a function checking feature.importance >= importanceThreshold.
4
Render the important features list in Astro
In an Astro component, use the importantFeatures array to render an unordered list (<ul>) where each feature's name is inside a list item (<li>)
Astro
Hint
Use Astro's JSX-like syntax to map importantFeatures to <li> elements inside a <ul>.
Practice
(1/5)
1. Why does Astro send less code to the browser compared to other frameworks?
easy
A. Because it uses server-side rendering only without any client code
B. Because it compiles code into a single large JavaScript file
C. Because it only sends the HTML and CSS needed, not all JavaScript by default
D. Because it disables CSS to reduce code size
Solution
Step 1: Understand Astro's code sending strategy
Astro sends only the HTML and CSS needed for the page, avoiding unnecessary JavaScript by default.
Step 2: Compare with other options
Other frameworks often send all JavaScript upfront or disable CSS, which is not true for Astro.
Final Answer:
Because it only sends the HTML and CSS needed, not all JavaScript by default -> Option C
Quick Check:
Less code sent = only needed HTML/CSS sent [OK]
Hint: Astro sends minimal code by default, mainly HTML and CSS [OK]
Common Mistakes:
Thinking Astro sends all JavaScript upfront
Believing Astro disables CSS to reduce size
Confusing Astro with frameworks that bundle all code
2. Which of the following is the correct way to load JavaScript only when needed in Astro?
easy
A. ...
B.
C.
D. ...
Solution
Step 1: Identify Astro's client directives
Astro uses client directives like client:load or client:visible to load JavaScript only when needed.
Step 2: Match correct syntax
<MyComponent client:visible>...</MyComponent> is the correct way to load JS when the component becomes visible.
Final Answer:
<MyComponent client:visible>...</MyComponent> -> Option A
Quick Check:
Use client directives to load JS only when needed [OK]
Hint: Look for components with client:visible directive to defer JS [OK]
Common Mistakes:
Using plain <script> tags without defer or client directives
Confusing import inside <script> as lazy loading
Using client:always which loads JS immediately
3. Given this Astro component snippet:
<MyWidget client:idle />
What happens to the JavaScript for MyWidget?
medium
A. It loads only when the browser is idle
B. It loads immediately when the page loads
C. It never loads, only HTML is sent
D. It loads only when the user clicks the widget
Solution
Step 1: Understand client:idle directive
The client:idle directive tells Astro to load the JavaScript when the browser is idle, not immediately.
Step 2: Compare other options
Loading immediately is client:load, never loading is incorrect, and loading on click is client:click.
Final Answer:
It loads only when the browser is idle -> Option A
Quick Check:
client:idle means load JS when browser is idle [OK]
Hint: client:idle defers JS until browser is free [OK]
Common Mistakes:
Assuming client:idle loads JS immediately
Confusing client:idle with client:click
Thinking JS never loads with client:idle
4. You wrote this Astro code:
<MyComponent client:load />
But the JavaScript never loads on the client. What is the likely problem?
medium
A. Astro does not support client-side JavaScript
B. client:load is not a valid directive in Astro
C. You need to add defer attribute to the script tag
D. You forgot to import MyComponent in the Astro file
Solution
Step 1: Check if MyComponent is imported
Astro requires components to be imported before usage; missing import means no JS bundle is generated.
Step 2: Validate client:load directive
client:load is a valid directive, so the issue is not the directive itself.
Final Answer:
You forgot to import MyComponent in the Astro file -> Option D
Quick Check:
Missing import means JS won't load [OK]
Hint: Always import components before using client directives [OK]
Common Mistakes:
Thinking client:load is invalid
Assuming Astro disables client JS
Adding defer attribute manually instead of using directives
5. You want to build a blog with Astro that loads JavaScript only for interactive widgets when visible, and sends static HTML for the rest. Which approach best achieves this?
hard
A. Use <script> tags with defer for all widgets
B. Use <MyWidget client:visible /> for widgets and normal Astro components for static content
C. Disable JavaScript globally and use only server rendering
D. Wrap all components in <AstroIsland client:load> to load JS immediately
Solution
Step 1: Identify how to load JS only when widgets are visible
The client:visible directive loads JavaScript only when the widget scrolls into view, saving resources.
Step 2: Use normal Astro components for static content
Static content can be rendered as plain HTML without JavaScript, improving performance.
Final Answer:
Use <MyWidget client:visible /> for widgets and normal Astro components for static content -> Option B
Quick Check:
client:visible loads JS on visibility, static content stays HTML [OK]
Hint: Use client:visible for widgets, normal components for static parts [OK]
Common Mistakes:
Loading all JS immediately wastes resources
Disabling JS globally breaks interactivity
Using plain <script> tags misses Astro's lazy loading
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.