Hydratable
hydratable() runs once on the server; the hydrated island reads the same value from <head> on the client without re-running the async work. Watch the server console — the 'computing fact' line prints once per request, never on hydration.
Page (SSR-only)
Runs in the page's top-level script and is inlined into the rendered HTML.
SQLite 3.53.4, server time 2026-12-02 10:49:53
Island using hydratable()
Reads the SSR value out of window.__svelte.h on the client without re-running the work.
SQLite 3.53.4, server time 2026-12-02 10:49:53
Island using a prop
Receives the value as a prop; Mochi serialises it into the island's HTML.
SQLite 3.53.4, server time 2026-12-02 10:49:53
<script lang="ts">
import { hydratable, setContext } from 'svelte';
import { Database } from 'bun:sqlite';
import FactCard from './FactCard.svelte';
import FactCardProps from './FactCardProps.svelte';
// With hydratable, the result is serialized into <head>; the matching call inside the hydrated
// island below reads it back from window.__svelte.h instead of re-running this function on the client.
const fact = hydratable('mochi-demo:fact', () => {
console.log('[server] computing fact via bun:sqlite (should print once per request)');
const db = new Database(':memory:');
const row = db.query("SELECT datetime('now') as now, sqlite_version() as version").get() as { now: string; version: string };
db.close();
return {
sqliteVersion: row.version,
computedAt: row.now,
};
});
// Lets FactCard's fallback hand Svelte's dev-mode server check this exact value instead of recomputing it.
setContext('mochi-demo:fact', fact);
</script>
<h3>Page (SSR-only)</h3>
<p class="hint">Runs in the page's top-level script and is inlined into the rendered HTML.</p>
<p class="row">SQLite <strong>{fact.sqliteVersion}</strong>, server time <code>{fact.computedAt}</code></p>
<h3>Island using hydratable()</h3>
<p class="hint">Reads the SSR value out of window.__svelte.h on the client without re-running the work.</p>
<FactCard mochi:hydrate />
<h3>Island using a prop</h3>
<p class="hint">Receives the value as a prop; Mochi serialises it into the island's HTML.</p>
<FactCardProps {fact} mochi:hydrate />
Styles
<style>
h3 {
margin: 1rem 0 0.25rem;
font-size: 1rem;
font-weight: 600;
}
.hint {
color: var(--text-muted);
font-size: 0.85em;
margin: 0 0 0.5rem;
}
.row {
margin: 0 0 0.5rem;
}
code {
font-size: 0.9em;
}
</style>
More demos
Isomorphic URL How the isomorphic URL helper works — one import that reads the request URL on the server and window.location on the client. HTML Entities in Props How HTML entities in island props work — an entity in a static prop (label="Tom & Jerry") decodes identically on the server and after hydration. Server Islands How server islands work — components marked mochi:defer render server-side on demand after the initial page is delivered.