🔥 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)
Server Rendering••By Komal Nakrani

Astro Server Islands: server:defer Explained with Examples

Astro server islands explained: keep pages static while server:defer renders personal or live parts per request. Fallbacks, props, caching, and when to use them.

#Astro#Server Islands#SSR#Performance

Astro server islands let a static page include components that render on the server per request. Add the server:defer directive to a component, and Astro builds the rest of the page as static HTML, leaves a placeholder (your fallback content) where the component goes, then fetches the component’s HTML from the server after the page loads. You get CDN-speed pages with personalized or live parts, without making the whole page dynamic.

This guide explains how Astro server islands work in Astro 7, how to write one with a fallback, how props and caching behave, and when a server island is the wrong tool.

Key Takeaways

  • server:defer turns any .astro component into a server island rendered per request.
  • The page around it can stay prerendered and cached on a CDN.
  • Use <div slot="fallback"> to show a placeholder until the island loads.
  • Props are serialized, encrypted and sent in the island request, so they must be serializable.
  • Server islands need an adapter, but they don’t send any framework JavaScript to the browser.

The problem server islands solve

Most pages are almost entirely static. A product page, a blog post or a marketing page is the same for every visitor, except for a small part: the signed-in user’s avatar, a cart count, stock levels, a “recently viewed” list or a price in the visitor’s currency.

Before server islands, that small part forced a choice:

  • Render the whole page on demand. Every visitor waits for the server, and the page can’t be cached as one shared file.
  • Fetch it with client-side JavaScript. You ship a framework island or a fetch script, build an API endpoint, and handle loading states yourself.

Server islands give a third option. The page is prerendered and cached like any static page. The dynamic component is rendered on the server, by Astro, as HTML, and dropped into place by a tiny inline script.

How Astro server islands work

When Astro builds a page that contains a server:defer component:

  1. The page is rendered without the island. In its place, Astro outputs the fallback content (or nothing) plus a small script.
  2. The island’s component is compiled into a server route under /_server-islands/.
  3. In the browser, the script requests the island’s HTML from that route, sending the component’s encrypted props.
  4. The server renders just that component, with access to cookies, headers and locals, and returns HTML.
  5. The script swaps the fallback for the returned HTML.

The page loads immediately with its static content, and the island arrives shortly after. Several islands on one page load in parallel and don’t block each other.

Static page On-demand page Page with a server island
Main HTML Built once, cached Rendered per request Built once, cached
Personalized parts Not possible Yes Yes, in the island
Time to first byte Fastest Depends on server Fastest
Framework JS needed No No No
Needs an adapter No Yes Yes

Writing your first server island

A server island is an ordinary Astro component. Nothing in the component itself changes:

---
// src/components/UserMenu.astro
const session = Astro.cookies.get('session')?.value;
const user = session ? await getUserFromSession(session) : null;
---
{user ? (
  <a href="/account/" class="user-menu">
    <img src={user.avatarUrl} alt="" width="32" height="32" />
    <span>{user.name}</span>
  </a>
) : (
  <a href="/login/" class="user-menu">Sign in</a>
)}

getUserFromSession stands in for your own lookup. The directive goes where you use the component:

---
// src/layouts/BaseLayout.astro
import UserMenu from '../components/UserMenu.astro';
---
<header>
  <a href="/">Example Studio</a>
  <UserMenu server:defer>
    <a slot="fallback" href="/login/" class="user-menu">Sign in</a>
  </UserMenu>
</header>
<slot />

Every page that uses this layout can stay prerendered. The header shows “Sign in” from the static HTML, and signed-in visitors see their name a moment later.

Before this works, add an adapter. Without one, the build stops with “Cannot use server islands without an adapter.” The fix for that error shows the one-line setup, and Astro adapters explained helps you choose one.

npx astro add node

Fallback content that avoids layout shift

The fallback slot renders in the static HTML and is replaced when the island arrives. Good fallbacks:

  • Match the final size. A skeleton box with the same height as the real content prevents layout shift, which protects your Core Web Vitals.
  • Show a sensible default. “Sign in” is correct for most visitors, so most never see a change.
  • Stay simple. It’s static HTML, so it can’t depend on the visitor.
---
// src/pages/products/[slug].astro (excerpt)
import StockLevel from '../../components/StockLevel.astro';
const { product } = Astro.props;
---
<StockLevel server:defer sku={product.sku}>
  <p slot="fallback" class="stock stock--loading" aria-busy="true">Checking stock...</p>
</StockLevel>
/* src/styles/stock.css */
.stock { min-height: 1.5rem; }
.stock--loading { color: var(--color-muted); }

