9/18 | Validate feature ideas earlier with AI-driven prototypes

What are best AI tools? Take the State of AI survey

Builder.io
Builder.io
Contact sales

9/18 | Validate feature ideas earlier with AI-driven prototypes

What are best AI tools? Take the State of AI survey

Builder.io
Builder.io
< Back to blog

Web Development

SvelteKit Routing : A Visual Guide

July 10, 2023

Written By Vishwas Gopinath

Routing plays a crucial role in the world of web development, allowing seamless navigation between various pages or views within an application. In this blog post, we will explore SvelteKit's routing capabilities with a visual guide.

Getting started

To begin, create and run a new SvelteKit project using the following commands in the terminal:

Open the project in your preferred code editor and expand the src folder. For a fresh start, delete the routes folder to create routes from scratch.

Routing conventions

SvelteKit employs a file-system-based routing mechanism, where URL paths in the browser are determined by files and folders in the codebase. Following conventions is crucial for proper routing functionality. Let's discuss and understand the different conventions.

Creating a route

To create a route for the root URL (localhost:5173), follow these steps:

  • Create a src/routes folder.
  • Inside the routes folder, create a page.svelte file. This file represents the route.
  • Define a simple Svelte component in the page.svelte file, to represent the “Home” page:

By following these conventions, we've successfully created our first route. Open your browser and navigate to localhost:5173 to see the "Welcome home!" message.

Visual representation

Adding additional routes

Let's now proceed to create two more routes: one for the About page and another for the Profile page.

  • Create a src/routes/about folder.
  • Inside the about folder, create a page.svelte file. This file represents the route.
  • Define a minimal Svelte component in the page.svelte file to represent the About page:
  • Similarly, create a src/routes/profile folder.
  • Inside the profile folder, create a page.svelte file. This file represents the route.
  • Define a simple Svelte component in the page.svelte file to represent the Profile page:

When you visit the root URL, localhost:5173, the home page is still= displayed. However, if you navigate to localhost:5173/about, the About me page displays. Similarly, changing the URL to /profile renders the My profile page.

This demonstrates that routes are associated with a file based on the containing folder's name within the routes folder. The page.svelte file within the about folder corresponds to /about, while the page.svelte file within the profile folder corresponds to /profile.

Visual representation

Nested routes

In addition to basic routes, SvelteKit offers support for nested routes, so you can establish a hierarchical structure within your application. Let’s aim to render a page when the user navigates to the URL localhost:5173/blog. Furthermore, we need to render pages for localhost:5173/blog/first and localhost:5173/blog/second.

To implement nested routes, follow these steps:

  1. Create a src/routes/blog folder.
  2. Inside the blog folder, create a page.svelte file for the main blog page:
  1. Navigate to localhost:5173/blog to see the My blog page.
  2. To implement /blog/first and /blog/second routes, create page.svelte files in the src/routes/blog/first and src/routes/blog/second folders:

Now, navigating to localhost:5173/blog/first displays the first blog post, and localhost:5173/blog/second shows the second blog post.

By creating a nested folder structure, SvelteKit automatically routes the files accordingly. This simplifies the process of creating nested routes and enhances the organization and structure of your application.

Visual representation

Dynamic routes

Predefined paths like /blog/first and /blog/second may not always be suitable for complex applications with hundreds of routes. SvelteKit supports dynamic routes to handle such scenarios. Let’s create dynamic routes to handle a product listing and detail page.

To create dynamic routes, follow these steps:

  1. Create a src/routes/products folder.
  2. Inside the products folder, create a page.svelte file to display a list of three products:
  1. By navigating to localhost:5173/products in the browser, the list of products displays.
  2. For the details page, within the products folder, create a new folder named [productId]. The square brackets indicate a dynamic route segment.
  3. Inside the [productId] folder, create a page.svelte file:

Now, when you navigate to localhost:5173/products/1, the product details page displays. Similarly, accessing /products/2, /products/3, or even /products/100 displays the same details page. [productId] is the dynamic route segment that can accommodate values like 1, 2, 3, and so on.

To display the specific product ID, you can make use of the stores module from SvelteKit. Modify the component as follows:

Now, when you navigate to localhost:5173/products/1, the details about product 1 displays. Similarly, visiting /products/100 will display details about product 100.

Dynamic routes are beneficial when implementing the list-detail pattern in any UI application. By understanding how to create dynamic routes in SvelteKit, you can build flexible and scalable applications that adapt to varying user interactions.

