Skip to content

Server Islands Utilities

This integration provides a set of utilities to work with Astro’s Server Islands.

Terminal window
npx astro add @inox-tools/server-islands

currentPageUrl

Type: (Astro: AstroGlobal) => URL

Extracts the URL of the current page from the Astro context object, automatically taking into account whether the current component is being executed on a Server Island.

src/components/MyComponent.astro
---
import {currentPageUrl} from '@it-astro:server-islands';
const pageUrl = currentPageUrl(Astro);
---

The Astro.url property is not the URL of the page when running inside a server island. This function is a convenient wrapper that returns getServerIslandContext().pageUrl when running on a Server Island and Astro.url when not.

getServerIslandContext

Extracts information about the current execution in the context of a server island from the Astro context object.

src/components/MyComponent.astro
---
import {getServerIslandContext} from '@it-astro:server-islands';
const context = getServerIslandContext(Astro);
---

This function will return null when not executing inside a Server Island (running directly on a page with no server:defer in this component nor any parent component).

When running in the context of a Server Island, this function returns an object with the following properties:

pageUrl

Type: URL

The URL of the page where the component will be added to. This is the url of the top of the page, no matter how nested the server island is (a server island may return content including another server island).

License

Server Islands Utilities is available under the MIT license.