@solidjs/router 0.9.0 → 0.10.0-beta.0

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 CHANGED
@@ -4,24 +4,23 @@
4
4
 
5
5
  # Solid Router [![npm Version](https://img.shields.io/npm/v/@solidjs/router.svg?style=flat-square)](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 data function that loads parallel to the routes ([render-as-you-fetch](https://epicreact.dev/render-as-you-fetch/)).
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 wrap your root component with the Router component:
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 context so that we can display the routes anywhere in the app.
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. Use the `Routes` component to specify where the routes should appear in your app.
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 { Routes, Route } from "@solidjs/router";
64
+ import { render } from "solid-js/web";
65
+ import { Router, Route } from "@solidjs/router";
72
66
 
73
- export default function App() {
74
- return (
75
- <>
76
- <h1>My Site with Lots of Pages</h1>
77
- <Routes></Routes>
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
- 2. Add each route using the `Route` component, specifying a path and an element or component to render when the user navigates to that path.
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 { Routes, Route } from "@solidjs/router";
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
- export default function App() {
92
- return (
93
- <>
94
- <h1>My Site with Lots of Pages</h1>
95
- <Routes>
96
- <Route path="/users" component={Users} />
97
- <Route path="/" component={Home} />
98
- <Route
99
- path="/about"
100
- element={<div>This site was made with Solid</div>}
101
- />
102
- </Routes>
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 { Routes, Route } from "@solidjs/router";
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
- export default function App() {
119
- return (
120
- <>
121
- <h1>My Site with Lots of Pages</h1>
122
- <Routes>
123
- <Route path="/users" component={Users} />
124
- <Route path="/" component={Home} />
125
- <Route
126
- path="/about"
127
- element={<div>This site was made with Solid</div>}
128
- />
129
- </Routes>
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
- ## Create Links to Your Routes
130
+ ### Create Links to Your Routes
136
131
 
137
- Use the `A` component to create an anchor tag that takes you to a route:
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 { Routes, Route, A } from "@solidjs/router";
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
- export default function App() {
146
- return (
147
- <>
148
- <h1>My Site with Lots of Pages</h1>
149
- <nav>
150
- <A href="/about">About</A>
151
- <A href="/">Home</A>
152
- </nav>
153
- <Routes>
154
- <Route path="/users" component={Users} />
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
- // Navigating to /redirect will redirect you to the result of getPath
190
- <Route path="/redirect" element={<Navigate href={getPath} />} />;
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 { Routes, Route } from "@solidjs/router";
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
- export default function App() {
205
- return (
206
- <>
207
- <h1>My Site with Lots of Pages</h1>
208
- <Routes>
209
- <Route path="/users" component={Users} />
210
- <Route path="/users/:id" component={User} />
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
- > **Note on Animation/Transitions**:
227
- > 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:
228
- > ```js
229
- ><Show when={params.something} keyed><MyComponent></Show>
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
- ```tsx
197
+ ```jsx
238
198
  import { lazy } from "solid-js";
239
- import { Routes, Route } from "@solidjs/router";
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
- export default function App() {
253
- return (
254
- <>
255
- <h1>My Site with Lots of Pages</h1>
256
- <Routes>
257
- <Route
258
- path="/users/:parent/:id/:withHtmlExtension"
259
- component={User}
260
- matchFilters={filters}
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?" element={<Stories />} />
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" element={<div>{useParams().any}</div>} />
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 adding a parent element with an `<Outlet/>`.
305
+ You can also take advantage of nesting by using `props.children` passed to the route component.
422
306
 
423
307
  ```jsx
424
- import { Outlet } from "@solidjs/router";
425
-
426
- function PageWrapper() {
308
+ function PageWrapper(props) {
427
309
  return (
428
310
  <div>
429
311
  <h1> We love our users! </h1>
430
- <Outlet />
312
+ {props.children}
431
313
  <A href="/">Back Home</A>
432
314
  </div>
433
315
  );
@@ -439,66 +321,182 @@ 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 `<Outlet/>` was declared.
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
- element={
331
+ component={(props) =>
450
332
  <div>
451
- Onion starts here <Outlet />
333
+ Onion starts here {props.children}
452
334
  </div>
453
335
  }
454
336
  >
455
337
  <Route
456
338
  path="layer1"
457
- element={
339
+ component={(props) =>
458
340
  <div>
459
- Another layer <Outlet />
341
+ Another layer {props.children}
460
342
  </div>
461
343
  }
462
344
  >
463
- <Route path="layer2" element={<div>Innermost layer</div>}></Route>
345
+ <Route path="layer2"
346
+ component={() => <div>Innermost layer</div>}> </Route>
464
347
  </Route>
465
348
  </Route>
466
349
  ```
467
350
 
468
- If you declare a `data` function on a parent and a child, the result of the parent's data function will be passed to the child's data function as the `data` property of the argument, as described in the last section. This works even if it isn't a direct child, because by default every route forwards its parent's data.
351
+ ## Data APIs
469
352
 
470
- ## Hash Mode Router
353
+ ### `cache`
471
354
 
472
- By default, Solid Router uses `location.pathname` as route path. You can simply switch to hash mode through the `source` property on `<Router>` component.
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
- import { Router, hashIntegration } from "@solidjs/router";
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`
374
+
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 ready causes Suspense/Transitions to trigger.
476
376
 
477
- <Router source={hashIntegration()}>
478
- <App />
479
- </Router>;
377
+ ```jsx
378
+ const user = createAsync(() => getUser(params.id))
480
379
  ```
481
380
 
482
- ## Memory Mode Router
381
+ `createAsync` is designed to only work with cached functions otherwise it will over fetch. If not using `cache`, continue using `createResource` instead.
483
382
 
484
- You can also use memory mode router for testing purpose.
383
+ ### `action`
485
384
 
385
+ Actions are data mutations that can trigger invalidations and further routing. A list of prebuilt response builders can be found below(TODO).
486
386
  ```jsx
487
- import { Router, memoryIntegration } from "@solidjs/router";
387
+ // anywhere
388
+ const myAction = action(async (data) => {
389
+ await doMutation(data);
390
+ return redirect("/", {
391
+ invalidate: [getUser, data.id]
392
+ }) // returns a response
393
+ });
488
394
 
489
- <Router source={memoryIntegration()}>
490
- <App />
491
- </Router>;
395
+ // in component
396
+ <form action={myAction} />
397
+
398
+ //or
399
+ <button type="submit" formaction={myAction}></button>
400
+ ```
401
+
402
+ #### Notes of `<form>` implementation and SSR
403
+ 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.
404
+
405
+ ```jsx
406
+ const myAction = action(async (args) => {}, "my-action");
407
+ ```
408
+
409
+ ### `useAction`
410
+
411
+ Instead of forms you can use actions directly by wrapping them in a `useAction` primitive. This is how we get the router context.
412
+
413
+ ```jsx
414
+ // in component
415
+ const submit = useAction(myAction)
416
+ submit(...args)
417
+ ```
418
+
419
+ The outside of a form context you can use custom data instead of formData, and these helpers preserve types.
420
+
421
+ ### `useSubmission`/`useSubmissions`
422
+
423
+ 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.
424
+
425
+ ```jsx
426
+ type Submission<T, U> = {
427
+ input: T;
428
+ result: U;
429
+ error: any;
430
+ pending: boolean
431
+ clear: () => {}
432
+ retry: () => {}
433
+ }
434
+
435
+ const submissions = useSubmissions(action, (input) => filter(input));
436
+ const submission = useSubmission(action, (input) => filter(input));
437
+ ```
438
+
439
+ ### Load Functions
440
+
441
+ 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.
442
+
443
+ To do this, we can call our cache function in the load function.
444
+
445
+ ```js
446
+ import { lazy } from "solid-js";
447
+ import { Route } from "@solidjs/router";
448
+ import { getUser } from ... // the cache function
449
+
450
+ const User = lazy(() => import("./pages/users/[id].js"));
451
+
452
+ // load function
453
+ function loadUser({params, location}) {
454
+ void getUser(params.id)
455
+ }
456
+
457
+ // Pass it in the route definition
458
+ <Route path="/users/:id" component={User} load={loadUser} />;
459
+ ```
460
+
461
+ The load function is called when the Route is loaded or eagerly when links are hovered. Inside your page component you
462
+
463
+ ```jsx
464
+ // pages/users/[id].js
465
+ import { getUser } from ... // the cache function
466
+
467
+ export default function User(props) {
468
+ const user = createAsync(() => getUser(props.params.id));
469
+ return <h1>{user().name}</h1>;
470
+ }
471
+ ```
472
+
473
+ As its only argument, the load function is passed an object that you can use to access route information:
474
+
475
+ | key | type | description |
476
+ | -------- | ------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
477
+ | params | object | The route parameters (same value as calling `useParams()` inside the route component) |
478
+ | location | `{ pathname, search, hash, query, state, key}` | An object that you can use to get more information about the path (corresponds to [`useLocation()`](#uselocation)) |
479
+
480
+ 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.
481
+
482
+ ```js
483
+ import { lazy } from "solid-js";
484
+ import { Route } from "@solidjs/router";
485
+ import loadUser from "./pages/users/[id].data.js";
486
+ const User = lazy(() => import("/pages/users/[id].js"));
487
+
488
+ // In the Route definition
489
+ <Route path="/users/:id" component={User} load={loadUser} />;
492
490
  ```
493
491
 
494
492
  ## Config Based Routing
495
493
 
496
- You don't have to use JSX to set up your routes; you can pass an object directly with `useRoutes`:
494
+ You don't have to use JSX to set up your routes; you can pass an object:
497
495
 
498
496
  ```jsx
499
497
  import { lazy } from "solid-js";
500
498
  import { render } from "solid-js/web";
501
- import { Router, useRoutes, A } from "@solidjs/router";
499
+ import { Router } from "@solidjs/router";
502
500
 
503
501
  const routes = [
504
502
  {
@@ -533,32 +531,75 @@ const routes = [
533
531
  },
534
532
  ];
535
533
 
536
- function App() {
537
- const Routes = useRoutes(routes);
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
- ),
534
+ render(() =>
535
+ <Router>{routes}</Router>,
558
536
  document.getElementById("app")
559
537
  );
560
538
  ```
561
539
 
540
+ ## Alternative Routers
541
+
542
+ ### Hash Mode Router
543
+
544
+ By default, Solid Router uses `location.pathname` as route path. You can simply switch to hash mode through the `source` property on `<Router>` component.
545
+
546
+ ```jsx
547
+ import { Router, hashIntegration } from "@solidjs/router";
548
+
549
+ <Router source={hashIntegration()} />;
550
+ ```
551
+
552
+ ### Memory Mode Router
553
+
554
+ You can also use memory mode router for testing purpose.
555
+
556
+ ```jsx
557
+ import { Router, memoryIntegration } from "@solidjs/router";
558
+
559
+ <Router source={memoryIntegration()} />;
560
+ ```
561
+
562
+ ## Components
563
+
564
+ ### `<A>`
565
+
566
+ Like the `<a>` tag but supports relative paths and active class styling.
567
+
568
+ 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.
569
+
570
+ | prop | type | description |
571
+ | ------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
572
+ | 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. |
573
+ | noScroll | boolean | If true, turn off the default behavior of scrolling to the top of the new page |
574
+ | 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.) |
575
+ | state | unknown | [Push this value](https://developer.mozilla.org/en-US/docs/Web/API/History/pushState) to the history stack when navigating |
576
+ | inactiveClass | string | The class to show when the link is inactive (when the current location doesn't match the link) |
577
+ | activeClass | string | The class to show when the link is active |
578
+ | 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` |
579
+
580
+ ### `<Navigate />`
581
+
582
+ 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:
583
+
584
+ ```jsx
585
+ function getPath({ navigate, location }) {
586
+ // navigate is the result of calling useNavigate(); location is the result of calling useLocation().
587
+ // You can use those to dynamically determine a path to navigate to
588
+ return "/some-path";
589
+ }
590
+
591
+ // Navigating to /redirect will redirect you to the result of getPath
592
+ <Route path="/redirect" component={() => <Navigate href={getPath} />} />;
593
+ ```
594
+
595
+ ### `<Route>`
596
+
597
+ The Component for defining Routes:
598
+
599
+ | prop | type | description |
600
+ |-|-|-|
601
+ |TODO
602
+
562
603
  ## Router Primitives
563
604
 
564
605
  Solid Router provides a number of primitives that read off the Router and Route context.
@@ -640,18 +681,6 @@ return (
640
681
  );
641
682
  ```
642
683
 
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
684
  ### useMatch
656
685
 
657
686
  `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 +691,6 @@ const match = useMatch(() => props.href);
662
691
  return <div classList={{ active: Boolean(match()) }} />;
663
692
  ```
664
693
 
665
- ### useRoutes
666
-
667
- Used to define routes via a config object instead of JSX. See [Config Based Routing](#config-based-routing).
668
-
669
694
  ### useBeforeLeave
670
695
 
671
696
  `useBeforeLeave` takes a function that will be called prior to leaving a route. The function will be called with:
@@ -694,6 +719,24 @@ useBeforeLeave((e: BeforeLeaveEventArgs) => {
694
719
  });
695
720
  ```
696
721
 
722
+ ## Migrations from 0.8.x
723
+
724
+ 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.
725
+
726
+ The biggest changes are around removed APIs that need to be replaced.
727
+
728
+ ### `<Outlet>`, `<Routes>`, `useRoutes`
729
+
730
+ 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.
731
+
732
+ ## `element` prop removed from `Route`
733
+
734
+ 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.
735
+
736
+ ### `data` functions & `useRouteData`
737
+
738
+ 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.
739
+
697
740
  ## SPAs in Deployed Environments
698
741
 
699
742
  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.