Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 39 ยท SolidStart and Routing

File-Based Routing

How SolidStart turns files in src/routes into URLs, with dynamic and catch-all segments, route groups, nested layouts, links, params and programmatic navigation.

In a SolidStart app you do not write a route table by hand. You create files in src/routes, and the file path becomes the URL. Under the hood the pages are still routes of Solid Router (@solidjs/router), the same library you can use in a plain Solid app, so everything here applies to both: in plain Solid you declare <Route path=... component=.../> yourself, in SolidStart the FileRoutes component generates them.

This lesson covers the naming rules, how layouts nest, and how to read parameters and navigate. The live example is a stand-in: a tiny file-to-URL matcher written in plain Solid so you can experiment with the rules.

From files to URLs

File URL
routes/index.tsx /
routes/about.tsx /about
routes/blog/index.tsx /blog
routes/blog/[slug].tsx /blog/hello-solid (any one segment)
routes/users/[id]/settings.tsx /users/42/settings
routes/[[lang]]/pricing.tsx /pricing and /en/pricing (optional segment)
routes/docs/[...path].tsx /docs/a, /docs/a/b/c (catch-all)
routes/[...404].tsx any URL nothing else matched

The rules in words:

  • index means "the folder itself".
  • Square brackets make a dynamic segment: [id] matches one path segment, and its value is available as the id param.
  • Double brackets [[name]] make the segment optional.
  • [...rest] is a catch-all: it matches the remaining segments, and the param is the rest of the path as a string.
  • A file or folder whose name starts with ( and ends with ), such as (marketing), is a route group: it organizes files but adds nothing to the URL.
  • Files outside routes or inside folders you do not export from are not pages. Components can live next to routes in src/components.

Static routes win over dynamic ones: /blog/new matches blog/new.tsx before blog/[slug].tsx.

A page component

A route file default-exports a component:

TSX
// src/routes/blog/[slug].tsx
import { useParams } from "@solidjs/router";

export default function BlogPost() {
  const params = useParams();               // reactive: params.slug updates on navigation
  return <h1>Post: {params.slug}</h1>;
}

useParams() returns a reactive object. Read params.slug inside JSX or an effect, not once at the top, because navigating from /blog/a to /blog/b reuses the component and only the param changes. That is the same fine-grained idea as everywhere else in Solid.

Nested layouts

A layout wraps several pages. Create a file with the same name as a folder, next to it. The layout renders its child pages through props.children:

Text
routes/
  dashboard.tsx          # layout for everything under /dashboard
  dashboard/
    index.tsx            # /dashboard
    settings.tsx         # /dashboard/settings
TSX
// src/routes/dashboard.tsx
import { A } from "@solidjs/router";
import type { ParentProps } from "solid-js";

export default function DashboardLayout(props: ParentProps) {
  return (
    <div class="layout">
      <aside>
        <A href="/dashboard" end>Overview</A>
        <A href="/dashboard/settings">Settings</A>
      </aside>
      <main>{props.children}</main>
    </div>
  );
}

The sidebar stays mounted while the page inside main changes, so its state (an open menu, a scroll position) survives navigation.

flowchart TD
  R["Root (app.tsx): nav + Suspense"] --> L["dashboard.tsx layout"]
  L --> P1["dashboard/index.tsx"]
  L --> P2["dashboard/settings.tsx"]

Layouts nest. The root wraps layouts, and layouts wrap pages.

Use the router's A component for internal links. It does client-side navigation, so there is no full page reload, and it adds an active class when its target matches the current URL (use the end prop on /dashboard so it is not active for every child).

TSX
import { A, useNavigate, useLocation, useSearchParams } from "@solidjs/router";

<A href="/blog/hello" activeClass="current">Read the post</A>

const navigate = useNavigate();
navigate("/login", { replace: true });        // programmatic, replaces history entry

const location = useLocation();               // location.pathname, location.search
const [search, setSearch] = useSearchParams(); // ?page=2 as a reactive object

Plain <a href> also works; it simply triggers a normal browser navigation to the server. useSearchParams is a convenient place for state that should survive a refresh or be shareable, such as a table's page number and sort order.

Try the matching rules

Stand-in: a file-path to URL matcher

Try /blog/new and /blog/hello: the first hits the static file, the second the dynamic one. /nope falls through to the [...404] catch-all. (Real Solid Router implements more rules, such as optional segments and groups, but the idea of scoring static above dynamic is the same.)

Plain Solid Router: the same thing by hand

Outside SolidStart you declare the same tree in JSX:

JSX
import { Router, Route } from "@solidjs/router";

<Router>
  <Route path="/" component={Home} />
  <Route path="/blog/:slug" component={BlogPost} />
  <Route path="/dashboard" component={DashboardLayout}>
    <Route path="/" component={Overview} />
    <Route path="/settings" component={Settings} />
  </Route>
  <Route path="*404" component={NotFound} />
</Router>

Here :slug is a dynamic segment, nested Routes are children of the layout, and *404 is the catch-all.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file matches the URL /blog/hello-solid?

    Square brackets mark a dynamic segment, so [slug] matches any single segment.
  2. 2. How do you make a layout for every page under /dashboard?

    A file named like the folder wraps the pages inside it.
  3. 3. Why read params.slug inside JSX instead of copying it once at the top?

    Navigating between /blog/a and /blog/b keeps the component alive, so the value must be read reactively.
  4. 4. What does a folder named (marketing) do to the URL?

    Parenthesised folders are route groups: organisation without affecting the path.

Summary

  • Files in src/routes become URLs: index is the folder, [id] is dynamic, [[id]] optional, [...rest] catch-all, (group) invisible.
  • Static routes beat dynamic ones; [...404] handles unmatched URLs.
  • A file with the same name as a folder is its layout and renders props.children.
  • Read params reactively with useParams; navigate with A, useNavigate and useSearchParams.
  • Plain Solid Router uses the same model with explicit <Route> elements.

Next: Route Data: query and createAsync