What if your website could be lightning fast by sending no JavaScript at all until needed?
Why Astro ships zero JavaScript by default - The Real Reasons
Imagine building a website where every interactive feature requires you to write and load lots of JavaScript code, even for simple pages that mostly show static content.
Loading all that JavaScript slows down your site, wastes users' data, and can make pages feel sluggish, especially on slow connections or older devices.
Astro solves this by sending zero JavaScript to the browser by default, only adding scripts when absolutely needed, making sites faster and lighter.
Load full JavaScript bundle for every page, even static ones.Astro sends only HTML and CSS by default; JavaScript loads only for interactive parts.
This approach lets you build super fast websites that work well everywhere, saving bandwidth and improving user experience.
Think of a blog that mostly shows articles without interaction; Astro delivers just the content quickly without extra scripts slowing it down.
Loading unnecessary JavaScript hurts speed and user experience.
Astro ships zero JavaScript by default to keep sites fast and light.
JavaScript is added only when needed for interactivity.
Practice
Solution
Step 1: Understand Astro's default behavior
Astro sends no JavaScript to the browser unless explicitly added.Step 2: Reason why this is beneficial
Sending less JavaScript makes websites load faster and simpler.Final Answer:
To make websites faster by sending only necessary code -> Option BQuick Check:
Astro ships zero JS by default to improve speed [OK]
- Thinking Astro doesn't support JavaScript at all
- Believing JavaScript is disabled in browsers
- Assuming Astro forces CSS-only coding
Solution
Step 1: Recall Astro client directive syntax
Astro usesclient:loadto add JavaScript loading behavior.Step 2: Compare options to correct syntax
Only<MyComponent client:load />uses the correctclient:loadsyntax.Final Answer:
<MyComponent client:load /> -> Option CQuick Check:
Correct client directive syntax is client:load [OK]
- Swapping order of 'client' and 'load'
- Using hyphens instead of colon
- Omitting the colon entirely
<MyWidget client:idle />
What happens when the page loads?
Solution
Step 1: Understand the client:idle directive
Theclient:idledirective loads JavaScript when the browser is idle.Step 2: Determine behavior on page load
JavaScript does not load immediately but waits until the browser is idle.Final Answer:
JavaScript for MyWidget loads only when the browser is idle -> Option DQuick Check:
client:idle delays JS load until idle [OK]
- Assuming JS loads immediately on page load
- Confusing client:idle with client:load
- Thinking JS never loads
<MyButton client:load>Click</MyButton>
But the JavaScript is not loading. What is the likely error?
Solution
Step 1: Check client directive usage
Usingclient:loadrequires the component to have JavaScript to load.Step 2: Identify if component exports JS
IfMyButtonhas no JavaScript, the directive does nothing.Final Answer:
You used client:load on a component that does not export JavaScript -> Option AQuick Check:
client:load needs JS in component to work [OK]
- Thinking self-closing slash is mandatory for client directives
- Believing Astro disallows client directives
- Assuming separate script import is always needed
Solution
Step 1: Understand Astro's zero JS principle
Astro sends no JS unless explicitly added to components needing it.Step 2: Choose minimal JS loading
Addingclient:loadonly to the button loads JS just where needed.Step 3: Avoid global or unnecessary JS
Global JS or inline scripts increase bundle size and slow page.Final Answer:
Add client:load only to the button component and keep the rest JS-free -> Option AQuick Check:
Minimal JS on needed components fits Astro philosophy [OK]
- Adding JS globally instead of per component
- Using inline scripts without directives
- Applying client:visible to entire page unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
