# Step 1: Create a new SvelteKit project
npm create svelte@latest streaming-demo
cd streaming-demo
# Step 2: Choose the following options during setup:
# - Skeleton project: Yes
# - TypeScript: Yes (optional, but recommended)
# - ESLint: Yes
# - Prettier: Yes
# - Playwright: No
# - Vitest: No
# - npm: Yes
# Step 3: Install dependencies
npm install
# Step 4: Add the demo files below by replacing/creating them
# Step 5: Run the dev server
npm run dev
# Open http://localhost:5173
# Open browser DevTools > Network tab > reload the page.
# You will see:
# - Initial HTML chunk with "FAST" content and fast counter (interactive immediately after hydration).
# - After ~3 seconds, second HTML chunk with "SLOW" content and slow counter (hydrates/interactive shortly after).
# This demonstrates:
# - Streaming SSR: Page loads progressively; fallback shows until slow data streams in.
# - Partial hydration: Fast counter works right away; slow counter waits for its chunk + JS bundle.
Key Files to Create/Replace
src/lib/delay.js (Utility for simulating slow data)
export function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
src/routes/+page.server.js (Server load function returning a streamed promise)
import { delay } from '$lib/delay.js';
export const load = () => ({
fast: 'FAST CONTENT (streams immediately)',
streamed: delay(3000).then(() => 'SLOW STREAMED CONTENT (after 3s)')
});
src/routes/+page.svelte (Main page with static/fast content + suspense boundary)
<script lang="ts">
import Slow from './Slow.svelte';
import type { PageData } from './$types';
export let data: PageData;
let fastCount = 0;
</script>
<svelte:head>
<title>Streaming SSR + Partial Hydration Demo</title>
</svelte:head>
<main class="container">
<h1>🧠 SvelteKit: Streaming SSR + Partial Hydration</h1>
<section class="fast-section">
<h2>🚀 Fast Content (streams & hydrates immediately)</h2>
<p>{data.fast}</p>
<div class="counter">
<button on:click={() => fastCount++}>
Increment Fast Counter: {fastCount}
</button>
</div>
</section>
<section class="slow-section">
<h2>⏳ Slow Streamed Content (waits 3s)</h2>
<suspense fallback={<div class="fallback">Loading slow interactive content...</div>}>
<Slow streamed={data.streamed} />
</suspense>
</section>
<p class="instructions">
<strong>Observe:</strong><br>
• Page appears instantly with fast content.<br>
• Fast counter is interactive immediately (partial hydration).<br>
• Slow section shows fallback → streams HTML after 3s → slow counter becomes interactive.
</p>
</main>
<style>
.container {
max-width: 800px;
margin: 0 auto;
padding: 2rem;
font-family: system-ui, sans-serif;
}
h1, h2 {
color: #333;
}
.fast-section {
border: 2px solid #10b981;
border-radius: 8px;
padding: 1.5rem;
margin-bottom: 2rem;
background: #ecfdf5;
}
.slow-section {
border: 2px solid #f59e0b;
border-radius: 8px;
padding: 1.5rem;
background: #fef3c7;
}
.fallback {
padding: 1rem;
background: #fbbf24;
border-radius: 4px;
color: #92400e;
font-style: italic;
}
.counter {
margin-top: 1rem;
}
button {
background: #3b82f6;
color: white;
border: none;
padding: 0.75rem 1.5rem;
border-radius: 6px;
cursor: pointer;
font-size: 1rem;
transition: background 0.2s;
}
button:hover {
background: #2563eb;
}
.instructions {
background: #f3f4f6;
padding: 1rem;
border-radius: 6px;
font-size: 0.9rem;
}
</style>
src/routes/Slow.svelte (Lazy/partial-hydrated interactive component)
<script lang="ts">
export let streamed: string;
let slowCount = 0;
</script>
<div class="slow-content">
<p>✅ {streamed}</p>
<div class="counter">
<button on:click={() => slowCount++}>
Increment Slow Counter: {slowCount}
</button>
</div>
</div>
<style>
.slow-content {
padding: 1rem 0;
}
.counter {
margin-top: 1rem;
}
button {
background: #ef4444;
}
button:hover {
background: #dc2626;
}
</style>
src/app.html (Optional: Ensure SSR is enabled; default is fine, but confirm)
<!doctype html>
<html lang="en" class="h-full">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
<body class="h-full" data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
svelte.config.js (Default; ensure no changes break SSR)
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/kit/vite';
const config = {
preprocess: vitePreprocess(),
kit: {
adapter: adapter()
}
};
export default config;
Additional Notes
- Version Requirements: Requires SvelteKit ≥1.22.0 (includes
<suspense>). Check with npm list @sveltejs/kit. Update if needed: npm update @sveltejs/kit.
- Production Build:
npm run build → npm run preview.
- Why Partial Hydration? SvelteKit automatically code-splits the JS for the
<suspense> subtree into a separate chunk, loaded only when the boundary resolves. Fast counter hydrates in ~1st chunk; slow in 2nd.
- Customization: Increase delay to 5000ms for clearer demo. Replace
delay with real async fetch for production.
- Svelte 5/SvelteKit 2: Even better support with async components/runes, but this works on stable today.
This is a fully functional, self-contained demo! 🚀