Skip to content

How Does SvelteKit File-Based Routing Work?

Web development coding framework

Problem

When I was building a side project with Express, I kept losing track of which route handler lived where. I had routes scattered across multiple files with no obvious connection to URL structure.

From Reddit, a developer shared:

“the file-based routing sold me too. I was building a side project with Express and kept losing track of which route handler lived where. moved to SvelteKit and the project structure basically documented itself.”

This resonated with my experience - I needed a routing system where the structure was self-explanatory.

Environment

  • SvelteKit with file-based routing
  • Express.js comparison (traditional routing)
  • Node.js runtime

What happened?

The Problem with Traditional Routing

With Express, I had to mentally map routes:

routes/users.js
router.get('/users', getUsersHandler);
router.post('/users', createUserHandler);
router.get('/users/:id', getUserByIdHandler);
routes/posts.js
router.get('/posts', getPostsHandler);
router.post('/posts', createPostHandler);
app.js
app.use('/users', usersRouter);
app.use('/posts', postsRouter);

Where is /users/:id? I had to trace through files mentally.

The SvelteKit Solution

SvelteKit maps directory structure directly to URLs:

Directory structure
src/routes/
├── +page.svelte → /
├── about/
│ └── +page.svelte → /about
├── blog/
│ ├── +page.svelte → /blog
│ └── [slug]/
│ └── +page.svelte → /blog/[slug]
└── users/
└── [id]/
└── +page.svelte → /users/[id]

Key Files

  • +page.svelte - Page component for that route
  • +page.ts - Page load function (runs before render)
  • +layout.svelte - Layout component (wraps child routes)
  • +layout.ts - Layout load function
  • +server.ts - Server-side API endpoints
  • +error.svelte - Error page component

Dynamic Routes

Use square brackets for dynamic segments:

src/routes/blog/[slug]/+page.svelte
<script>
export let data;
</script>
<h1>Blog Post: {data.slug}</h1>

This matches /blog/any-slug-here.

Comparison

AspectTraditional (Express)SvelteKit File-Based
Route discoverySearch through filesLook at folder structure
Adding routesCreate file + register routeCreate folder
Route organizationManual conventionsEnforced by filesystem
OnboardingHigh cognitive loadSelf-explanatory
RefactoringError-proneFolder operations

How to solve it?

I started creating routes by adding folders:

  1. Create src/routes/about/+page.svelte/about route exists
  2. Create src/routes/blog/[slug]/+page.svelte/blog/my-post route exists
  3. No configuration files needed

The reason

The key insight is that filesystem hierarchy naturally maps to URL hierarchy. By treating folders as route segments, the project structure becomes self-documenting.

Summary

In this post, I explained how SvelteKit’s file-based routing works. The key point is your filesystem becomes your routing table, eliminating configuration overhead and making routes immediately discoverable by looking at folder structure.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments