Spec Registry
Help your agents use open-source better. Learn more.
Find usage specs for your project’s dependencies
- Author
- tessl
- Last updated
- Spec files
npm-svelte
Describes: npm/svelte
- Description
- A cybernetically enhanced web application framework that compiles to highly optimized JavaScript with reactive state management and component-based architecture.
- Author
- tessl
- Last updated
ssr.md docs/
1# Server-Side Rendering23Svelte provides server-side rendering capabilities that allow you to generate HTML on the server for improved performance, SEO, and user experience.45## Capabilities67### render89Renders a Svelte component on the server and returns HTML string with CSS and metadata.1011```typescript { .api }12/**13* Render a component on the server14* @param component - Component to render15* @param options - Server rendering options16* @returns Rendered HTML with CSS and metadata17*/18function render<Comp extends Component<any>>(19component: Comp,20options?: RenderOptions<ComponentProps<Comp>>21): RenderOutput;22```2324**Usage Examples:**2526```typescript27import { render } from "svelte/server";28import App from "./App.svelte";2930// Basic server rendering31const result = render(App, {32props: {33title: "My App",34initialData: { users: [], posts: [] }35}36});3738console.log(result.body); // HTML string39console.log(result.css?.code); // CSS string (if any)40console.log(result.head); // Head elements4142// Express.js integration43app.get("*", async (req, res) => {44const { body, head, css } = render(App, {45props: {46url: req.url,47user: req.user48}49});5051const html = `52<!DOCTYPE html>53<html>54<head>55<title>My App</title>56${head}57${css ? `<style>${css.code}</style>` : ""}58</head>59<body>60<div id="app">${body}</div>61<script src="/build/bundle.js"></script>62</body>63</html>64`;6566res.send(html);67});6869// With context and props70const result = render(App, {71props: {72initialState: serverData73},74context: new Map([75["theme", { mode: "dark" }],76["api", { baseUrl: process.env.API_URL }]77])78});79```8081### SvelteKit Integration8283```typescript84// In SvelteKit, SSR is handled automatically85// src/routes/+page.server.js86export async function load({ params, url, cookies }) {87const data = await fetchData(params.id);8889return {90props: {91data,92url: url.pathname93}94};95}9697// src/routes/+page.svelte98let { data } = $props();99100// This component will be server-rendered with the data101```102103### Hydration Considerations104105```typescript106// Client-side hydration107import { hydrate } from "svelte";108import App from "./App.svelte";109110// Hydrate server-rendered content111const app = hydrate(App, {112target: document.getElementById("app"),113props: {114// Must match server-side props115initialData: window.__INITIAL_DATA__116}117});118119// Handle hydration mismatches120const app = hydrate(App, {121target: document.getElementById("app"),122props: serverProps,123recover: true // Attempt to recover from mismatches124});125```126127## SSR-Specific Patterns128129### Environment Detection130131```typescript132import { browser } from "$app/environment"; // SvelteKit133// or134import { BROWSER } from "esm-env"; // Universal135136let data = $state([]);137138// Only run on client139if (browser || BROWSER) {140// Browser-only code141data = JSON.parse(localStorage.getItem("data") || "[]");142}143144// Server-safe initialization145onMount(() => {146// This only runs on the client147initializeClientOnlyFeatures();148});149```150151### Conditional Rendering152153```typescript154let mounted = $state(false);155156onMount(() => {157mounted = true;158});159160// Template161/*162{#if mounted}163<ClientOnlyComponent />164{:else}165<div>Loading...</div>166{/if}167*/168```169170### API Integration171172```typescript173// Server-side data loading174export async function load({ fetch }) {175const response = await fetch("/api/data");176const data = await response.json();177178return {179props: { data }180};181}182183// Component receives pre-loaded data184let { data } = $props();185186// Client-side updates still work187async function refreshData() {188const response = await fetch("/api/data");189data = await response.json();190}191```192193## Types194195```typescript { .api }196interface RenderOptions<Props extends Record<string, any> = Record<string, any>> {197/** Component properties */198props?: Props;199/** Context map for the component tree */200context?: Map<any, any>;201}202203interface RenderOutput {204/** Rendered HTML body */205body: string;206/** CSS code (null if no styles) */207css: null | {208code: string;209map: SourceMap;210};211/** Head elements to insert */212head: string;213/** Server-side warnings */214warnings: Warning[];215}216217interface HydrateOptions<Props extends Record<string, any> = Record<string, any>> {218/** Target element containing server-rendered content */219target: Document | Element | ShadowRoot;220/** Component properties (must match server props) */221props?: Props;222/** Context map accessible via getContext() */223context?: Map<any, any>;224/** Whether to play transitions during hydration */225intro?: boolean;226/** Attempt to recover from hydration mismatches */227recover?: boolean;228}229```230231## Best Practices232233### Data Serialization234235```typescript236// Serialize complex data safely237function serializeData(data) {238return JSON.stringify(data, (key, value) => {239if (value instanceof Date) {240return { __type: "Date", value: value.toISOString() };241}242return value;243});244}245246// Deserialize on client247function deserializeData(serialized) {248return JSON.parse(serialized, (key, value) => {249if (value && value.__type === "Date") {250return new Date(value.value);251}252return value;253});254}255```256257### Head Management258259```typescript260// In SvelteKit261import { page } from "$app/stores";262263// Dynamic head content264$effect(() => {265document.title = `${$page.data.title} - My App`;266267// Meta tags268let metaDescription = document.querySelector('meta[name="description"]');269if (metaDescription) {270metaDescription.content = $page.data.description;271}272});273```274275### Progressive Enhancement276277```typescript278let enhanced = $state(false);279280onMount(() => {281enhanced = true;282});283284// Template with progressive enhancement285/*286<form method="POST" action="/api/submit">287<input name="email" type="email" required />288289{#if enhanced}290<button type="button" on:click={handleClientSubmit}>291Submit (Enhanced)292</button>293{:else}294<button type="submit">295Submit296</button>297{/if}298</form>299*/300301async function handleClientSubmit(event) {302event.preventDefault();303// Enhanced client-side submission304const formData = new FormData(event.target.form);305const response = await fetch("/api/submit", {306method: "POST",307body: formData308});309// Handle response...310}311```312313## Common Patterns3143151. **Data loading**: Use server-side load functions to pre-populate component props3162. **Environment detection**: Check `browser` or `BROWSER` flags for client-only code3173. **Graceful degradation**: Ensure forms and navigation work without JavaScript3184. **Hydration matching**: Server and client props must match exactly3195. **Progressive enhancement**: Add client-side features while maintaining server functionality3206. **Head management**: Use `svelte:head` or meta frameworks for SEO tags3217. **Error boundaries**: Handle both server and client errors appropriately