🔥 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 Hydration Explained: How Islands Come to Life

Astro hydration explained: how client directives work, what the astro-island element contains, how props are serialized, and how to debug islands that won't hydrate.

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

Astro hydration is opt-in and per component. Astro renders every component to HTML on the server, and only components you mark with a client:* directive (client:load, client:idle, client:visible, client:media or client:only) get their JavaScript sent to the browser. Each one is wrapped in an <astro-island> element that loads the component’s code and its framework renderer when the directive’s condition is met, then attaches the framework to the existing HTML.

Everything else on the page stays as static HTML with no runtime. That’s the islands architecture: small interactive regions in a static page, each hydrating on its own schedule.

Key Takeaways

  • Hydration means attaching a framework’s event handlers and state to HTML that was already rendered on the server.
  • In Astro, nothing hydrates unless you add a client:* directive to a framework component (React, Vue, Svelte, Solid, Preact).
  • The directive decides when: immediately (load), when idle (idle), when in view (visible), when a media query matches (media), or client-only with no server render (only).
  • Props are serialized into an HTML attribute, so they must be serializable: no functions, no class instances beyond the supported types.
  • .astro components never hydrate. For small interactions, a <script> tag is often lighter than an island.

What hydration means

A framework like React normally renders a page in the browser: download the JavaScript, run it, build the DOM. Server rendering speeds that up by sending HTML first, but the HTML is inert. Buttons don’t work until the framework runs in the browser, rebuilds its picture of the component tree, matches it to the existing DOM and attaches event handlers. That step is hydration.

In a typical single-page app framework, the whole page hydrates, even the parts that never change. On a content page, that’s a lot of JavaScript spent re-creating a footer and a paragraph of text.

Astro reverses the default. The page is HTML. Only the components you explicitly mark become interactive, and each is hydrated independently. The islands architecture explainer covers the design idea; this article covers the mechanics.

Opting in with client directives

---
// src/pages/pricing.astro
import Header from '../components/Header.astro';
import PriceCalculator from '../components/PriceCalculator.jsx';
import Testimonials from '../components/Testimonials.svelte';
---
<Header />
<PriceCalculator client:load plans={['starter', 'team']} />
<Testimonials client:visible />

Header is an Astro component: HTML only, never hydrated. PriceCalculator hydrates as soon as the page loads. Testimonials renders to HTML on the server and hydrates only when it scrolls into view.

Directive Hydrates when Server-rendered HTML
client:load Immediately on page load Yes
client:idle When the browser is idle (requestIdleCallback) Yes
client:visible When the component enters the viewport (IntersectionObserver) Yes
client:media="(query)" When the media query matches Yes
client:only="react" Immediately, rendering only in the browser No

Two directives take options:

<Comments client:visible={{ rootMargin: '200px' }} />
<ChatWidget client:idle={{ timeout: 2000 }} />

rootMargin starts hydration slightly before the component is visible, so it’s ready by the time the reader reaches it. timeout forces client:idle to run within that many milliseconds even if the browser never goes idle. In browsers without requestIdleCallback, client:idle falls back to a short setTimeout.

Picking the right one per component matters for performance. The client directive decision guide walks through it.

What the browser receives

For an island, Astro renders the component to HTML on the server and wraps it in a custom element. Simplified, the output looks like this:

<astro-island
  uid="Z1abc2d"
  component-url="/_astro/Testimonials.CkP3x9.js"
  component-export="default"
  renderer-url="/_astro/client.svelte.Bq2kL1.js"
  props="{&quot;limit&quot;:[0,3]}"
  ssr=""
  client="visible"
  opts="{&quot;name&quot;:&quot;Testimonials&quot;,&quot;value&quot;:true}"
>
  <!-- server-rendered HTML of the component -->
  <section class="testimonials">...</section>
</astro-island>

Each attribute has a job:

  • component-url: the built JavaScript chunk for this component.
  • renderer-url: the framework’s client entry (the Svelte, React or Vue hydration code). Shared by every island using that framework.
  • component-export: which export of the module is the component.
  • props: the serialized props, so the browser copy starts with the same data as the server.
  • client: the directive, which selects the loading strategy.
  • opts: directive options and the display name.
  • ssr: present when the component was server-rendered (absent for client:only).

Astro also inlines a small script that defines the <astro-island> element and the directive loaders the page uses. A page with client:visible islands includes the visible loader; a page with no islands includes none of this.

The hydration sequence

