Discover how Astro makes your website feel lightning fast by sending only what's needed, right when it's needed.
Why Astro excels at performance - The Real Reasons
Imagine building a website where every small change means loading a big chunk of JavaScript and waiting for the browser to do a lot of work before showing anything.
Loading all scripts and running them on the browser slows down the page, wastes data, and makes users wait longer. It feels like waiting for a slow elevator every time you want to see something new.
Astro sends only the essential HTML to the browser first and loads JavaScript only when needed. This means pages load faster and feel snappier, like taking the stairs instead of waiting for the elevator.
<div id="app">Loading...</div> <script src="big-app.js"></script>
<!-- Astro sends static HTML first -->
<div>Welcome!</div>
<!-- JavaScript loads only if user interacts -->Astro lets you build websites that load instantly and run smoothly, even on slow connections or old devices.
Think of a news site where you can start reading articles immediately without waiting for all the scripts to load in the background.
Manual full JavaScript loading slows down websites.
Astro sends minimal HTML first, delaying JavaScript.
This approach creates fast, smooth user experiences.
Practice
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 CQuick Check:
Less code sent = only needed HTML/CSS sent [OK]
- Thinking Astro sends all JavaScript upfront
- Believing Astro disables CSS to reduce size
- Confusing Astro with frameworks that bundle all code
Solution
Step 1: Identify Astro's client directives
Astro uses client directives likeclient:loadorclient:visibleto 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 AQuick Check:
Use client directives to load JS only when needed [OK]
- Using plain <script> tags without defer or client directives
- Confusing import inside <script> as lazy loading
- Using client:always which loads JS immediately
<MyWidget client:idle />
What happens to the JavaScript for
MyWidget?Solution
Step 1: Understand client:idle directive
Theclient:idledirective tells Astro to load the JavaScript when the browser is idle, not immediately.Step 2: Compare other options
Loading immediately isclient:load, never loading is incorrect, and loading on click isclient:click.Final Answer:
It loads only when the browser is idle -> Option AQuick Check:
client:idle means load JS when browser is idle [OK]
- Assuming client:idle loads JS immediately
- Confusing client:idle with client:click
- Thinking JS never loads with client:idle
<MyComponent client:load />
But the JavaScript never loads on the client. What is the likely problem?
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:loadis a valid directive, so the issue is not the directive itself.Final Answer:
You forgot to import MyComponent in the Astro file -> Option DQuick Check:
Missing import means JS won't load [OK]
- Thinking client:load is invalid
- Assuming Astro disables client JS
- Adding defer attribute manually instead of using directives
Solution
Step 1: Identify how to load JS only when widgets are visible
Theclient:visibledirective 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 BQuick Check:
client:visible loads JS on visibility, static content stays HTML [OK]
- Loading all JS immediately wastes resources
- Disabling JS globally breaks interactivity
- Using plain <script> tags misses Astro's lazy loading
Ready to prove it?
Pass the test and this pattern moves unknown
