Skip to content

SPA, SSR and hydration

Astro server islands: the fallback is all a static fetch gets

Spot server:defer islands in raw HTML, print the fallback text a static fetch keeps, and decide whether the missing part needs a browser.

By Cerys Eaton, SiteRender team · Published

This article was drafted with AI assistance from the sources listed below and edited and published by the SiteRender team. Disclosure: the SiteRender API, which this article mentions, is this site's own product.

Is the missing text inside a server island?
  1. 1Count markersSearch the raw HTML for server-island-start comments.
  2. 2List island routesEach island is requested from a path under /_server-islands/.
  3. 3Print the fallbackThe slot="fallback" content is the only island text in the first response.
  4. 4DecideMain text inside an island needs a browser; text outside it reads with a static fetch.

The page is built with Astro, it is server-rendered, and the grep for a visible sentence still comes back empty. Or it finds the sentence, but one block of the page reads "Loading..." in your extracted text. If the site uses server islands, that is the expected result, not a fetch error.

What server:defer does to the first response

A server island is a component marked with the server:defer directive. It is rendered on demand, separately from the rest of the page, and the feature needs an adapter installed. The point, as the Astro docs put it, is that visitors see the main content sooner and the page around the island can be cached more aggressively.

For a reader, the important part is how. When Astro builds the site, it leaves the component out of the page and puts a script where it was. Any content the developer marked with slot="fallback" is rendered into the initial HTML and stays visible until the island loads. Each island becomes its own route, and the script requests it when the page runs in a browser. Islands load independently of each other.

So a static fetch of an Astro page gets everything outside the islands, plus each island's fallback. The component's own HTML is not in the response at all. The rendering-modes guide covers the other rendering modes.

The markers to search for

Astro 5's runtime writes three things per island:

  • a comment, <!--[if astro]>server-island-start<![endif]-->, before the fallback;
  • the fallback content, if there is any;
  • a <script type="module"> with a data-island-id attribute, whose code fetches the island and calls replaceServerIsland with the response.

At runtime, replaceServerIsland removes the nodes between the comment and the script, inserts the fetched HTML before the script, and removes the script. The island's URL is built from the site's base path plus _server-islands/ and the component's name. For a GET, the query string carries three parameters, e, p and s, holding the encrypted component export, the encrypted props and the slots, and Astro also adds a preload link for that URL.

Count and list them:

URL=https://example.com/
curl -sL -H 'Accept-Encoding: identity' "$URL" -o raw.html
grep -o 'server-island-start' raw.html | wc -l               # islands on the page
grep -oE '_server-islands/[A-Za-z0-9_-]+' raw.html | sort -u # their routes
grep -cE "method: ?'POST'" raw.html                          # lines with POST islands

A count of zero on the first line: no server islands, and the missing text has another cause. The server-side rendering checks cover the usual ones.

The fallback is the only island text a static reader keeps, and your extractor will treat it as normal page content. Print it per island:

# fallbacks.py: python3 fallbacks.py raw.html
import re
import sys

html = open(sys.argv[1], encoding="utf-8").read()
island = re.compile(
    r'<!--\[if astro\]>server-island-start<!\[endif\]-->(.*?)'
    r'<script[^>]*data-island-id="([^"]+)"',
    re.S,
)
for match in island.finditer(html):
    text = " ".join(re.sub(r"<[^>]+>", " ", match.group(1)).split())
    print(match.group(2), "|", text or "(no fallback)")

Each line is one island: its id, then the text that stands in for it. "(no fallback)" means the island has no text at all in what you fetched. If the sentence you were looking for belongs in one of these slots, the page is server-rendered everywhere except where you need it.

Why the island URL is not something to store

The island route is visible in the HTML, so it is tempting to request it directly. Details from the docs argue against building on it.

First, the props are encrypted with a random key that Astro generates on each build and embeds in the server bundle. A site that needs the key to stay the same across builds creates one with astro create-key and sets it as ASTRO_KEY; the docs name rolling deployments, multi-region hosting and CDNs that cache pages with islands as the cases for that. Without it, a URL copied from one build's HTML carries props encrypted for that build.

Second, the request method is not fixed. Astro uses a GET with the props in the query string so standard Cache-Control headers apply. When the URL would pass 2048 bytes, it switches to a POST with the props in the body, and browsers do not cache POST requests.

Third, it is not a page. Inside an island, Astro.url looks like /_server-islands/Avatar, not the page's address; the docs point island code to the Referer header to learn which page loaded it. The route is an internal step in rendering one page, not a document of its own. If the text you need lives in an island, read the rendered page in a browser, and check robots.txt and the site's terms first, as for any page.

For site owners

If you want fetchers and AI crawlers to read a page, keep its main text out of server:defer. Server islands suit the parts that are personal or slow: an avatar, a cart count, a "recommended for you" row. Write each fallback as if it were the final content for a reader without JavaScript, because for that reader it is. Then run the fallback script on your own page and read the output.

Test it yourself

Pick an Astro page you maintain, or one that uses server:defer, and run the three greps. Then run fallbacks.py and compare its output with the page in a browser. Every difference is text that only exists after a second request.

In SiteRender API, the browser fallback is for pages the fast fetch cannot read (no or very little visible text, or a timeout), so a page with plenty of text outside its islands can come back from the fast fetch with only the fallbacks. render: true renders every URL in a real browser; the docs list the request options.

Frequently asked questions

What does an Astro server island look like in the raw HTML?

A comment marker that reads server-island-start, then the island's fallback content, then a module script with a data-island-id attribute. The script requests the island's HTML from a path under /_server-islands/ and swaps it in where the fallback was. The component itself is not in the first response.

Does curl get the content of a server:defer component?

No. At build time Astro leaves the component out of the page and puts a script in its place. curl receives the fallback content, if the site defined one, and the script. The component's HTML arrives only when that script runs in a browser and makes a second request.

Why does a server island sometimes use a POST request?

Astro sends the island's encrypted props in the query string of a GET request. When that URL would exceed 2048 bytes, it sends a POST with the props in the request body instead. Browsers do not cache POST requests, so the browser does not cache that island.

How do I keep the main content of an Astro page readable without JavaScript?

Keep it outside server:defer. Use server islands for personalized or slow parts such as an avatar or a cart, and give each one a fallback that makes sense on its own, because the fallback is what any reader without JavaScript keeps.

Sources

  1. Server islands (Astro docs) (Documentation)
  2. server-islands.js (Astro 5 runtime, npm build) (Documentation)