When the <astro-island> element connects to the page:

  1. Wait for children. If the island’s HTML is still streaming in, it waits until its content has arrived.
  2. Run the directive. The directive gets a load function. client:load calls it immediately; client:visible sets up an IntersectionObserver on the island’s children and calls it on first intersection; client:media calls it when matchMedia matches.
  3. Load code. load imports the component chunk and the renderer in parallel. If an import fails (a flaky network, a deploy that replaced the file), current versions retry once after a short delay.
  4. Revive props. The props attribute is parsed back into values, restoring types like Date and Map.
  5. Hydrate. The framework renderer attaches the component to the existing HTML, matching what the server rendered. For client:only, it renders from scratch instead.
  6. Signal. The island dispatches an astro:hydrate event when it finishes.

Nested islands work too: a parent island waits for children inside it before hydrating, so frameworks see a complete tree.

Props: what you can pass

Because props travel through an HTML attribute, they’re serialized on the server and revived in the browser. Supported:

  • Strings, numbers, booleans, null, plain objects and arrays.
  • Date, Map, Set, RegExp, URL, BigInt, Infinity.
  • Typed arrays (Uint8Array, Uint16Array, Uint32Array).

Not supported:

  • Functions. You can’t pass an onClick handler from an Astro page to a React island. Define handlers inside the island, or communicate through events or a shared store.
  • Class instances other than the types above. They arrive as plain objects or not at all.
  • Very large data without cost. A 200 KB array prop becomes a 200 KB attribute in the HTML. Pass IDs and let the island fetch details, or trim the data on the server.

Children and named slots are passed as server-rendered HTML, which the framework treats as static content. The passing props to islands tip shows patterns for each case.

Hydration and view transitions

With <ClientRouter />, navigation swaps the page body instead of reloading. Islands on the old page are unmounted after the swap, and islands on the new page hydrate according to their directives, as on a fresh load.

To keep an island alive across navigations (a music player, a cart drawer), add transition:persist. Astro moves the existing element into the new page instead of replacing it, so its state survives. The persist islands tip covers the details.

Debugging an island that won’t hydrate

Symptom Likely cause Fix
Component renders but buttons do nothing No client:* directive Add one; without it the component is HTML only
Directive added to an .astro component, nothing happens Astro components don’t hydrate Use a framework component, or a <script>
client:visible island never hydrates Island has no visible children (for example, zero height) Give it content or size, or use client:idle
Hydration mismatch warning in the console Server and browser render differently (dates, random values, window checks) Render the variable part only after mount, or use client:only
“No matching renderer” error Framework integration not installed npx astro add react (or vue, svelte, solid, preact)
client:only error about a missing hint No framework name given Use client:only="react"

In DevTools, inspect the <astro-island> element: check the client attribute, confirm the component-url file loads in the Network panel, and watch the console for [astro-island] errors. Current versions also dispatch a cancelable astro:hydration-error event on the island when loading or hydrating fails, which you can listen for to report errors to your monitoring tool.

When not to use an island

Hydration has a cost: the framework runtime, the component code, and the work of hydrating. For a menu toggle, a copy button or a tab set, a <script> in an .astro component does the same job with a fraction of the JavaScript and no hydration step. The island or script decision guide helps you choose per component.

Islands are the right choice when the component has real state, is already written in a framework, or uses a framework-only library. Astro also lets you write custom client directives through the integration API’s addClientDirective, for cases like hydrating on first click.

Komal themes follow this approach: static Astro components by default, with interactivity added only where a component needs it, so you can see the pattern in a finished site when you browse Astro themes. The getting started docs show how to run one locally and inspect its output. The official Astro islands documentation and client directives reference cover the full API.

FAQ

What is hydration in Astro?

Hydration is the step where a framework component’s JavaScript runs in the browser and attaches interactivity to HTML Astro already rendered on the server. In Astro, it only happens for components with a client:* directive.

Do Astro components hydrate?

No. .astro components render to HTML only and never ship a runtime. To add interactivity to one, include a <script> tag, or use a React, Vue, Svelte, Solid or Preact component with a client directive.

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

client:load hydrates the component as soon as the page loads. client:visible waits until the component enters the viewport, so below-the-fold components don’t compete with the initial page load.

Can I pass a function as a prop to an Astro island?

No. Island props are serialized into HTML, and functions can’t be serialized. Define event handlers inside the framework component, or communicate between islands with custom events or a shared store such as nanostores.

Why does my Astro island show a hydration mismatch?

The HTML rendered on the server differs from what the component renders in the browser, often because of dates, random values, locale formatting or code that checks window. Make the first render deterministic and update after mount, or use client:only to skip server rendering.