SvelteKit manages routing for both the frontend and backend in one place. This makes building web apps simpler and faster.
Why SvelteKit handles full-stack routing
src/routes/ +page.svelte // Frontend page component +page.server.js // Server-side code for that page +server.js // API endpoints for backend logic
SvelteKit uses special files like +page.svelte for frontend pages and +server.js for backend routes.
This folder structure lets SvelteKit know what code runs on the client and what runs on the server.
src/routes/about/+page.svelte
// This file shows the About page content to users.src/routes/about/+page.server.js export function load() { return { info: 'About info from server' }; }
src/routes/api/data/+server.js export function GET() { return new Response(JSON.stringify({ message: 'Hello from API' }), { status: 200 }); }
This example shows a main page that gets data from the server before showing it. It also has an API route that returns JSON data.
// File: src/routes/+page.svelte <script> export let data; </script> <h1>Welcome to SvelteKit</h1> <p>Message from server: {data.message}</p> // File: src/routes/+page.server.js export function load() { return { message: 'Hello from server-side load!' }; } // File: src/routes/api/hello/+server.js export function GET() { return new Response(JSON.stringify({ greeting: 'Hello from API!' }), { status: 200 }); }
SvelteKit routing combines frontend and backend in one place, reducing setup time.
Server routes run only on the server, so they can safely handle secrets and databases.
Using full-stack routing helps keep your app organized and easier to maintain.
SvelteKit handles both frontend pages and backend routes using a simple folder structure.
This approach makes building full-stack apps easier and faster.
You can fetch data on the server and send it to pages before they load.
Practice
Solution
Step 1: Understand SvelteKit routing design
SvelteKit uses a folder structure to organize routes, which includes both frontend pages and backend endpoints.Step 2: Recognize the benefit of unified routing
This design allows developers to build full-stack apps easily by managing frontend and backend routes together.Final Answer:
To handle both frontend pages and backend routes in one place -> Option AQuick Check:
Unified folder routing = C [OK]
- Thinking routing only manages frontend pages
- Confusing routing with styling or scripts
- Assuming server-side rendering is disabled
Solution
Step 1: Identify backend API route files
In SvelteKit, files insidesrc/routes/api/with .js or .ts extensions define backend endpoints.Step 2: Differentiate frontend and backend files
Files with .svelte are frontend components, and CSS files are for styling, not routes.Final Answer:
src/routes/api/data.js -> Option CQuick Check:
Backend API file = D [OK]
- Choosing .svelte files as backend routes
- Confusing component files with routes
- Selecting CSS files as routes
src/routes/blog/[slug].svelte, what does it do?Solution
Step 1: Understand dynamic route syntax
Square brackets like[slug]in filenames create dynamic routes matching any value in that position.Step 2: Apply to blog route
Sosrc/routes/blog/[slug].sveltemakes a page for each blog post URL like /blog/post1 or /blog/hello.Final Answer:
Creates a dynamic page for each blog post based on the slug -> Option AQuick Check:
Dynamic route with [slug] = A [OK]
- Thinking it creates a static page named 'slug'
- Confusing frontend pages with backend APIs
- Assuming it imports a component
src/routes/api/user.svelte?Solution
Step 1: Check file extension for backend routes
Backend routes in SvelteKit use .js or .ts files, not .svelte which is for frontend components.Step 2: Confirm correct folder usage
Placing backend routes insidesrc/routes/api/is correct, but the extension must be .js or .ts.Final Answer:
Backend routes should not use .svelte extension -> Option BQuick Check:
Backend route file extension = A [OK]
- Using .svelte for backend routes
- Thinking API routes can't be in api folder
- Believing CSS files are required for backend
Solution
Step 1: Understand server-side data fetching in SvelteKit
SvelteKit uses a specialloadfunction exported from a page's .js or .ts file to fetch data on the server before rendering.Step 2: Differentiate from client-side fetching
UsingonMountruns only on the client after page loads, so it doesn't fetch data server-side first.Final Answer:
By exporting aloadfunction in the page's .js or .ts file -> Option DQuick Check:
Server data fetch = load function export [OK]
- Using onMount for server data fetching
- Importing data directly in script tag
- Thinking static folder is for dynamic data
Ready to prove it?
Pass the test and this pattern moves unknown
