SolidStart Routing — SolidJS

SolidStart uses file-based routing: every file in src/routes becomes one address (index.tsx becomes /, about.tsx becomes /about). A file name with square…

Add one about.tsx file to the src/routes folder, and the Nadia's Bookshop site has an /about page right away. There's no route list for you to edit. That's file-based routing: every file in src/routes automatically becomes one address on your site, without registering them one by one. It's like a book's table of contents: each line points to one page, and here the table of contents is the src/routes folder.

This comes from SolidStart, a meta-framework for Solid: Solid plus a server, data loading and file-based routing. If you've used Next.js with React, it plays a similar role.

To create the project: run npm create solid@latest bookshop, pick SolidStart (Solid 1.x), then version 2 (Stable). SolidStart 2 (@solidjs/start 2.2.2) needs Node.js 24 or newer. Its config is in vite.config.ts, which adds the plugins solidStart() and nitro(). A tutorial that uses app.config.ts was written for SolidStart 1, now labelled Legacy.

Files in src/routes become addresses

The Nadia's Bookshop site as a SolidStart project:

src/
├── app.tsx
└── routes/
    ├── index.tsx        → /
    ├── about.tsx        → /about
    ├── books.tsx         → layout for every /books/...
    ├── books/
    │   └── [id].tsx     → /books/1, /books/2, /books/3, ...
    └── [...404].tsx     → any other address that doesn't match

app.tsx: Router and FileRoutes

src/app.tsx sets up the router. Its shape follows the SolidStart template:

// src/app.tsx
import { Router } from "@solidjs/router"
import { FileRoutes } from "@solidjs/start/router"
import { Suspense } from "solid-js"
import Nav from "./components/Nav"

export default function App() {
  return (
    <Router
      root={(props) => (
        <>
          <Nav />
          <Suspense>{props.children}</Suspense>
        </>
      )}
    >
      <FileRoutes />
    </Router>
  )
}

Nav: links with A

import { A } from "@solidjs/router"

function Nav() {
  return (
    <nav>
      <A href="/">Home</A>
      <A href="/about">About</A>
    </nav>
  )
}

Dynamic routes: [id].tsx and useParams

One file serves every book. A part of the address written in square brackets in the file name becomes a parameter:

// src/routes/books/[id].tsx
import { useParams } from "@solidjs/router"
import { Show } from "solid-js"

const catalog = [
  { id: 1, title: "The Rainbow Troops", price: 18, stock: 4 },
  { id: 2, title: "This Earth of Mankind", price: 24, stock: 2 },
  { id: 3, title: "Beauty Is a Wound", price: 19, stock: 0 },
]

export default function BookDetail() {
  const params = useParams()
  const book = () => catalog.find((b) => b.id === Number(params.id))

  return (
    <Show when={book()} fallback={<p>Book not found</p>}>
      <h2>{book().title}</h2>
      <p>${book().price} · stock {book().stock}</p>
    </Show>
  )
}
Address params.id Page
/books/1 "1" The Rainbow Troops, $18 · stock 4
/books/2 "2" This Earth of Mankind, $24 · stock 2
/books/99 "99" Book not found

Read params where they are used

params is reactive like a store: it changes when the address changes. Moving from /books/1 to /books/2 uses the same route, so the component doesn't run again. Tested with @solidjs/router 1.0:

Written After clicking from /books/1 to /books/2
const id = Number(params.id) in the component body stays The Rainbow Troops
const book = () => catalog.find((b) => b.id === Number(params.id)) This Earth of Mankind

The title stays The Rainbow Troops. The component body runs once, so id only holds 1. A read of params.id inside a function that the JSX calls is read again every time the address changes.

Layout: a file named like its folder

// src/routes/books.tsx
import { A } from "@solidjs/router"

export default function BookShelf(props) {
  return (
    <section class="shelf">
      <A href="/">Back to the shelf</A>
      {props.children}
    </section>
  )
}

Catch-all: [...404].tsx

Three dots inside the brackets catch the rest of the address, however long. [...404].tsx in src/routes serves any address no other file matches, /promo for one, and is used as the 404 page. Without that file, /promo shows only Nav with nothing under it.

⚠️ Links that stay active, and a book title that won't change

TL;DR: SolidStart uses file-based routing: every file in src/routes becomes one address (index.tsx becomes /, about.tsx becomes /about). A file name with square brackets, books/[id].tsx, becomes a dynamic route whose value is read through useParams() as a string, and must be read where it is used, not stored once. A file named like its folder becomes a layout with {props.children}, and [...404].tsx catches the addresses that don't match. app.tsx sets up <Router root={...}><FileRoutes /></Router>. Links are written <A href="/about">About</A> from "@solidjs/router": navigation without reloading the page, plus the active class. Add end to the / link.

⚡ Files in src/routes become addresses

Write:

src/routes/
├── index.tsx        → /
├── about.tsx        → /about
└── books/
    └── [id].tsx     → /books/1, /books/2, ...

Meaning: Every file in src/routes automatically becomes one address, with no route list for you to edit. index.tsx serves its folder's own address, and every page file does an export default of one component.

⚡ Links with A

Write:

<nav>
  <A href="/">Home</A>
  <A href="/about">About</A>
</nav>

Meaning: A from "@solidjs/router" draws an ordinary <a>, but the page is swapped in the browser without reloading everything, and the link that matches the current address gets the class active. This exercise checks the order and the addresses exactly: / first, then /about.

⚡ Dynamic routes with square brackets

Write:

// src/routes/books/[id].tsx
const params = useParams()
const book = () => catalog.find((b) => b.id === Number(params.id))
Guess first: if you write const id = Number(params.id) in the component body, then move from /books/1 to /books/2, what does the title become?

It stays The Rainbow Troops. The route is the same, so the component doesn't run again, and id only holds 1.

Meaning: A part of the file name in square brackets becomes a parameter, read through useParams() as a string. Read it where it's used, inside a function or right in the JSX.

What you will learn