Svelte Kit

Introduction

  • SvelteKit is a framework for rapidly developing robust, performant web applications using Sveltearrow-up-right. If you're coming from React, SvelteKit is similar to Next.

Data Pre-rendering

  • SvelteKit allows server-side rendering, but also can be disabled to let application be single app application (spa)

+page.ts
import { error } from '@sveltejs/kit';
import type { PageLoad } from './$types';

/** @type {import('./$types').PageLoad} */
export const load:PageLoad = ({ params }) => {
	if (params.slug === 'hello-world') {
		return {
			title: 'Hello world!',
			content: 'Welcome to our blog. Lorem ipsum dolor sit amet...'
		};
	}

	throw error(404, 'Not found');
}

// Disable SSR
// export const csr = true;
// export const ssr = false;

API

  • The page can act as an api

Hook

  • 'Hooks' are app-wide functions you declare that SvelteKit will call in response to specific events, giving you fine-grained control over the framework's behaviour

  • Every time the SvelteKit server receives a requestarrow-up-right — whether that happens while the app is running, or during prerenderingarrow-up-right — and determines the responsearrow-up-right. It receives an event object representing the request and a function called resolve, which renders the route and generates a Response. This allows you to modify response headers or bodies, or bypass SvelteKit entirely

  • Allows you to modify (or replace) a fetch request that happens inside a load or action function that runs on the server

Last updated

Was this helpful?