@solidjs/router 0.9.1 → 0.10.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +323 -282
- package/dist/components.d.ts +7 -21
- package/dist/components.jsx +18 -30
- package/dist/data/action.d.ts +8 -0
- package/dist/data/action.js +113 -0
- package/dist/data/cache.d.ts +3 -0
- package/dist/data/cache.js +114 -0
- package/dist/data/createAsync.d.ts +5 -0
- package/dist/data/createAsync.js +54 -0
- package/dist/data/index.d.ts +3 -0
- package/dist/data/index.js +3 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +441 -121
- package/dist/index.jsx +2 -1
- package/dist/routing.d.ts +8 -9
- package/dist/routing.js +159 -72
- package/dist/types.d.ts +26 -20
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +1 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -4,24 +4,23 @@
|
|
|
4
4
|
|
|
5
5
|
# Solid Router [](https://www.npmjs.org/package/@solidjs/router)
|
|
6
6
|
|
|
7
|
-
#### Note: v0.9.0 requires Solid 1.8.4 or later
|
|
8
|
-
|
|
9
7
|
A router lets you change your view based on the URL in the browser. This allows your "single-page" application to simulate a traditional multipage site. To use Solid Router, you specify components called Routes that depend on the value of the URL (the "path"), and the router handles the mechanism of swapping them in and out.
|
|
10
8
|
|
|
11
9
|
Solid Router is a universal router for SolidJS - it works whether you're rendering on the client or on the server. It was inspired by and combines paradigms of React Router and the Ember Router. Routes can be defined directly in your app's template using JSX, but you can also pass your route configuration directly as an object. It also supports nested routing, so navigation can change a part of a component, rather than completely replacing it.
|
|
12
10
|
|
|
13
|
-
It supports all of Solid's SSR methods and has Solid's transitions baked in, so use it freely with suspense, resources, and lazy components. Solid Router also allows you to define a
|
|
11
|
+
It supports all of Solid's SSR methods and has Solid's transitions baked in, so use it freely with suspense, resources, and lazy components. Solid Router also allows you to define a load function that loads parallel to the routes ([render-as-you-fetch](https://epicreact.dev/render-as-you-fetch/)).
|
|
14
12
|
|
|
15
13
|
- [Getting Started](#getting-started)
|
|
16
14
|
- [Set Up the Router](#set-up-the-router)
|
|
17
15
|
- [Configure Your Routes](#configure-your-routes)
|
|
18
|
-
- [Create Links to Your Routes](#create-links-to-your-routes)
|
|
16
|
+
- [Create Links to Your Routes](#create-links-to-your-routes)
|
|
19
17
|
- [Dynamic Routes](#dynamic-routes)
|
|
20
|
-
- [Data Functions](#data-functions)
|
|
21
18
|
- [Nested Routes](#nested-routes)
|
|
22
19
|
- [Hash Mode Router](#hash-mode-router)
|
|
23
20
|
- [Memory Mode Router](#memory-mode-router)
|
|
21
|
+
- [Data APIs](#data-apis)
|
|
24
22
|
- [Config Based Routing](#config-based-routing)
|
|
23
|
+
- [Components](#components)
|
|
25
24
|
- [Router Primitives](#router-primitives)
|
|
26
25
|
- [useParams](#useparams)
|
|
27
26
|
- [useNavigate](#usenavigate)
|
|
@@ -30,7 +29,6 @@ It supports all of Solid's SSR methods and has Solid's transitions baked in, so
|
|
|
30
29
|
- [useIsRouting](#useisrouting)
|
|
31
30
|
- [useRouteData](#useroutedata)
|
|
32
31
|
- [useMatch](#usematch)
|
|
33
|
-
- [useRoutes](#useroutes)
|
|
34
32
|
- [useBeforeLeave](#usebeforeleave)
|
|
35
33
|
- [SPAs in Deployed Environments](#spas-in-deployed-environments)
|
|
36
34
|
|
|
@@ -42,67 +40,64 @@ It supports all of Solid's SSR methods and has Solid's transitions baked in, so
|
|
|
42
40
|
> npm i @solidjs/router
|
|
43
41
|
```
|
|
44
42
|
|
|
45
|
-
Install `@solidjs/router`, then
|
|
43
|
+
Install `@solidjs/router`, then start your application by rendering the router component
|
|
46
44
|
|
|
47
45
|
```jsx
|
|
48
46
|
import { render } from "solid-js/web";
|
|
49
47
|
import { Router } from "@solidjs/router";
|
|
50
|
-
import App from "./App";
|
|
51
48
|
|
|
52
49
|
render(
|
|
53
|
-
() =>
|
|
54
|
-
<Router>
|
|
55
|
-
<App />
|
|
56
|
-
</Router>
|
|
57
|
-
),
|
|
50
|
+
() => <Router />,
|
|
58
51
|
document.getElementById("app")
|
|
59
52
|
);
|
|
60
53
|
```
|
|
61
54
|
|
|
62
|
-
This sets up a
|
|
55
|
+
This sets up a Router that will match on the url to display the desired page
|
|
63
56
|
|
|
64
57
|
### Configure Your Routes
|
|
65
58
|
|
|
66
59
|
Solid Router allows you to configure your routes using JSX:
|
|
67
60
|
|
|
68
|
-
1.
|
|
61
|
+
1. Add each route to a `<Router>` using the `Route` component, specifying a path and an element or component to render when the user navigates to that path.
|
|
69
62
|
|
|
70
63
|
```jsx
|
|
71
|
-
import {
|
|
64
|
+
import { render } from "solid-js/web";
|
|
65
|
+
import { Router, Route } from "@solidjs/router";
|
|
72
66
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
67
|
+
import Home from "./pages/Home";
|
|
68
|
+
import Users from "./pages/Users";
|
|
69
|
+
|
|
70
|
+
render(() => (
|
|
71
|
+
<Router>
|
|
72
|
+
<Route path="/users" component={Users} />
|
|
73
|
+
<Route path="/" component={Home} />
|
|
74
|
+
</Router>
|
|
75
|
+
), document.getElementById("app"));
|
|
81
76
|
```
|
|
77
|
+
2. Provide a root level layout
|
|
82
78
|
|
|
83
|
-
|
|
79
|
+
This will always be there and won't update on page change. It is the ideal place to put top level navigation and Context Providers
|
|
84
80
|
|
|
85
81
|
```jsx
|
|
86
|
-
import {
|
|
82
|
+
import { render } from "solid-js/web";
|
|
83
|
+
import { Router, Route } from "@solidjs/router";
|
|
87
84
|
|
|
88
85
|
import Home from "./pages/Home";
|
|
89
86
|
import Users from "./pages/Users";
|
|
90
87
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
);
|
|
105
|
-
}
|
|
88
|
+
const App = props => (
|
|
89
|
+
<>
|
|
90
|
+
<h1>My Site with lots of pages</h1>
|
|
91
|
+
{props.children}
|
|
92
|
+
</>
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
render(() => (
|
|
96
|
+
<Router root={App}>
|
|
97
|
+
<Route path="/users" component={Users} />
|
|
98
|
+
<Route path="/" component={Home} />
|
|
99
|
+
</Router>
|
|
100
|
+
), document.getElementById("app"));
|
|
106
101
|
```
|
|
107
102
|
|
|
108
103
|
3. Lazy-load route components
|
|
@@ -111,83 +106,56 @@ This way, the `Users` and `Home` components will only be loaded if you're naviga
|
|
|
111
106
|
|
|
112
107
|
```jsx
|
|
113
108
|
import { lazy } from "solid-js";
|
|
114
|
-
import {
|
|
109
|
+
import { render } from "solid-js/web";
|
|
110
|
+
import { Router, Route } from "@solidjs/router";
|
|
111
|
+
|
|
115
112
|
const Users = lazy(() => import("./pages/Users"));
|
|
116
113
|
const Home = lazy(() => import("./pages/Home"));
|
|
117
114
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
);
|
|
132
|
-
}
|
|
115
|
+
const App = props => (
|
|
116
|
+
<>
|
|
117
|
+
<h1>My Site with lots of pages</h1>
|
|
118
|
+
{props.children}
|
|
119
|
+
</>
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
render(() => (
|
|
123
|
+
<Router root={App}>
|
|
124
|
+
<Route path="/users" component={Users} />
|
|
125
|
+
<Route path="/" component={Home} />
|
|
126
|
+
</Router>
|
|
127
|
+
), document.getElementById("app"));
|
|
133
128
|
```
|
|
134
129
|
|
|
135
|
-
|
|
130
|
+
### Create Links to Your Routes
|
|
136
131
|
|
|
137
|
-
Use
|
|
132
|
+
Use an anchor tag that takes you to a route:
|
|
138
133
|
|
|
139
134
|
```jsx
|
|
140
135
|
import { lazy } from "solid-js";
|
|
141
|
-
import {
|
|
136
|
+
import { render } from "solid-js/web";
|
|
137
|
+
import { Router, Route } from "@solidjs/router";
|
|
138
|
+
|
|
142
139
|
const Users = lazy(() => import("./pages/Users"));
|
|
143
140
|
const Home = lazy(() => import("./pages/Home"));
|
|
144
141
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
<
|
|
149
|
-
<
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
<Route path="/" component={Home} />
|
|
156
|
-
<Route
|
|
157
|
-
path="/about"
|
|
158
|
-
element={<div>This site was made with Solid</div>}
|
|
159
|
-
/>
|
|
160
|
-
</Routes>
|
|
161
|
-
</>
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
The `<A>` tag also has an `active` class if its href matches the current location, and `inactive` otherwise. **Note:** By default matching includes locations that are descendents (eg. href `/users` matches locations `/users` and `/users/123`), use the boolean `end` prop to prevent matching these. This is particularly useful for links to the root route `/` which would match everything.
|
|
167
|
-
|
|
168
|
-
| prop | type | description |
|
|
169
|
-
| ------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
170
|
-
| href | string | The path of the route to navigate to. This will be resolved relative to the route that the link is in, but you can preface it with `/` to refer back to the root. |
|
|
171
|
-
| noScroll | boolean | If true, turn off the default behavior of scrolling to the top of the new page |
|
|
172
|
-
| replace | boolean | If true, don't add a new entry to the browser history. (By default, the new page will be added to the browser history, so pressing the back button will take you to the previous route.) |
|
|
173
|
-
| state | unknown | [Push this value](https://developer.mozilla.org/en-US/docs/Web/API/History/pushState) to the history stack when navigating |
|
|
174
|
-
| inactiveClass | string | The class to show when the link is inactive (when the current location doesn't match the link) |
|
|
175
|
-
| activeClass | string | The class to show when the link is active |
|
|
176
|
-
| end | boolean | If `true`, only considers the link to be active when the curent location matches the `href` exactly; if `false`, check if the current location _starts with_ `href` |
|
|
177
|
-
|
|
178
|
-
### The Navigate Component
|
|
179
|
-
|
|
180
|
-
Solid Router provides a `Navigate` component that works similarly to `A`, but it will _immediately_ navigate to the provided path as soon as the component is rendered. It also uses the `href` prop, but you have the additional option of passing a function to `href` that returns a path to navigate to:
|
|
181
|
-
|
|
182
|
-
```jsx
|
|
183
|
-
function getPath({ navigate, location }) {
|
|
184
|
-
// navigate is the result of calling useNavigate(); location is the result of calling useLocation().
|
|
185
|
-
// You can use those to dynamically determine a path to navigate to
|
|
186
|
-
return "/some-path";
|
|
187
|
-
}
|
|
142
|
+
const App = props => (
|
|
143
|
+
<>
|
|
144
|
+
<nav>
|
|
145
|
+
<a href="/about">About</a>
|
|
146
|
+
<a href="/">Home</a>
|
|
147
|
+
</nav>
|
|
148
|
+
<h1>My Site with lots of pages</h1>
|
|
149
|
+
{props.children}
|
|
150
|
+
</>
|
|
151
|
+
);
|
|
188
152
|
|
|
189
|
-
|
|
190
|
-
<
|
|
153
|
+
render(() => (
|
|
154
|
+
<Router root={App}>
|
|
155
|
+
<Route path="/users" component={Users} />
|
|
156
|
+
<Route path="/" component={Home} />
|
|
157
|
+
</Router>
|
|
158
|
+
), document.getElementById("app"));
|
|
191
159
|
```
|
|
192
160
|
|
|
193
161
|
## Dynamic Routes
|
|
@@ -196,52 +164,43 @@ If you don't know the path ahead of time, you might want to treat part of the pa
|
|
|
196
164
|
|
|
197
165
|
```jsx
|
|
198
166
|
import { lazy } from "solid-js";
|
|
199
|
-
import {
|
|
167
|
+
import { render } from "solid-js/web";
|
|
168
|
+
import { Router, Route } from "@solidjs/router";
|
|
169
|
+
|
|
200
170
|
const Users = lazy(() => import("./pages/Users"));
|
|
201
171
|
const User = lazy(() => import("./pages/User"));
|
|
202
172
|
const Home = lazy(() => import("./pages/Home"));
|
|
203
173
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
<Route path="/" component={Home} />
|
|
212
|
-
<Route
|
|
213
|
-
path="/about"
|
|
214
|
-
element={<div>This site was made with Solid</div>}
|
|
215
|
-
/>
|
|
216
|
-
</Routes>
|
|
217
|
-
</>
|
|
218
|
-
);
|
|
219
|
-
}
|
|
174
|
+
render(() => (
|
|
175
|
+
<Router>
|
|
176
|
+
<Route path="/users" component={Users} />
|
|
177
|
+
<Route path="/users/:id" component={User} />
|
|
178
|
+
<Route path="/" component={Home} />
|
|
179
|
+
</Router>
|
|
180
|
+
), document.getElementById("app"));
|
|
220
181
|
```
|
|
221
182
|
|
|
222
183
|
The colon indicates that `id` can be any string, and as long as the URL fits that pattern, the `User` component will show.
|
|
223
184
|
|
|
224
185
|
You can then access that `id` from within a route component with `useParams`.
|
|
225
186
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
187
|
+
**Note on Animation/Transitions**:
|
|
188
|
+
Routes that share the same path match will be treated as the same route. If you want to force re-render you can wrap your component in a keyed `<Show>` like:
|
|
189
|
+
```jsx
|
|
190
|
+
<Show when={params.something} keyed><MyComponent></Show>
|
|
191
|
+
```
|
|
232
192
|
---
|
|
233
193
|
|
|
234
194
|
Each path parameter can be validated using a `MatchFilter`.
|
|
235
195
|
This allows for more complex routing descriptions than just checking the presence of a parameter.
|
|
236
196
|
|
|
237
|
-
```
|
|
197
|
+
```jsx
|
|
238
198
|
import { lazy } from "solid-js";
|
|
239
|
-
import {
|
|
199
|
+
import { render } from "solid-js/web";
|
|
200
|
+
import { Router, Route } from "@solidjs/router";
|
|
240
201
|
import type { SegmentValidators } from "./types";
|
|
241
202
|
|
|
242
|
-
const Users = lazy(() => import("./pages/Users"));
|
|
243
203
|
const User = lazy(() => import("./pages/User"));
|
|
244
|
-
const Home = lazy(() => import("./pages/Home"));
|
|
245
204
|
|
|
246
205
|
const filters: MatchFilters = {
|
|
247
206
|
parent: ["mom", "dad"], // allow enum values
|
|
@@ -249,20 +208,15 @@ const filters: MatchFilters = {
|
|
|
249
208
|
withHtmlExtension: (v: string) => v.length > 5 && v.endsWith(".html"), // we want an `*.html` extension
|
|
250
209
|
};
|
|
251
210
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
/>
|
|
262
|
-
</Routes>
|
|
263
|
-
</>
|
|
264
|
-
);
|
|
265
|
-
}
|
|
211
|
+
render(() => (
|
|
212
|
+
<Router>
|
|
213
|
+
<Route
|
|
214
|
+
path="/users/:parent/:id/:withHtmlExtension"
|
|
215
|
+
component={User}
|
|
216
|
+
matchFilters={filters}
|
|
217
|
+
/>
|
|
218
|
+
</Router>
|
|
219
|
+
), document.getElementById("app"));
|
|
266
220
|
```
|
|
267
221
|
|
|
268
222
|
Here, we have added the `matchFilters` prop. This allows us to validate the `parent`, `id` and `withHtmlExtension` parameters against the filters defined in `filters`.
|
|
@@ -278,26 +232,13 @@ So in this example:
|
|
|
278
232
|
|
|
279
233
|
---
|
|
280
234
|
|
|
281
|
-
```jsx
|
|
282
|
-
// async fetching function
|
|
283
|
-
import { fetchUser } ...
|
|
284
|
-
|
|
285
|
-
export default function User () {
|
|
286
|
-
const params = useParams();
|
|
287
|
-
|
|
288
|
-
const [userData] = createResource(() => params.id, fetchUser);
|
|
289
|
-
|
|
290
|
-
return <A href={userData.twitter}>{userData.name}</A>
|
|
291
|
-
}
|
|
292
|
-
```
|
|
293
|
-
|
|
294
235
|
### Optional Parameters
|
|
295
236
|
|
|
296
237
|
Parameters can be specified as optional by adding a question mark to the end of the parameter name:
|
|
297
238
|
|
|
298
239
|
```jsx
|
|
299
240
|
// Matches stories and stories/123 but not stories/123/comments
|
|
300
|
-
<Route path="/stories/:id?"
|
|
241
|
+
<Route path="/stories/:id?" component={Stories} />
|
|
301
242
|
```
|
|
302
243
|
|
|
303
244
|
### Wildcard Routes
|
|
@@ -312,7 +253,7 @@ Parameters can be specified as optional by adding a question mark to the end of
|
|
|
312
253
|
If you want to expose the wild part of the path to the component as a parameter, you can name it:
|
|
313
254
|
|
|
314
255
|
```jsx
|
|
315
|
-
<Route path="foo/*any"
|
|
256
|
+
<Route path="foo/*any" component={Foo} />
|
|
316
257
|
```
|
|
317
258
|
|
|
318
259
|
Note that the wildcard token must be the last part of the path; `foo/*any/bar` won't create any routes.
|
|
@@ -326,63 +267,6 @@ Routes also support defining multiple paths using an array. This allows a route
|
|
|
326
267
|
<Route path={["login", "register"]} component={Login} />
|
|
327
268
|
```
|
|
328
269
|
|
|
329
|
-
## Data Functions
|
|
330
|
-
|
|
331
|
-
In the [above example](#dynamic-routes), the User component is lazy-loaded and then the data is fetched. With route data functions, we can instead start fetching the data parallel to loading the route, so we can use the data as soon as possible.
|
|
332
|
-
|
|
333
|
-
To do this, create a function that fetches and returns the data using `createResource`. Then pass that function to the `data` prop of the `Route` component.
|
|
334
|
-
|
|
335
|
-
```js
|
|
336
|
-
import { lazy } from "solid-js";
|
|
337
|
-
import { Route } from "@solidjs/router";
|
|
338
|
-
import { fetchUser } ...
|
|
339
|
-
|
|
340
|
-
const User = lazy(() => import("./pages/users/[id].js"));
|
|
341
|
-
|
|
342
|
-
// Data function
|
|
343
|
-
function UserData({params, location, navigate, data}) {
|
|
344
|
-
const [user] = createResource(() => params.id, fetchUser);
|
|
345
|
-
return user;
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
// Pass it in the route definition
|
|
349
|
-
<Route path="/users/:id" component={User} data={UserData} />;
|
|
350
|
-
```
|
|
351
|
-
|
|
352
|
-
When the route is loaded, the data function is called, and the result can be accessed by calling `useRouteData()` in the route component.
|
|
353
|
-
|
|
354
|
-
```jsx
|
|
355
|
-
// pages/users/[id].js
|
|
356
|
-
import { useRouteData } from "@solidjs/router";
|
|
357
|
-
|
|
358
|
-
export default function User() {
|
|
359
|
-
const user = useRouteData();
|
|
360
|
-
return <h1>{user().name}</h1>;
|
|
361
|
-
}
|
|
362
|
-
```
|
|
363
|
-
|
|
364
|
-
As its only argument, the data function is passed an object that you can use to access route information:
|
|
365
|
-
|
|
366
|
-
| key | type | description |
|
|
367
|
-
| -------- | ------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
|
|
368
|
-
| params | object | The route parameters (same value as calling `useParams()` inside the route component) |
|
|
369
|
-
| location | `{ pathname, search, hash, query, state, key}` | An object that you can use to get more information about the path (corresponds to [`useLocation()`](#uselocation)) |
|
|
370
|
-
| navigate | `(to: string, options?: NavigateOptions) => void` | A function that you can call to navigate to a different route instead (corresponds to [`useNavigate()`](#usenavigate)) |
|
|
371
|
-
| data | unknown | The data returned by the [parent's](#nested-routes) data function, if any. (Data will pass through any intermediate nesting.) |
|
|
372
|
-
|
|
373
|
-
A common pattern is to export the data function that corresponds to a route in a dedicated `route.data.js` file. This way, the data function can be imported without loading anything else.
|
|
374
|
-
|
|
375
|
-
```js
|
|
376
|
-
import { lazy } from "solid-js";
|
|
377
|
-
import { Route } from "@solidjs/router";
|
|
378
|
-
import { fetchUser } ...
|
|
379
|
-
import UserData from "./pages/users/[id].data.js";
|
|
380
|
-
const User = lazy(() => import("/pages/users/[id].js"));
|
|
381
|
-
|
|
382
|
-
// In the Route definition
|
|
383
|
-
<Route path="/users/:id" component={User} data={UserData} />;
|
|
384
|
-
```
|
|
385
|
-
|
|
386
270
|
## Nested Routes
|
|
387
271
|
|
|
388
272
|
The following two route definitions have the same result:
|
|
@@ -418,16 +302,14 @@ Only leaf Route nodes (innermost `Route` components) are given a route. If you w
|
|
|
418
302
|
</Route>
|
|
419
303
|
```
|
|
420
304
|
|
|
421
|
-
You can also take advantage of nesting by
|
|
305
|
+
You can also take advantage of nesting by using `props.children` passed to the route component.
|
|
422
306
|
|
|
423
307
|
```jsx
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
function PageWrapper() {
|
|
308
|
+
function PageWrapper(props) {
|
|
427
309
|
return (
|
|
428
310
|
<div>
|
|
429
311
|
<h1> We love our users! </h1>
|
|
430
|
-
|
|
312
|
+
{props.children}
|
|
431
313
|
<A href="/">Back Home</A>
|
|
432
314
|
</div>
|
|
433
315
|
);
|
|
@@ -439,66 +321,180 @@ function PageWrapper() {
|
|
|
439
321
|
</Route>;
|
|
440
322
|
```
|
|
441
323
|
|
|
442
|
-
The routes are still configured the same, but now the route elements will appear inside the parent element where the
|
|
324
|
+
The routes are still configured the same, but now the route elements will appear inside the parent element where the `props.children` was declared.
|
|
443
325
|
|
|
444
326
|
You can nest indefinitely - just remember that only leaf nodes will become their own routes. In this example, the only route created is `/layer1/layer2`, and it appears as three nested divs.
|
|
445
327
|
|
|
446
328
|
```jsx
|
|
447
329
|
<Route
|
|
448
330
|
path="/"
|
|
449
|
-
|
|
331
|
+
component={(props) =>
|
|
450
332
|
<div>
|
|
451
|
-
Onion starts here
|
|
333
|
+
Onion starts here {props.children}
|
|
452
334
|
</div>
|
|
453
335
|
}
|
|
454
336
|
>
|
|
455
337
|
<Route
|
|
456
338
|
path="layer1"
|
|
457
|
-
|
|
339
|
+
component={(props) =>
|
|
458
340
|
<div>
|
|
459
|
-
Another layer
|
|
341
|
+
Another layer {props.children}
|
|
460
342
|
</div>
|
|
461
343
|
}
|
|
462
344
|
>
|
|
463
|
-
<Route path="layer2"
|
|
345
|
+
<Route path="layer2"
|
|
346
|
+
component={() => <div>Innermost layer</div>}> </Route>
|
|
464
347
|
</Route>
|
|
465
348
|
</Route>
|
|
466
349
|
```
|
|
467
350
|
|
|
468
|
-
|
|
351
|
+
## Data APIs
|
|
469
352
|
|
|
470
|
-
|
|
353
|
+
### `cache`
|
|
471
354
|
|
|
472
|
-
|
|
355
|
+
To prevent duplicate fetching and to trigger handle refetching we provide a cache api. That takes a function and returns the same function.
|
|
473
356
|
|
|
474
357
|
```jsx
|
|
475
|
-
|
|
358
|
+
const getUser = cache((id) => {
|
|
359
|
+
return (await fetch(`/api/users${id}`)).json()
|
|
360
|
+
}, "users") // used as cache key + serialized arguments
|
|
361
|
+
```
|
|
362
|
+
It is expected that the arguments to the cache function are serializable.
|
|
363
|
+
|
|
364
|
+
This cache accomplishes the following:
|
|
365
|
+
|
|
366
|
+
1. It does just deduping on the server for the lifetime of the request.
|
|
367
|
+
2. It does preload cache in the browser which lasts 10 seconds. When a route is preloaded on hover or when load is called when entering a route it will make sure to dedupe calls.
|
|
368
|
+
3. We have a reactive refetch mechanism based on key. So we can tell routes that aren't new to retrigger on action revalidation.
|
|
369
|
+
4. It will serve as a back/forward cache for browser navigation up to 5 mins. Any user based navigation or link click bypasses it. Revalidation or new fetch updates the cache.
|
|
370
|
+
|
|
371
|
+
This cache can be defined anywhere and then used inside your components with:
|
|
372
|
+
|
|
373
|
+
### `createAsync`
|
|
476
374
|
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
375
|
+
This is light wrapper over `createResource` that aims to serve as stand-in for a future primitive we intend to bring to Solid core in 2.0. It is a simpler async primitive where the function tracks like `createMemo` and it expects a promise back that it turns into a Signal. Reading it before it is ready causes Suspense/Transitions to trigger.
|
|
376
|
+
|
|
377
|
+
```jsx
|
|
378
|
+
const user = createAsync(() => getUser(params.id))
|
|
480
379
|
```
|
|
481
380
|
|
|
482
|
-
|
|
381
|
+
### `action`
|
|
483
382
|
|
|
484
|
-
|
|
383
|
+
Actions are data mutations that can trigger invalidations and further routing. A list of prebuilt response builders can be found below(TODO).
|
|
384
|
+
```jsx
|
|
385
|
+
// anywhere
|
|
386
|
+
const myAction = action(async (data) => {
|
|
387
|
+
await doMutation(data);
|
|
388
|
+
return redirect("/", {
|
|
389
|
+
invalidate: [getUser, data.id]
|
|
390
|
+
}) // returns a response
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
// in component
|
|
394
|
+
<form action={myAction} />
|
|
395
|
+
|
|
396
|
+
//or
|
|
397
|
+
<button type="submit" formaction={myAction}></button>
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
#### Notes of `<form>` implementation and SSR
|
|
401
|
+
This requires stable references as you can only serialize a string as an attribute, and across SSR they'd need to match. The solution is providing a unique name.
|
|
485
402
|
|
|
486
403
|
```jsx
|
|
487
|
-
|
|
404
|
+
const myAction = action(async (args) => {}, "my-action");
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
### `useAction`
|
|
408
|
+
|
|
409
|
+
Instead of forms you can use actions directly by wrapping them in a `useAction` primitive. This is how we get the router context.
|
|
410
|
+
|
|
411
|
+
```jsx
|
|
412
|
+
// in component
|
|
413
|
+
const submit = useAction(myAction)
|
|
414
|
+
submit(...args)
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
The outside of a form context you can use custom data instead of formData, and these helpers preserve types.
|
|
418
|
+
|
|
419
|
+
### `useSubmission`/`useSubmissions`
|
|
420
|
+
|
|
421
|
+
Are used to injecting the optimistic updates while actions are in flight. They either return a single Submission(latest) or all that match with an optional filter function.
|
|
488
422
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
423
|
+
```jsx
|
|
424
|
+
type Submission<T, U> = {
|
|
425
|
+
input: T;
|
|
426
|
+
result: U;
|
|
427
|
+
error: any;
|
|
428
|
+
pending: boolean
|
|
429
|
+
clear: () => {}
|
|
430
|
+
retry: () => {}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
const submissions = useSubmissions(action, (input) => filter(input));
|
|
434
|
+
const submission = useSubmission(action, (input) => filter(input));
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
### Load Functions
|
|
438
|
+
|
|
439
|
+
Even with the cache API it is possible that we have waterfalls both with view logic and with lazy loaded code. With load functions, we can instead start fetching the data parallel to loading the route, so we can use the data as soon as possible.
|
|
440
|
+
|
|
441
|
+
To do this, we can call our cache function in the load function.
|
|
442
|
+
|
|
443
|
+
```js
|
|
444
|
+
import { lazy } from "solid-js";
|
|
445
|
+
import { Route } from "@solidjs/router";
|
|
446
|
+
import { getUser } from ... // the cache function
|
|
447
|
+
|
|
448
|
+
const User = lazy(() => import("./pages/users/[id].js"));
|
|
449
|
+
|
|
450
|
+
// load function
|
|
451
|
+
function loadUser({params, location}) {
|
|
452
|
+
void getUser(params.id)
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Pass it in the route definition
|
|
456
|
+
<Route path="/users/:id" component={User} load={loadUser} />;
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
The load function is called when the Route is loaded or eagerly when links are hovered. Inside your page component you
|
|
460
|
+
|
|
461
|
+
```jsx
|
|
462
|
+
// pages/users/[id].js
|
|
463
|
+
import { getUser } from ... // the cache function
|
|
464
|
+
|
|
465
|
+
export default function User(props) {
|
|
466
|
+
const user = createAsync(() => getUser(props.params.id));
|
|
467
|
+
return <h1>{user().name}</h1>;
|
|
468
|
+
}
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
As its only argument, the load function is passed an object that you can use to access route information:
|
|
472
|
+
|
|
473
|
+
| key | type | description |
|
|
474
|
+
| -------- | ------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
|
|
475
|
+
| params | object | The route parameters (same value as calling `useParams()` inside the route component) |
|
|
476
|
+
| location | `{ pathname, search, hash, query, state, key}` | An object that you can use to get more information about the path (corresponds to [`useLocation()`](#uselocation)) |
|
|
477
|
+
|
|
478
|
+
A common pattern is to export the preload function and data wrappers that corresponds to a route in a dedicated `route.data.js` file. This way, the data function can be imported without loading anything else.
|
|
479
|
+
|
|
480
|
+
```js
|
|
481
|
+
import { lazy } from "solid-js";
|
|
482
|
+
import { Route } from "@solidjs/router";
|
|
483
|
+
import loadUser from "./pages/users/[id].data.js";
|
|
484
|
+
const User = lazy(() => import("/pages/users/[id].js"));
|
|
485
|
+
|
|
486
|
+
// In the Route definition
|
|
487
|
+
<Route path="/users/:id" component={User} load={loadUser} />;
|
|
492
488
|
```
|
|
493
489
|
|
|
494
490
|
## Config Based Routing
|
|
495
491
|
|
|
496
|
-
You don't have to use JSX to set up your routes; you can pass an object
|
|
492
|
+
You don't have to use JSX to set up your routes; you can pass an object:
|
|
497
493
|
|
|
498
494
|
```jsx
|
|
499
495
|
import { lazy } from "solid-js";
|
|
500
496
|
import { render } from "solid-js/web";
|
|
501
|
-
import { Router
|
|
497
|
+
import { Router } from "@solidjs/router";
|
|
502
498
|
|
|
503
499
|
const routes = [
|
|
504
500
|
{
|
|
@@ -533,32 +529,75 @@ const routes = [
|
|
|
533
529
|
},
|
|
534
530
|
];
|
|
535
531
|
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
return (
|
|
539
|
-
<>
|
|
540
|
-
<h1>Awesome Site</h1>
|
|
541
|
-
<A class="nav" href="/">
|
|
542
|
-
Home
|
|
543
|
-
</A>
|
|
544
|
-
<A class="nav" href="/users">
|
|
545
|
-
Users
|
|
546
|
-
</A>
|
|
547
|
-
<Routes />
|
|
548
|
-
</>
|
|
549
|
-
);
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
render(
|
|
553
|
-
() => (
|
|
554
|
-
<Router>
|
|
555
|
-
<App />
|
|
556
|
-
</Router>
|
|
557
|
-
),
|
|
532
|
+
render(() =>
|
|
533
|
+
<Router>{routes}</Router>,
|
|
558
534
|
document.getElementById("app")
|
|
559
535
|
);
|
|
560
536
|
```
|
|
561
537
|
|
|
538
|
+
## Alternative Routers
|
|
539
|
+
|
|
540
|
+
### Hash Mode Router
|
|
541
|
+
|
|
542
|
+
By default, Solid Router uses `location.pathname` as route path. You can simply switch to hash mode through the `source` property on `<Router>` component.
|
|
543
|
+
|
|
544
|
+
```jsx
|
|
545
|
+
import { Router, hashIntegration } from "@solidjs/router";
|
|
546
|
+
|
|
547
|
+
<Router source={hashIntegration()} />;
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
### Memory Mode Router
|
|
551
|
+
|
|
552
|
+
You can also use memory mode router for testing purpose.
|
|
553
|
+
|
|
554
|
+
```jsx
|
|
555
|
+
import { Router, memoryIntegration } from "@solidjs/router";
|
|
556
|
+
|
|
557
|
+
<Router source={memoryIntegration()} />;
|
|
558
|
+
```
|
|
559
|
+
|
|
560
|
+
## Components
|
|
561
|
+
|
|
562
|
+
### `<A>`
|
|
563
|
+
|
|
564
|
+
Like the `<a>` tag but supports relative paths and active class styling.
|
|
565
|
+
|
|
566
|
+
The `<A>` tag has an `active` class if its href matches the current location, and `inactive` otherwise. **Note:** By default matching includes locations that are descendents (eg. href `/users` matches locations `/users` and `/users/123`), use the boolean `end` prop to prevent matching these. This is particularly useful for links to the root route `/` which would match everything.
|
|
567
|
+
|
|
568
|
+
| prop | type | description |
|
|
569
|
+
| ------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
570
|
+
| href | string | The path of the route to navigate to. This will be resolved relative to the route that the link is in, but you can preface it with `/` to refer back to the root. |
|
|
571
|
+
| noScroll | boolean | If true, turn off the default behavior of scrolling to the top of the new page |
|
|
572
|
+
| replace | boolean | If true, don't add a new entry to the browser history. (By default, the new page will be added to the browser history, so pressing the back button will take you to the previous route.) |
|
|
573
|
+
| state | unknown | [Push this value](https://developer.mozilla.org/en-US/docs/Web/API/History/pushState) to the history stack when navigating |
|
|
574
|
+
| inactiveClass | string | The class to show when the link is inactive (when the current location doesn't match the link) |
|
|
575
|
+
| activeClass | string | The class to show when the link is active |
|
|
576
|
+
| end | boolean | If `true`, only considers the link to be active when the curent location matches the `href` exactly; if `false`, check if the current location _starts with_ `href` |
|
|
577
|
+
|
|
578
|
+
### `<Navigate />`
|
|
579
|
+
|
|
580
|
+
Solid Router provides a `Navigate` component that works similarly to `A`, but it will _immediately_ navigate to the provided path as soon as the component is rendered. It also uses the `href` prop, but you have the additional option of passing a function to `href` that returns a path to navigate to:
|
|
581
|
+
|
|
582
|
+
```jsx
|
|
583
|
+
function getPath({ navigate, location }) {
|
|
584
|
+
// navigate is the result of calling useNavigate(); location is the result of calling useLocation().
|
|
585
|
+
// You can use those to dynamically determine a path to navigate to
|
|
586
|
+
return "/some-path";
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// Navigating to /redirect will redirect you to the result of getPath
|
|
590
|
+
<Route path="/redirect" component={() => <Navigate href={getPath} />} />;
|
|
591
|
+
```
|
|
592
|
+
|
|
593
|
+
### `<Route>`
|
|
594
|
+
|
|
595
|
+
The Component for defining Routes:
|
|
596
|
+
|
|
597
|
+
| prop | type | description |
|
|
598
|
+
|-|-|-|
|
|
599
|
+
|TODO
|
|
600
|
+
|
|
562
601
|
## Router Primitives
|
|
563
602
|
|
|
564
603
|
Solid Router provides a number of primitives that read off the Router and Route context.
|
|
@@ -640,18 +679,6 @@ return (
|
|
|
640
679
|
);
|
|
641
680
|
```
|
|
642
681
|
|
|
643
|
-
### useRouteData
|
|
644
|
-
|
|
645
|
-
Retrieves the return value from the data function.
|
|
646
|
-
|
|
647
|
-
> In previous versions you could use numbers to access parent data. This is no longer supported. Instead the data functions themselves receive the parent data that you can expose through the specific nested routes data.
|
|
648
|
-
|
|
649
|
-
```js
|
|
650
|
-
const user = useRouteData();
|
|
651
|
-
|
|
652
|
-
return <h1>{user().name}</h1>;
|
|
653
|
-
```
|
|
654
|
-
|
|
655
682
|
### useMatch
|
|
656
683
|
|
|
657
684
|
`useMatch` takes an accessor that returns the path and creates a Memo that returns match information if the current path matches the provided path. Useful for determining if a given path matches the current route.
|
|
@@ -662,10 +689,6 @@ const match = useMatch(() => props.href);
|
|
|
662
689
|
return <div classList={{ active: Boolean(match()) }} />;
|
|
663
690
|
```
|
|
664
691
|
|
|
665
|
-
### useRoutes
|
|
666
|
-
|
|
667
|
-
Used to define routes via a config object instead of JSX. See [Config Based Routing](#config-based-routing).
|
|
668
|
-
|
|
669
692
|
### useBeforeLeave
|
|
670
693
|
|
|
671
694
|
`useBeforeLeave` takes a function that will be called prior to leaving a route. The function will be called with:
|
|
@@ -694,6 +717,24 @@ useBeforeLeave((e: BeforeLeaveEventArgs) => {
|
|
|
694
717
|
});
|
|
695
718
|
```
|
|
696
719
|
|
|
720
|
+
## Migrations from 0.8.x
|
|
721
|
+
|
|
722
|
+
v0.9.0 brings some big changes to support the future of routing including Islands/Partial Hydration hybrid solutions. Most notably there is no Context API available in non-hydrating parts of the application.
|
|
723
|
+
|
|
724
|
+
The biggest changes are around removed APIs that need to be replaced.
|
|
725
|
+
|
|
726
|
+
### `<Outlet>`, `<Routes>`, `useRoutes`
|
|
727
|
+
|
|
728
|
+
This is no longer used and instead will use `props.children` passed from into the page components for outlets. Nested Routes inherently cause waterfalls and are Outlets in a sense themselves. We do not want to encourage the pattern and if you must do it you can always nest `<Routers>` with appropriate base path.
|
|
729
|
+
|
|
730
|
+
## `element` prop removed from `Route`
|
|
731
|
+
|
|
732
|
+
Related without Outlet component it has to be passed in manually. At which point the `element` prop has less value. Removing the second way to define route components to reduce confusion and edge cases.
|
|
733
|
+
|
|
734
|
+
### `data` functions & `useRouteData`
|
|
735
|
+
|
|
736
|
+
These have been replaced by a load mechanism. This allows link hover preloads (as the load function can be run as much as wanted without worry about reactivity). It support deduping/cache APIs which give more control over how things are cached. It also addresses TS issues with getting the right types in the Component without `typeof` checks.
|
|
737
|
+
|
|
697
738
|
## SPAs in Deployed Environments
|
|
698
739
|
|
|
699
740
|
When deploying applications that use a client side router that does not rely on Server Side Rendering you need to handle redirects to your index page so that loading from other URLs does not cause your CDN or Hosting to return not found for pages that aren't actually there.
|