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
- A file's name becomes part of the address:
about.tsxserves/about. A folder adds one part:books/[id].tsxserves/books/.... index.tsxserves its folder's own address:routes/index.tsxis the home page/.- Every page file does an
export defaultof one component. That component is drawn when its address is opened.
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>
)
}
<FileRoutes />reads what is insrc/routesand turns it into a list of routes. You don't write<Route path=...>yourself.rootis the frame around every page.props.childrenholds the page that matches the address, soNavshows above every page.<Suspense>holds a page whose data is still loading. Data loading is the next lesson.
Nav: links with A
import { A } from "@solidjs/router"
function Nav() {
return (
<nav>
<A href="/">Home</A>
<A href="/about">About</A>
</nav>
)
}
Ais imported from"@solidjs/router". It draws an ordinary<a>with the samehref, so<A href="/">Home</A>becomes<a href="/">Home</a>plus a few attributes.- This is the exercise: one
<nav>, a first link to/labelledHome, a second link to/aboutlabelledAbout, in exactly that order with exactly those addresses. - The router catches the link's click: the page is swapped in the browser without reloading the whole page, and
Navisn't built again. Aadds the classactiveto the link that matches the current address (the others getinactive), andaria-current="page"to the link whose address is exactly the same. That is what lets a menu bold the page that is open.
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 |
[id].tsxmatches any part of the address, and its value is read throughuseParams()under the name inside the brackets:params.id.- The value is always a string.
Number(params.id)converts it so it can be compared with the numericidin the catalogue.
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>
)
}
books.tsxnext to thebooks/folder wraps every page inside that folder.props.childrenis the matchingbooks/[id].tsxpage.- Moving between books,
BookShelfstays mounted. Only its content changes.
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
<A href="/">is active on every page. The classactiveis set when the current address starts with the link's address, and every address starts with/. Tested: on/about, Home and About are bothactive. Write<A href="/" end>Home</A>so Home is only active on/.- The parameter is read once.
const id = params.idin the component body doesn't follow a move to another book. Read it inside a function or right in the JSX. - A layout without
{props.children}. The pages in its folder have nowhere to go, so they never show. - Links outside the
<nav>, or in the wrong order. The exercise looks fornav a[href="/"]as the first link and/aboutas the second, with the textsHomeandAbout.
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.