Discover how to make your website interactive exactly when users need it, without headaches or delays!
Why client:load directive in Astro? - Purpose & Use Cases
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.
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 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.
window.onload = () => { initGallery(); }<MyGallery client:load />
You can easily add interactive features that start working only after the page is fully ready, improving user experience and performance.
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.
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.