🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Components & Islands••By Komal Nakrani

Astro Client Directives: client:load vs idle vs visible

Astro client directives compared: how client:load, client:idle, client:visible, client:media and client:only hydrate islands, their options, and which to pick.

#Astro#Astro Client Directives#Hydration#Islands#Performance

Astro client directives decide when a framework component’s JavaScript loads in the browser. client:load hydrates immediately on page load, client:idle waits until the browser is idle, and client:visible waits until the component scrolls into view. Two more cover special cases: client:media hydrates when a CSS media query matches, and client:only skips server rendering and renders in the browser only. Without any directive, the component ships as static HTML with no JavaScript.

This article explains how each directive works in Astro 7, down to the browser API it uses, the options each accepts, and how to choose between them.

Key Takeaways

  • No directive means no JavaScript: React, Vue and Svelte components render to static HTML by default.
  • client:load is for controls needed the moment the page appears; it competes with everything else for the main thread.
  • client:idle uses requestIdleCallback and accepts {{ timeout }} to cap the wait.
  • client:visible uses IntersectionObserver and accepts {{ rootMargin }} to start early.
  • client:only="react" skips the server render entirely, so the island is blank until JavaScript runs.

How Astro client directives work

Every island, whatever its directive, is server-rendered to HTML first (except with client:only). Astro wraps it in an <astro-island> element that records the component’s bundle URL, its props and its directive. When the page loads, a tiny inline script for that directive decides when to import the component and hydrate it.

So the directive doesn’t change what the visitor sees first. It changes when the component becomes interactive, and when its JavaScript competes for bandwidth and CPU. Our Astro islands architecture guide explains the model behind this.

The five directives compared

Directive When it hydrates Browser API Options Server-rendered
client:load Immediately None None Yes
client:idle When the main thread is idle requestIdleCallback (falls back to setTimeout) {{ timeout: ms }} Yes
client:visible When the island enters the viewport IntersectionObserver {{ rootMargin: '200px' }} Yes
client:media="query" When the media query matches matchMedia The query string Yes
client:only="framework" Immediately None The framework name No

client:load

---
// src/components/Header.astro
import SearchBox from './SearchBox.jsx';
---
<header>
  <a href="/">Home</a>
  <SearchBox client:load />
</header>

client:load starts fetching the component and its framework as soon as the page’s scripts run, then hydrates. Use it for controls that visitors interact with immediately and that sit above the fold: a search box in the header, the main call-to-action form, a cart button.

The cost is that its JavaScript downloads and executes during page load, alongside your CSS, fonts and images. Every client:load island adds to the work before the page settles, and too many hurt Interaction to Next Paint and Total Blocking Time. If you’re unsure, it’s probably not client:load.

client:idle

---
// src/pages/index.astro
import ChatLauncher from '../components/ChatLauncher.svelte';
---
<ChatLauncher client:idle />
<ChatLauncher client:idle={{ timeout: 1000 }} />

client:idle waits for requestIdleCallback, which fires once the browser has finished its initial loading work and the main thread is free. In browsers without requestIdleCallback, Astro falls back to setTimeout with a 200 ms delay (or your timeout).

The timeout option sets a maximum wait: with {{ timeout: 1000 }}, the island hydrates after one second even if the browser never goes idle, which can happen on a busy page with animations or third-party scripts.

Use it for things that should work soon but not instantly: a chat launcher, a newsletter popup trigger, a theme switcher in the footer, a “copy link” button.

client:visible

---
// src/pages/product.astro
import Reviews from '../components/Reviews.vue';
import Gallery from '../components/Gallery.jsx';
---
<Gallery client:visible={{ rootMargin: '300px' }} />
<!-- ... product details ... -->
<Reviews client:visible />

client:visible creates an IntersectionObserver on the island’s rendered elements and hydrates the first time any of them intersects the viewport. If the visitor never scrolls that far, the JavaScript never downloads.

rootMargin extends the trigger area. '300px' starts loading when the island is 300 px below the viewport, so it’s usually ready by the time it’s on screen. Without it, a large component may briefly be visible but unresponsive while its code loads.

Use it for anything below the fold: carousels, comments, maps, charts, embedded calculators, pricing toggles further down a landing page. For most islands on content pages, client:visible is the right default.

