DEV Community

Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on

Astro’s Client Directives: When and Where to Use Each

Hello, I'm Maneshwar. I'm working on FreeDevTools online currently building *one place for all dev tools, cheat codes, and TLDRs* — a free, open-source hub where developers can quickly find and use tools without any hassle of searching all over the internet.

Astro’s core principle is to ship less JavaScript to the browser.

By default, components from UI frameworks (like React, Svelte, or Vue) are rendered as static HTML — no hydration, no JavaScript.

But when interactivity is needed, Astro gives precise control through client directives.

These directives determine when and how a component’s JavaScript should load and hydrate on the client. Using them effectively can drastically improve performance and user experience.

1. client:load

When to use: For components that need to be interactive immediately after the page loads.
Examples: Buttons, navigation menus, or hero section sliders.

How it works:
The component’s JavaScript loads and hydrates as soon as the page starts loading — highest priority hydration.

<BuyButton client:load />
Enter fullscreen mode Exit fullscreen mode

Benefit: Instant interactivity for key UI components.
Drawback: Can increase initial page load time if overused.

2. client:idle

When to use: For UI components that can wait until after the initial load.
Examples: Toggle buttons, accordions, or interactive widgets below the hero section.

How it works:
Hydration happens once the browser fires requestIdleCallback, meaning the browser is done handling critical tasks.

<ShowHideButton client:idle />
Enter fullscreen mode Exit fullscreen mode

Benefit: Reduces render-blocking JS and improves Core Web Vitals.
Optional: Control hydration timing precisely with a timeout.

<ShowHideButton client:idle={{timeout: 500}} />
Enter fullscreen mode Exit fullscreen mode

Use case: Useful when you want the component to hydrate even if the browser never goes idle within a given timeframe.

3. client:visible

When to use: For components that appear far down the page or aren’t always seen.
Examples: Image galleries, carousels, or heavy UI widgets “below the fold.”

How it works:
Hydration happens only when the component enters the user’s viewport using IntersectionObserver.

<HeavyImageCarousel client:visible />
Enter fullscreen mode Exit fullscreen mode

Optional root margin:

<HeavyImageCarousel client:visible={{rootMargin: "200px"}} />
Enter fullscreen mode Exit fullscreen mode

Benefit: Saves bandwidth by avoiding hydration for unseen elements, reduces layout shift, and improves scroll performance.

4. client:media

When to use: For components that are needed only on certain screen sizes.
Examples: Mobile-only navigation toggles or responsive toolbars.

How it works:
Hydration triggers when a specified CSS media query condition is true.

<SidebarToggle client:media="(max-width: 50em)" />
Enter fullscreen mode Exit fullscreen mode

Benefit: Perfect for responsive UIs — loads components only when relevant.
Note: If the element is already hidden by CSS, client:visible might be simpler.

5. client:only

When to use: For components that should render entirely on the client — skipping SSR.
Examples: Apps that depend on browser APIs (like maps, editors, or chat widgets).

How it works:
No server-rendered HTML — Astro renders nothing until the client loads the component. You must specify the framework.

<SomeReactComponent client:only="react" />
<SomeVueComponent client:only="vue" />
Enter fullscreen mode Exit fullscreen mode

Benefit: Best for components that can’t be pre-rendered.
Drawback: Users see nothing until the client JavaScript loads.

Optional fallback content while loading:

<ClientComponent client:only="vue">
  <div slot="fallback">Loading...</div>
</ClientComponent>
Enter fullscreen mode Exit fullscreen mode

6. Custom Client Directives

From Astro 2.6 onwards, you can create your own custom client:* directives to define unique hydration behaviors.

Example: You could create a directive like client:scroll that hydrates components only after the user scrolls a certain distance.

See the addClientDirective API for implementation details.

Choosing the Right Directive

Directive Priority Best For When It Hydrates Typical Use
client:load High Essential UI On page load Navbar, Buy button
client:idle Medium Secondary UI When browser is idle Toggles, small widgets
client:visible Low Below-the-fold When in viewport Carousels, lazy components
client:media Low Responsive UIs When media query matches Mobile nav, sidebars
client:only Client-only apps On page load (no SSR) Maps, chat, editors

Final Thoughts

Astro’s client directives give full control over when interactivity starts.

The right balance between client:load, client:idle, and client:visible can make your site feel both fast and responsive.

For interactive-heavy pages, think of hydration as a budget — use it only where it adds real value.

FreeDevTools

I’ve been building for FreeDevTools.

A collection of UI/UX-focused tools crafted to simplify workflows, save time, and reduce friction in searching tools/materials.

Any feedback or contributors are welcome!

It’s online, open-source, and ready for anyone to use.

👉 Check it out: FreeDevTools
⭐ Star it on GitHub: freedevtools

Top comments (0)