SvelteKit Routing

16. SvelteKit Routing, Layouts & Page Options

Design scalable SvelteKit file-based routing hierarchies, layout inheritance resets, route groups, and route-level prerender/ssr/csr options.

routing-pages.ts
// SvelteKit Page Options & Routing Topology

// src/routes/dashboard/+page.ts
import type { PageLoad } from './$types';

// 1. Page Options: Control SSR, Prerendering and CSR per-route
export const prerender = true;     // Statically render at build time
export const ssr = true;           // Server-Side Rendering enabled
export const csr = true;           // Client-Side Hydration enabled
export const trailingSlash = 'never';

// 2. Universal Data Load Function
export const load: PageLoad = async ({ fetch, params, parent }) => {
  const parentData = await parent(); // Inherit data from +layout.ts
  const res = await fetch('/api/metrics');
  const metrics = await res.json();

  return { metrics, user: parentData.user };
};

SvelteKit Routing, Layouts & Page Options

SvelteKit's file-system router handles complex routing hierarchies with zero-configuration directory conventions and granular page options.

  • Dynamic & Catch-all Parameters: [param] matches single path segments; [...rest] matches multiple deep segments.
  • Route Groups (group): Organize code and layout boundaries without adding segments to the browser URL.
  • Layout Resets (+layout@): Escape nested layout hierarchies for standalone dashboards or login screens.
  • Page Options: Configure export const prerender = true, export const ssr = false, or export const csr = true per individual route.
SvelteKit Routing Pattern Visualizer File-Based Router

Dynamic Parameter Route

Extracts params.userId in load function with full TypeScript type safety.

File Path: src/routes/users/[userId]/+page.svelte
Browser URL: /users/usr_2026