What is an Island?
In Astro, an island is an enhanced UI component on an otherwise static page of HTML. A client island is an interactive JavaScript UI component that is hydrated separately from the rest of the page, while a server island is a UI component that server-renders its dynamic content separately from the rest of the page. Both islands run expensive or slower processes independently, on a per-component basis, for optimized page loads.A Brief History
The term “component island” was first coined by Etsy’s frontend architect Katie Sylor-Miller in 2019. This idea was then expanded on and documented in this post by Preact creator Jason Miller on August 11, 2020.The general idea of an “Islands” architecture is deceptively simple: render HTML pages on the server, and inject placeholders or slots around highly dynamic regions that can then be “hydrated” on the client into small self-contained widgets, reusing their server-rendered initial HTML.— Jason Miller, Creator of Preact
Client Islands
By default, Astro will automatically render every UI component to just HTML and CSS, stripping out all client-side JavaScript automatically.src/pages/index.astro
client:* directive. Astro then automatically builds and bundles your client-side JavaScript for optimized performance.
src/pages/index.astro
client:* directives.
And because interaction is configured at the component-level, you can handle different loading priorities for each component based on its usage. For example:
client:idletells a component to load when the browser becomes idleclient:visibletells a component to load only once it enters the viewport
Benefits of Client Islands
The most obvious benefit of building with Astro Islands is performance: the majority of your website is converted to fast, static HTML and JavaScript is only loaded for the individual components that need it. JavaScript is one of the slowest assets that you can load per-byte, so every byte counts. Another benefit is parallel loading. In the low-priority “image carousel” island doesn’t need to block the high-priority “header” island. The two load in parallel and hydrate in isolation, meaning that the header becomes interactive immediately without having to wait for the heavier carousel lower down the page. Even better, you can tell Astro exactly how and when to render each component. If that image carousel is really expensive to load, you can attach a special client directive that tells Astro to only load the carousel when it becomes visible on the page. If the user never sees it, it never loads. In Astro, it’s up to you as the developer to explicitly tell Astro which components on the page need to also run in the browser. Astro will only hydrate exactly what’s needed on the page and leave the rest of your site as static HTML. Client islands are the secret to Astro’s fast-by-default performance story!Server Islands
Server islands are a way to move expensive or slow server-side code out of the way of the main rendering process, making it easy to combine high-performance static HTML and dynamic server-generated components. Add theserver:defer directive to any Astro component on your page to turn it into its own server island:
src/pages/index.astro
Benefits of Server Islands
One benefit of server islands is the ability to render the more highly dynamic parts of your page on the fly. This allows the outer shell and main content to be more aggressively cached, providing faster performance. Another benefit is providing a great visitor experience. Server islands are optimized and load quickly, often even before the browser has even painted the page. But in the short time it takes for your islands to render, you can display custom fallback content and prevent any layout shift. An example of a site that benefits from Astro’s server islands is an e-commerce storefront. Although the main content of product pages change infrequently, these pages typically have some dynamic pieces:- The user’s avatar in the header
- Special deals and sales for the product
- User reviews