0
0
Astroframework~10 mins

When to hydrate vs keep static in Astro - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - When to hydrate vs keep static
Start: Astro component rendered
Decide: Does component need interactivity?
Hydrate component
Browser runs JS
User interacts
Update UI
End
The flow shows deciding if a component needs interactivity to choose hydration or static rendering.
Execution Sample
Astro
<MyButton client:load />
<MyHeader />
Hydrates MyButton for interactivity; MyHeader stays static without JS.
Execution Table
StepComponentHydration DecisionAction TakenResult
1MyButtonNeeds interactivityHydrate with client:loadJS loads, button interactive
2MyHeaderNo interactivity neededKeep staticNo JS, static header rendered
3User clicks MyButtonEvent triggersJS handles clickUI updates dynamically
4User views MyHeaderNo JSStatic content shownFaster load, no CPU used
5End--Hydration only for interactive components
💡 All components processed; hydration applied only where needed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
MyButton hydratedfalsetruetruetruetrue
MyHeader hydratedfalsefalsefalsefalsefalse
JS loadedfalsetruetruetruetrue
UI interactivefalsetruetruetruetrue
Key Moments - 3 Insights
Why does MyHeader not load JavaScript?
Because MyHeader is kept static (see execution_table step 2), it does not need JS for interactivity.
What triggers hydration for MyButton?
The client:load directive tells Astro to hydrate MyButton on page load (execution_table step 1).
Does hydration happen for all components automatically?
No, only components marked with hydration directives like client:load are hydrated (see execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the hydration decision for MyHeader at step 2?
AKeep static
BHydrate with client:load
CHydrate with client:idle
DHydrate with client:visible
💡 Hint
Check the 'Hydration Decision' column at step 2 in the execution_table.
At which step does the user interaction with MyButton trigger a UI update?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for 'User clicks MyButton' and 'UI updates dynamically' in the execution_table.
If MyButton did not have client:load, what would change in the variable tracker after step 1?
AMyButton hydrated would be false
BMyButton hydrated would be true
CJS loaded would be true
DUI interactive would be true
💡 Hint
Refer to the 'MyButton hydrated' row in variable_tracker after step 1.
Concept Snapshot
Astro components can be static or hydrated.
Use hydration directives (client:load, client:idle, etc.) to add interactivity.
Static components load faster with no JS.
Hydrated components run JS for user interaction.
Choose hydration only when needed to optimize performance.
Full Transcript
This visual execution shows how Astro decides whether to hydrate a component or keep it static. First, the Astro component renders. Then it checks if the component needs interactivity. If yes, it hydrates the component using directives like client:load, loading JavaScript so the user can interact with it. If no, it keeps the component static with no JavaScript, improving load speed and reducing CPU use. For example, MyButton is hydrated and interactive, while MyHeader remains static. User clicks on MyButton trigger JavaScript events and UI updates. This approach helps balance performance and interactivity by hydrating only necessary components.