0
0
Astroframework~3 mins

When to hydrate vs keep static in Astro - When to Use Which

Choose your learning style9 modes available
The Big Idea

Discover how to make your website lightning fast by smartly choosing when to add JavaScript!

The Scenario

Imagine building a website where every interactive button and form needs JavaScript to work. You try adding scripts manually to each part, but it quickly becomes a mess to manage and slows down your page.

The Problem

Manually adding JavaScript to every element means loading lots of code even when users don't need it. This makes pages slow to load and can confuse browsers, causing bugs and poor user experience.

The Solution

Astro lets you choose when to add JavaScript to your components: hydrate them only when needed for interactivity, or keep them static if they don't require scripts. This keeps pages fast and smooth.

Before vs After
Before
Add scripts to every component regardless of need, causing slow load times.
After
<Component client:load /> to hydrate only interactive parts, <Component /> to keep static parts without extra scripts.
What It Enables

This choice lets you build fast websites that only use JavaScript where it truly matters, improving speed and user experience.

Real Life Example

A blog page where static articles load instantly, and only the comment form hydrates JavaScript when a user wants to interact.

Key Takeaways

Manually adding JavaScript everywhere slows down pages.

Hydration means adding scripts only to interactive parts.

Keeping components static saves resources and speeds up loading.