0
0
Astroframework~3 mins

Why client:load directive in Astro? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how to make your website interactive exactly when users need it, without headaches or delays!

The Scenario

Imagine building a website where you want some interactive parts to work only after the page fully loads, like a photo gallery or a chat widget.

You try to add JavaScript manually to run after the page loads, but it's tricky to get the timing right and keep your site fast.

The Problem

Manually controlling when scripts run is hard and error-prone.

If you run scripts too early, they break because the page isn't ready.

If you run them too late, users see delays or blank spots.

Also, managing these scripts separately makes your code messy and slow.

The Solution

The client:load directive in Astro runs your interactive code exactly when the page finishes loading.

This means your components become interactive at the right time without extra setup.

Your site stays fast and clean because Astro handles the timing for you.

Before vs After
Before
window.onload = () => { initGallery(); }
After
<MyGallery client:load />
What It Enables

You can easily add interactive features that start working only after the page is fully ready, improving user experience and performance.

Real Life Example

On a travel blog, the photo slideshow loads only after the page is fully visible, so readers see the text immediately and the slideshow starts smoothly without delays.

Key Takeaways

Manual script timing is tricky and can cause errors.

client:load runs code right after page load automatically.

This keeps your site fast, clean, and interactive at the right moment.