This is the contents of the README on the GitHub repo.
<is-land>
This is an experiment with adding partial hydration to a SvelteKit site with @11ty/is-land, a “framework independent partial hydration islands architecture implementation.”
By using the provided Island.svelte
component, you can selectively hydrate individual components instead of the whole page.
This is just a POC for now, but may be packaged for reuse at a later date.
Create a component in /src/lib/islands
. On your page, import the Island.svelte
component and the component you would like to render inside the island.
For partial hydration to work, you need to disable client-side rendering for the page. Otherwise, the entire page will hydrate.
<script>
import Count from '$lib/islands/Count.svelte';
import Island from '$lib/Island.svelte';
</script>
<h2>Without props</h2>
<Island component={Count} name="Count" />
<h2>With props</h2>
<Island component={Count} name="Count" props={{ title: 'island' }} />
<h2>Customizing island options</h2>
<Island component={Count} name="Count" islandProps={{ 'on:interaction': true }} />
You will also need to set up a separate Vite build to bundle the island components - see the build
script in this repo’s package.json
for an example.
component
: the component to render. This is used to server-render the componentname
: the name of the component file. This is used to import the component on the client when hydration occurs.props
: an object of props to pass to the componentislandProps
: an object of props to pass to the <is-land>
. See the is-land documentation.See the source code for +page.svelte for a full example.
The bulk of the work is in /src/lib/Island.svelte
. This component renders an <is-land>
element with a <template data-island>
that initializes the provided component using the client-side component API.
We have two build steps so we can import the island components indepently. First, we build the components in $lib/islands
using Vite with islands.config.js
, and then we build the SvelteKit app as normal. A separate entry point is generated for each island (e.g. /src/lib/islands/Count.svelte
-> /static/__islands/Count.js
). The islands are placed in the static/
folder so that it is deployed with the app. We can’t go through the normal SvelteKit build pipeline since when CSR is disabled, no client-side JS will load.
At dev time, no separate build step is required, since we can import Svelte components directly from /src/lib/islands
and the Vite dev server will handle it.
These are the current limitations. If you have ideas on how to fix some of these, let me know!
/src/lib/islands
$app/*
, $env/*
, $lib/*
). While it may appear to work in dev, it will fail at build time since the island components are bundled separately and don’t have access to the same aliases. If you need access to these imports, you can pass them into your islands as a prop.<script>
, all props passed to the island must be serializable with devalue.name
prop as well. Otherwise, the wrong component will be hydrated. (If you have an idea of how to design this so users do not need to pass the name prop, please let me know!)$app/*
imports) that fail at build time.Once you’ve created a project and installed dependencies with npm install
(or pnpm install
or yarn
), start a development server:
npm run dev
# or start the server and open the app in a new browser tab
npm run dev -- --open
To create a production version of your app:
npm run build
You can preview the production build with npm run preview
.