Skip to content

Formatters & Code

SvelteKit Component + Page Generator

+page.svelte, +page.server.ts and load functions in one go.

Runs in your browser
src/routes/blog/[slug]/+page.svelte
lines: 13chars: 229size: 229 B
live
src/routes/blog/[slug]/+page.server.ts
lines: 14chars: 360size: 360 B
live

Understanding SvelteKit routes

+page, +page.server, +layout — that's it.

SvelteKit's filesystem routing, the three load patterns, and the form actions that make server mutations feel idiomatic.

The plus-prefix files.

SvelteKit uses +-prefixed filenames to mark special files inside route folders. +page.svelte: the route's UI. +page.ts: a load function that runs on both server and client (universal data loading).+page.server.ts: a load function that runs only on the server (database queries, secrets). +layout.svelte + companion ts files: parent layouts. +server.ts: an API route returning JSON.

The three load patterns.

Pattern 1: pure client data — fetch from the browser, no server involvement. Pattern 2: universal load — fetch from either side, useful for static content that should also hydrate on the client. Pattern 3: server-only load — runs on the server only, ships the result as JSON to the client. The choice is the same as Next.js's server/client component split, framed slightly differently. Use server-only for anything secret; universal for cacheable read data; client-only for genuinely client-state-dependent fetches.

A worked route.

A blog post route at routes/blog/[slug]. +page.server.ts exports a load function: export async function load({ params }) { const post = await db.posts.findUnique({ where: { slug: params.slug } }); if (!post) error(404); return { post }; } +page.svelte consumes via {export let data} or (Svelte 5) let { data } = $props(). The post object is fetched on the server and serialised into the page; no client-side fetch needed for the initial render.

Server load → page render

+page.server.ts → +page.svelte

The server load returns data; the page receives it as a prop.

load returns { post } → page uses data.post

= SSR with hydration

Form actions.

SvelteKit's standout feature for mutations: +page.server.ts can export an actions object whose keys are form action names. <form method="POST"> in the page submits to the default action; <form action="?/login"> submits to the named one. Each handler runs on the server, has access to the form data, returns a response. Works without JavaScript (progressive enhancement is automatic); enhances with JS to avoid the full reload.

Layouts compose top-down.

A +layout.svelte at any folder wraps the route and every nested route below. Nested layouts compose: the root layout wraps everything, an (app) route group's layout wraps just the authenticated app pages. Same idea as Next.js's layout.tsx; same default (persistent across navigation, doesn't re-render unless its own dependencies change).

SvelteKit vs Next.js, the honest comparison.

Same shape (filesystem routing, server-rendered pages, hydrated islands, form actions). SvelteKit's runtime is smaller (Svelte compiles to vanilla JS; React is heavier). Next.js has more deployment lock-in to Vercel (though self-hosting is possible) and more breaking changes per major. SvelteKit's ecosystem is smaller; React's wins for component-library availability. Pick based on what your team already knows; both are excellent frameworks.

Frequently asked questions

Quick answers.

What is the difference between +page.ts and +page.server.ts?

`+page.ts` runs on both the server and the client for universal data fetching, while `+page.server.ts` only runs on the server. Use the server version for operations requiring private environment variables or direct database access.

Are the generated files saved to my disk?

No. This tool generates the code text which you can then copy and paste into your local IDE. It does not have permission to modify your local filesystem directly.

Does this support Svelte 5 snippets and runes?

Yes. You can toggle between legacy Svelte 4 syntax and the newer Svelte 5 runes including `$state` and `$derived` within the settings.

Can I include TypeScript by default?

Yes. The generator defaults to TypeScript syntax for script blocks and load functions, but you can switch to plain JavaScript if your project requires it.

People also search for

Related tools

More in this room.

See all in Formatters & Code