📖 Scenario: You are building a simple web app that shows a list of books and their details. You want to understand how SvelteKit manages routing for both the pages users see and the server-side data fetching.
🎯 Goal: Build a small SvelteKit app that demonstrates full-stack routing by creating a page that lists books and a server route that provides the book data.
📋 What You'll Learn
Create a SvelteKit page route to display book titles
Create a server route to provide book data as JSON
Use the load function in the page to fetch data from the server route
Understand how SvelteKit connects page routes and server routes for full-stack routing
💡 Why This Matters
🌍 Real World
Full-stack routing in SvelteKit lets you build apps where the server provides data and the client displays it seamlessly. This is common in blogs, shops, and dashboards.
💼 Career
Understanding full-stack routing is key for SvelteKit developers to build efficient, maintainable web apps that handle both server and client logic in one framework.
Progress0 / 4 steps
1
Set up the book data in a server route
Create a file called src/routes/api/books/+server.js and export a GET handler that returns a JSON response with this exact array of books: [{ id: 1, title: 'Svelte Basics' }, { id: 2, title: 'Advanced SvelteKit' }]. Use the json helper from @sveltejs/kit.
Svelte
Hint
Use export function GET() to create the server route handler and return the books array using json(books).
2
Create a page route to display books
Create a file called src/routes/books/+page.svelte. Add a <script> block with an exported load function that fetches data from /api/books and returns it as books. Use fetch inside load. Then, add markup to loop over books and display each book's title in a list.
Svelte
Hint
Use export async function load({ fetch }) to get data from the server route, then loop over books with {#each} to show titles.
3
Add a configuration variable for the API path
In src/routes/books/+page.svelte, add a constant called apiPath set to '/api/books'. Then update the fetch call inside load to use this apiPath variable instead of the string literal.
Svelte
Hint
Define const apiPath = '/api/books' and use it in the fetch call inside load.
4
Complete the page with accessibility and semantic HTML
In src/routes/books/+page.svelte, wrap the list in a <section> with an aria-label of 'Book list'. Add a heading <h2> with the text 'Books Available' above the list.
Svelte
Hint
Use a <section> with aria-label="Book list" and add a <h2> heading before the list.
Practice
(1/5)
1. What is the main reason SvelteKit uses a folder structure for routing?
easy
A. To handle both frontend pages and backend routes in one place
B. To separate CSS files from JavaScript files
C. To force developers to write only client-side code
D. To disable server-side rendering by default
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 A
Quick Check:
Unified folder routing = C [OK]
Hint: Remember: SvelteKit folders map to both pages and APIs [OK]
Common Mistakes:
Thinking routing only manages frontend pages
Confusing routing with styling or scripts
Assuming server-side rendering is disabled
2. Which file name in SvelteKit defines a backend API route?
easy
A. src/routes/styles.css
B. src/routes/page.svelte
C. src/routes/api/data.js
D. src/components/Button.svelte
Solution
Step 1: Identify backend API route files
In SvelteKit, files inside src/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 C
Quick Check:
Backend API file = D [OK]
Hint: Backend routes use .js/.ts inside routes folder [OK]
Common Mistakes:
Choosing .svelte files as backend routes
Confusing component files with routes
Selecting CSS files as routes
3. Given this SvelteKit route file src/routes/blog/[slug].svelte, what does it do?
medium
A. Creates a dynamic page for each blog post based on the slug
B. Defines a static page named 'slug' inside blog folder
C. Creates a backend API endpoint for blog data
D. Imports a component named slug from blog folder
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
So src/routes/blog/[slug].svelte makes 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 A
Quick Check:
Dynamic route with [slug] = A [OK]
Hint: Brackets in route names mean dynamic pages [OK]
Common Mistakes:
Thinking it creates a static page named 'slug'
Confusing frontend pages with backend APIs
Assuming it imports a component
4. What is wrong with this SvelteKit backend route file src/routes/api/user.svelte?
medium
A. File must be named user.js instead of user.svelte
B. Backend routes should not use .svelte extension
C. API routes cannot be inside src/routes/api folder
D. Backend routes require a .css file alongside
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 inside src/routes/api/ is correct, but the extension must be .js or .ts.
Final Answer:
Backend routes should not use .svelte extension -> Option B
Quick Check:
Backend route file extension = A [OK]
Hint: Backend routes use .js/.ts, not .svelte [OK]
Common Mistakes:
Using .svelte for backend routes
Thinking API routes can't be in api folder
Believing CSS files are required for backend
5. How does SvelteKit let you fetch data on the server before showing a page?
hard
A. By writing fetch calls inside the onMount lifecycle in .svelte files
B. By placing data files inside the static folder only
C. By importing data directly into the script tag of .svelte files
D. By exporting a load function in the page's .js or .ts file
Solution
Step 1: Understand server-side data fetching in SvelteKit
SvelteKit uses a special load function exported from a page's .js or .ts file to fetch data on the server before rendering.
Step 2: Differentiate from client-side fetching
Using onMount runs only on the client after page loads, so it doesn't fetch data server-side first.
Final Answer:
By exporting a load function in the page's .js or .ts file -> Option D
Quick Check:
Server data fetch = load function export [OK]
Hint: Use load() export for server-side data fetching [OK]
Common Mistakes:
Using onMount for server data fetching
Importing data directly in script tag
Thinking static folder is for dynamic data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.