Without a fallback, the island’s space is empty until it loads.

Props and server islands

Props you pass to a server island are serialized and sent with the island request. Astro encrypts them, so visitors can’t read or tamper with the values, but there are rules:

  • Props must be serializable: strings, numbers, booleans, plain objects and arrays, dates and similar values. Functions and class instances can’t be passed.
  • Keep them small. Large props make the island request bigger; pass an ID and look up the data inside the island instead.
  • Astro generates the encryption key at build time. If you run multiple builds or servers that must share island requests (rolling deploys, several instances), generate a key once with astro create-key and set it as the ASTRO_KEY environment variable so every instance uses the same one.
---
// Pass an ID, not the whole object
---
<Recommendations server:defer productId={product.id} />

Reading the request inside an island

Inside a server island, Astro.cookies, Astro.locals (populated by middleware) and request headers work as in any on-demand page. One difference: Astro.url is the island’s own URL, not the page’s. If the island needs the page path, pass it as a prop:

<ShareCount server:defer path={Astro.url.pathname} />

Because the page itself was prerendered, values from Astro.url in the page are known at build time, so passing them as props is safe.

Caching server island responses

By default an island renders on every request. When the output is the same for many visitors (stock levels, a “trending” list, exchange rates), cache it by setting a header inside the island:

---
// src/components/StockLevel.astro
const { sku } = Astro.props;
const stock = await getStock(sku);
Astro.response.headers.set('Cache-Control', 'public, max-age=60, s-maxage=60');
---
<p class="stock">{stock > 0 ? `${stock} in stock` : 'Out of stock'}</p>

Island requests are GET requests as long as the encrypted props keep the URL under 2,048 characters (Astro switches to POST beyond that, which isn’t cached), so CDNs and browsers can cache them like any response. Small props keep islands cacheable. Never add a public cache header to an island that shows personal data, such as a user menu or cart. The Astro performance guide covers caching headers more broadly.

When to use a server island (and when not to)

Good fits:

  • User menus, avatars and “signed in as” text on otherwise static pages
  • Cart counts and mini-carts on e-commerce pages
  • Stock, price or availability that changes often
  • Personalized recommendations or “recently viewed”
  • Content that depends on location or request headers

Poor fits:

  • Interactive widgets. A server island returns HTML, not a hydrated component. For something that responds to clicks, use a client island with a client:* directive, or put a client island inside the server island. The client directive guide explains the options.
  • Content that’s the same for everyone and changes rarely. Prerender it and rebuild instead.
  • SEO-critical content. Crawlers may not wait for or run the island script, so keep anything that must be indexed in the static HTML.
  • Fully static hosting. GitHub Pages and other file-only hosts can’t run the island route.

Server islands versus client islands

Server island (server:defer) Client island (client:*)
Rendered On the server, per request In the browser (often also prerendered)
Sends to browser HTML Component JavaScript plus framework runtime
Can read cookies and secrets Yes No
Interactive No (plain HTML) Yes
Component types .astro components React, Vue, Svelte, Solid, Preact and more
Needs an adapter Yes No

They combine well: a server island can render personalized data and contain a client island for the interactive part. The island or script guide covers lighter alternatives for small interactions.

Server islands in a static theme

A static theme is a good base for server islands, because the expensive part (every page’s HTML) is already prerendered. Komal themes are built for static output with no adapter, so you’d add one, then mark only the dynamic component with server:defer; every other page stays a static file on the CDN. Themes in the Retail & E-commerce category are a natural fit for cart counts and stock levels.

The official reference is Astro’s server islands guide.

FAQ

What are server islands in Astro?

Server islands are components marked with server:defer that render on the server per request while the rest of the page stays static. Astro outputs a fallback in the static HTML and replaces it with the server-rendered component after the page loads.

Do Astro server islands need an adapter?

Yes. The island is served from a server route, so you need an adapter such as @astrojs/node, @astrojs/cloudflare, @astrojs/netlify or @astrojs/vercel. The pages containing the islands can still be prerendered.

Are server islands bad for SEO?

They’re fine for personalized or fast-changing parts that don’t need to be indexed. Keep primary content, headings and links in the static HTML, because search engines may not render content loaded after the initial page.

Can I use React components as server islands?

server:defer is for Astro components. You can render a React, Vue or Svelte component inside an Astro server island, either as static HTML or as a client island with a client:* directive if it needs interactivity.

What is the difference between server:defer and client:load?

server:defer renders a component on the server per request and sends HTML, which can use cookies and secrets but isn’t interactive. client:load sends the component’s JavaScript to the browser and hydrates it so it can respond to user input.