Using islands reduces JavaScript shipping, speeds up page loading, and offers fine-grain control over JavaScript. Islands benefit smaller pages, load components in parallel, and optimize JavaScript delivery. Applications in eCommerce, media sites, docs, and blogs provide varied uses for islands, enhancing interactivity without excessive JavaScript. Nanostores facilitate state sharing among islands across different frameworks.
So rather than pre-rendering the HTML on the server, it will only send the JavaScript to the client. This can cause some issues with layout shift and things like that. But it can also be very helpful if you don't want to render any of those components on the page. And you can create your own directives. A great one as an example was rainy in New York, where your client component would only load if it was raining in New York. But you can define a small component, which will allow you to define whatever behavior you want. That could be whether you're waiting for a button press or you are calling some API to find out if some event is happening.
So what are the benefits of islands? Well, smaller pages. You don't need to ship the JavaScript where you don't need it, and you don't need to ship as much of it. And with that, you get faster LCP. You're not waiting for your entire component, your entire page to load JavaScript and run that JavaScript for the entire page. JavaScript is the heaviest form of content that you can ship to a page. So the less of it, the better. It also loads components in parallel. If you have three, four different islands on a page, these will load in parallel. You don't have to wait for one to finish before you load the next. And finally it gives you fine grain control over the JavaScript that you ship to your users.
So when should you use islands? Well, there's a few different places. In eCommerce, you can use it for product models, cards, recommendation widgets, any kind of interactive element on the page. But given that most of your page is going to be static, doesn't have any kind of API, it's not going to be a good idea to use islands. Media sites, you can use this to load video players, comments, modals, forms, anything that you might need. Docs and blogs, code playgrounds, interactive charts, any kind of small components that require some JavaScript but don't mean you want to render the entire page in JavaScript. Even if you do, you can use this in dashboards for small amounts of hydration with charts and filters, or even to load your entire app. But how do we share a state between all of these different islands? Islands, especially in Astro, can be from different frameworks and have no built-in way of sharing state. One of my favorite ways to do that is using a library called Nanostores. Nanostores is a tiny, tiny state manager which allows you to share state across pretty much anything. It has zero dependencies and has integrations for preact, react, Vue, Svelte, Angular. You can use it in a normal script tag. Quick example of Nanostores, we import our atom here.
Comments