One catch: the observer watches the island’s rendered HTML. If the component renders nothing on the server (an empty fragment, or something hidden with display: none), there’s nothing to intersect, and it may never hydrate. Give it some visible placeholder markup.

client:media

---
// src/components/Nav.astro
import MobileMenu from './MobileMenu.jsx';
---
<MobileMenu client:media="(max-width: 768px)" />

client:media hydrates when matchMedia reports a match, either on load or later if the viewport changes. On a desktop browser, the mobile menu above never loads its JavaScript.

Two details matter. First, the component is still server-rendered for everyone, so hide it with CSS on viewports where it shouldn’t appear; the directive controls JavaScript, not visibility. Second, hydration is one-way: once the query matches and the island hydrates, it stays hydrated if the query stops matching.

Use it for viewport-specific UI: a mobile drawer, a desktop-only sticky sidebar, or features behind (prefers-reduced-motion: no-preference).

client:only

---
// src/pages/dashboard.astro
import LiveChart from '../components/LiveChart.jsx';
---
<LiveChart client:only="react">
  <p slot="fallback">Loading chart…</p>
</LiveChart>

client:only skips server rendering. The component exists only in the browser, which is what you need when it touches window, localStorage or a browser-only library during render and would crash on the server.

Because Astro never renders the component on the server, it can’t detect which framework it belongs to, so you must name the renderer: "react", "preact", "vue", "svelte" or "solid-js". Leaving it out raises an error; the client:only missing hint fix shows the exact message.

The downside is a blank space until JavaScript runs, which causes layout shift and shows nothing to crawlers. Pass a slot="fallback" element, and reserve space with CSS. Prefer guarding browser APIs inside a useEffect (or equivalent) and using a normal directive where you can.

Choosing a client directive

Work through these in order and stop at the first yes:

  1. Does it need JavaScript at all? If it’s a static layout, list or card, use no directive. If it’s a small toggle, a plain <script> in an Astro component may be lighter; see island or script.
  2. Does it crash or make no sense on the server? client:only="framework".
  3. Is it only for some viewports or preferences? client:media.
  4. Is it below the fold? client:visible, with a rootMargin for heavy components.
  5. Is it needed right away, above the fold? client:load.
  6. Everything else: client:idle.

Our shorter decision guide, Which client directive should I use?, lays out the same choice as a flowchart.

Measuring the effect

Directives are a performance tool, so measure. Build and preview the site, then in Chrome DevTools:

# terminal
npm run build
npm run preview
  1. Open the Network panel, filter by JS, and reload. Islands with client:visible or client:media shouldn’t appear until you scroll or resize.
  2. Run a Lighthouse or Performance panel trace and compare Total Blocking Time before and after moving islands off client:load.

The bundle size analysis tip shows how to see which islands produce the largest chunks, and the lazy-loading islands tip covers deferring heavy components further.

Custom client directives

If none of the five fits, an integration can register its own with addClientDirective in the astro:config:setup hook, for example a client:click that hydrates on first interaction. The directive is a function that receives a load callback and decides when to call it. This is advanced; most sites never need it. The official client directives reference documents the API.

Komal themes keep client directives to a minimum, since most pages are pure Astro components with no hydration. You can browse Astro themes and check the built output with the getting started docs.

FAQ

What is the difference between client:load and client:idle?

client:load hydrates as soon as the page’s scripts run. client:idle waits until the browser’s main thread is free, using requestIdleCallback, so it doesn’t compete with initial page loading. Use client:idle unless the component must respond the instant the page appears.

Does client:visible improve performance?

Yes, for components below the fold. Their JavaScript isn’t downloaded or executed until they scroll into view, and never if the visitor doesn’t scroll that far, which reduces work during page load.

What happens if I don’t add a client directive in Astro?

The framework component renders to static HTML on the server and no JavaScript is sent for it. It looks correct but buttons, inputs and state changes won’t work in the browser.

When should I use client:only in Astro?

Only when a component can’t render on the server, typically because it uses window, document or a browser-only library during render. You must pass the framework name, such as client:only="react".

Can I use client directives on Astro components?

No. Client directives work on framework components (React, Vue, Svelte, Solid, Preact). Astro components always render to HTML; for interactivity inside them, use a <script> tag or a framework island.