Visual representation

Nested dynamic routes

In the previous section, we learned about dynamic routes. Now, let's take it a step further and explore nested dynamic routes.

Complex applications often require multiple dynamic route segments. For instance, when navigating to /products/1, the user expects the details for product 1. Similarly, visiting /products/1/reviews/1 should display the first review for that product. Let's find out how we can achieve this.

To create nested dynamic routes, follow these steps:

  • Create a src/routes/products/[productId]/reviews folder. This structure takes us to the route /products/productId/reviews. However, we also need a dynamic reviewId.
  • Within the reviews folder, create a new folder named [reviewId]. Once again, the square brackets indicate a dynamic route segment.
  • Inside the [reviewId] folder, create a page.svelte file where we'll define a Svelte component to display both the productId and the reviewId.

Now, if we navigate to localhost:5173/products/1/reviews/1 in the browser, the expected text displays. Similarly, navigating to productId 100 and reviewId 5 reflects the corresponding IDs in the UI.

The key takeaway from this section is that it is possible to create nested dynamic routes by having dynamic segments in the folder names.

Visual representation

Catch-all routes

SvelteKit provides the catch-all routes feature which allows for flexible routing. Let's say we want to create a documentation site with multiple features and concepts, where each concept has its own unique route. Instead of creating separate files for each route, we can use a catch-all route.

To implement a catch-all route, follow these steps:

  1. Create a src/routes/docs folder.
  2. Inside the docs folder, create a folder with a special name recognized by SvelteKit. Use square brackets and three dots (for example, [...slug]) to enclose a name of your choice.
  3. Inside the slug folder, create a page.svelte file with a basic Svelte component representing the documentation home page.
  1. To access the different segments in the URL, rely on the stores module that SvelteKit provides. For example: localhost:5173/docs/routing/catch-all-segments can render the following component:

With catch-all segments, you can create a hierarchical structure for your routes, allowing better organization and SEO, while reusing a single file for different variations of the URL. This approach is particularly useful for documentation websites.

Visual representation

Conclusion

Routing is an integral part of web development, so users can navigate between different pages within an application. SvelteKit simplifies routing through its file-system-based routing mechanism.

In this blog post, we explored the basics of routing in SvelteKit. We discussed routing conventions, created routes for different scenarios, and highlighted SvelteKit’s convention over configuration approach for routing.

With SvelteKit, you can easily define and manage routes by leveraging the file and folder structure of your codebase, eliminating the need for additional routing configuration.

Share

Twitter
LinkedIn
Facebook
Share this blog
Copy icon
Twitter "X" icon
LinkedIn icon
Facebook icon

Visually edit your codebase with AI

Using simple prompts or Figma-like controls.

Try it nowGet a demo

Design to Code Automation

A pragmatic guide for engineering leaders and development teams


Continue Reading
design9 MIN
How to generate (actually good) designs with AI
September 17, 2025
AI9 MIN
7 Levels of Context Engineering for Designers
September 16, 2025
Design to Code8 MIN
Git Branching for Designers
September 11, 2025

Product

Visual CMS

Theme Studio for Shopify

Sign up

Login

Featured Integrations

React

Angular

Next.js

Gatsby

Resources

User Guides

Developer Docs

Forum

Blog

Github

Get In Touch

Chat With Us

Twitter

Linkedin

Careers

© 2020 Builder.io, Inc.

Security

Privacy Policy

Terms of Service

Get the latest from Builder.io

By submitting, you agree to our Privacy Policy

  • Fusion

  • Publish

  • Product Updates

  • Design to Code

  • Headless CMS

    Multi-Brand CMS

  • Landing Pages

  • Web Apps

  • Prototypes

  • Marketing Sites

  • Headless Commerce

  • Documentation

  • Fusion Docs

  • Publish Docs

  • Blog

  • Webinars

  • Guides

  • Case Studies

  • Community Forum

  • Partners

  • Affiliate Program

  • CMS Integrations

  • CMS Blueprints

  • Glossary

  • Figma to Code Guide

  • Headless CMS Guide

  • Headless Commerce Guide

  • Composable DXP Guide

  • About

  • Careers

  • Contact Sales

Security

Privacy Policy

SaaS Terms

Compliance

Cookie Preferences

YouTube icon
Github icon
Blsky Icon
Twitter "X" icon
LinkedIn icon
Feed Icon
Gartner Cool Vendor 2024