You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: teach Solid Router nested routes, server rendering, setup, and route definitions
- Nested routes: rewritten around the store's account and collections
sections. Explains what stays mounted and why, pathless layouts as a
sign-in check, parameters flowing down, parallel preloads at every level,
sharing layout data through context (Solid 2 provider syntax), and a
common-problems section.
- Server rendering: rewritten from observed behavior (no data request on
load, one round trip per mutation, forms work with JS off) to the
mechanism and the switches, using the fullstack template's
server-config.ts as the reference wiring.
- Route definitions: preload examples now start the query with void and
read it through a memo; props.data is described as captured once rather
than shown as an awaited value.
- Setup: opens with who should read it and turns the option list into a
when-to-reach-for-it table.
Co-authored-by: Cursor <cursoragent@cursor.com>
Copy file name to clipboardExpand all lines: src/routes/(4)routing/(1)solid-router/(1)setup.mdx
+20-17Lines changed: 20 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,10 @@ version: "2.0"
4
4
description: "Install Solid Router, create a route tree, and mount the router instance."
5
5
---
6
6
7
-
Install Solid Router in a Solid 2 application:
7
+
The `basic` and `fullstack` templates from `npm create solid` already install and mount Solid Router, so if you started from one of them, skip to [Configure the router](#configure-the-router) when you need to change an option.
8
+
The rest of this page is for adding the router to a project that does not have it, such as one created from the `bare` shape or an existing Vite app.
The `const` type parameter preserves path literals that TypeScript would otherwise widen.
21
-
`createRouter({ routes })` can then infer typed paths from the array.
22
-
An inline array passed directly to `createRouter` already receives literal inference.
20
+
The route array is also where the types come from.
21
+
`paths.about` exists and `paths.abuot` does not because TypeScript kept the literal `"/about"`.
22
+
An inline array passed to `createRouter` keeps its literals on its own; an array assigned to a variable first would widen to `string`, so wrap it in `defineRoutes` to preserve them.
23
23
24
24
## Type a route at its definition
25
25
26
-
`defineRoute` is an identity helper that types a route component and preload from the route's own path pattern.
26
+
Inside a route's own `component` and `preload`, `params` is an open record by default: every key is `string | undefined`, even `:id`, which the pattern guarantees.
27
+
Wrap the route in `defineRoute` and both are typed from the route's own `path`:
Required parameters such as `:id` are typed as `string`.
41
42
Optional parameters such as `:tab?` are typed as `string | undefined`.
42
-
The return type of `preload` becomes the component's `props.data` type.
43
43
Parameters inherited from a parent remain accessible as `string | undefined`.
44
44
45
+
Whatever `preload` returns is typed as the component's `props.data`.
46
+
It is captured once when the route matches, so for async data the pattern on the [Data](/routing/solid-router/data) page is to start the query in `preload` with `void` and read it through a memo in the component, where it stays reactive to `params`.
47
+
45
48
For a component declared in another module, annotate it with a path witness:
46
49
47
50
```tsx
@@ -197,21 +200,25 @@ An optional named `route` export can supply:
const post =createMemo(() =>getPost(props.params.id));
215
+
return <h1>{post().title}</h1>;
210
216
}
211
217
```
212
218
213
-
The string passed to `defineFileRoute` is a type witness for the file's path.
214
-
The manifest path remains the runtime source of truth.
219
+
Inside a route file the pattern lives in the filename, so there is no `paths` node to type against.
220
+
The string passed to `defineFileRoute` stands in for it: it types `preload`'s params, validates `matchFilters`, and lets the config double as the component's `RouteProps` witness.
221
+
The manifest path remains the runtime source of truth; if the file moves, update the string with it.
215
222
When the manifest has generated literal types, route paths, filters, and search schemas continue into `Router.paths`.
216
223
217
224
Code-split manifest components become Solid `lazy` components.
0 commit comments