Skip to content

Commit 8ada8a7

Browse files
feat: get router docs up to date (#886)
1 parent b3f39c8 commit 8ada8a7

File tree

16 files changed

+5399
-4185
lines changed

16 files changed

+5399
-4185
lines changed

pnpm-lock.yaml

Lines changed: 5129 additions & 3941 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/routes/guides/routing-and-navigation.mdx

Lines changed: 145 additions & 157 deletions
Large diffs are not rendered by default.

src/routes/solid-router/reference/components/route.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ title: Route
55
`Route` is the component used when defining the routes of an application.
66
This component is used to define the structure of the application and the components that will be rendered for each route.
77

8-
98
<Callout title="Multiple paths">
109
Routes support defining multiple paths using an array.
1110
This is useful for when you want a route to remain mounted and not re-render when switching between two or more locations that it matches:
@@ -15,12 +14,13 @@ This is useful for when you want a route to remain mounted and not re-render whe
1514
```
1615

1716
This would mean navigating from `/login` to `/register` would not cause the `Login` component to re-render.
17+
1818
</Callout>
1919

20-
| prop | type | description |
21-
| ------------ | --------------- | ----------------------------------------------------------------- |
22-
| path | `string \| string[]` | Path partial for defining the route segment |
23-
| component | `Component` | Component that will be rendered for the matched segment |
24-
| matchFilters | `MatchFilters` | Additional constraints for matching against the route |
25-
| children | `JSX.Element` | Nested `<Route>` definitions |
26-
| load | `RouteLoadFunc` | Function called during preload or when the route is navigated to. |
20+
| prop | type | description |
21+
| ------------ | -------------------- | ----------------------------------------------------------------- |
22+
| path | `string \| string[]` | Path partial for defining the route segment |
23+
| component | `Component` | Component that will be rendered for the matched segment |
24+
| matchFilters | `MatchFilters` | Additional constraints for matching against the route |
25+
| children | `JSX.Element` | Nested `<Route>` definitions |
26+
| preload | `RoutePreloadFunc` | Function called during preload or when the route is navigated to. |

src/routes/solid-router/reference/components/router.mdx

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,25 @@ There is an optional `root` prop that can be used to wrap the entire application
99
import { render } from "solid-js/web";
1010
import { Router, Route } from "@solidjs/router";
1111

12-
const App = props => (
13-
<>
14-
<h1>Root header</h1>
15-
{props.children}
16-
</>
17-
)
12+
const App = (props) => (
13+
<>
14+
<h1>Root header</h1>
15+
{props.children}
16+
</>
17+
);
1818

19-
render(() => (
20-
<Router root={App}>
21-
{/*... routes */}
22-
</Router>
23-
), document.getElementById("app"));
19+
render(
20+
() => <Router root={App}>{/*... routes */}</Router>,
21+
document.getElementById("app")
22+
);
2423
```
2524

26-
| prop | type | description |
27-
| ------------- | -------------------------------------------------------- | ----------------------------------------------------------------------------------- |
28-
| children | `JSX.Element`, `RouteDefinition`, or `RouteDefinition[]` | The route definitions |
29-
| root | Component | Top level layout component |
30-
| base | string | Base url to use for matching routes |
31-
| actionBase | string | Root url for server actions, default: `/_server` |
32-
| preload | boolean | Enables/disables preloads globally, default: `true` |
33-
| explicitLinks | boolean | Disables all anchors being intercepted and instead requires `<A>`. default: `false` |
34-
| url | string | The initial route to render |
25+
| prop | type | description |
26+
| ------------- | -------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
27+
| children | `JSX.Element`, `RouteDefinition`, or `RouteDefinition[]` | The route definitions |
28+
| root | Component | Top level layout component |
29+
| base | string | Base url to use for matching routes |
30+
| actionBase | string | Root url for server actions, default: `/_server` |
31+
| preload | boolean | Enables/disables preloads globally, default: `true` |
32+
| explicitLinks | boolean | Disables all anchors being intercepted and instead requires `<A>`. default: `false`. (To disable interception for a specific link, set `target` to any value, e.g. `<a target="_self">`.) |
33+
| url | string | The initial route to render |

src/routes/solid-router/reference/data-apis/cache.mdx

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ title: cache
66
When this newly created function is called for the first time with a specific set of arguments, the original function is run, and its return value is stored in a cache and returned to the caller of the created function.
77
The next time the created function is called with the same arguments (as long as the cache is still valid), it will return the cached value instead of re-executing the original function.
88

9-
109
<Callout>
1110
`cache` can be defined anywhere and then used inside your components with [`createAsync`](/solid-router/reference/data-apis/create-async).
1211

@@ -18,8 +17,11 @@ However, using `cache` directly in [`createResource`](/reference/basic-reactivit
1817

1918
```js
2019
const getUser = cache(
21-
(id, options = {}) => fetch(`/api/users/${id}?summary=${options.summary || false}`).then(r => r.json()),
22-
"usersById"
20+
(id, options = {}) =>
21+
fetch(`/api/users/${id}?summary=${options.summary || false}`).then((r) =>
22+
r.json()
23+
),
24+
"usersById"
2325
);
2426

2527
getUser(123); // Causes a GET request to /api/users/123?summary=false
@@ -28,9 +30,9 @@ getUser(123, { summary: true }); // Causes a GET request to /api/users/123?summa
2830
setTimeout(() => getUser(123, { summary: true }), 999000); // Eventually causes another GET request to /api/users/123?summary=true
2931
```
3032

31-
### With load functions
33+
### With preload functions
3234

33-
Using it with a [load function](https://docs.solidjs.com/solid-router/reference/load-functions/load):
35+
Using it with a [preload function](/solid-router/reference/preload-functions/preload):
3436

3537
```js
3638
import { lazy } from "solid-js";
@@ -39,13 +41,13 @@ import { getUser } from ... // the cache function
3941

4042
const User = lazy(() => import("./pages/users/[id].js"));
4143

42-
// load function
43-
function loadUser({params, location}) {
44+
// preload function
45+
function preloadUser({params, location}) {
4446
void getUser(params.id)
4547
}
4648

4749
// Pass it in the route definition
48-
<Route path="/users/:id" component={User} load={loadUser} />;
50+
<Route path="/users/:id" component={User} preload={preloadUser} />;
4951
```
5052

5153
### Inside a route's component
@@ -68,7 +70,7 @@ export default function User(props) {
6870

6971
1. Deduping on the server for the lifetime of the request.
7072
2. Preloading the cache in the browser - this lasts 5 seconds.
71-
When a route is preloaded on hover or when load is called when entering a route it will make sure to dedupe calls.
73+
When a route is preloaded on hover or when preload is called when entering a route it will make sure to dedupe calls.
7274
3. A reactive refetch mechanism based on key.
7375
This prevents routes that are not new from retriggering on action revalidation.
7476
4. Serve as a back/forward cache for browser navigation for up to 5 minutes.
@@ -77,24 +79,24 @@ export default function User(props) {
7779

7880
## Cache keys
7981

80-
To ensure that the cache keys are consistent and unique, arguments are deterministically serialized using JSON.stringify.
81-
Before serialization, key/value pairs in objects are sorted so that the order of properties does not affect the serialization.
82+
To ensure that the cache keys are consistent and unique, arguments are deterministically serialized using JSON.stringify.
83+
Before serialization, key/value pairs in objects are sorted so that the order of properties does not affect the serialization.
8284
For instance, both `{ name: 'Ryan', awesome: true }` and `{ awesome: true, name: 'Ryan' }` will serialize to the same string so that they produce the same cache key.
8385

8486
## Return value
8587

8688
The return value is a `CachedFunction`, a function that has the same signature as the function you passed to `cache`.
87-
This cached function stores the return value using the cache key.
89+
This cached function stores the return value using the cache key.
8890
Under most circumstances, this temporarily prevents the passed function from running with the same arguments, even if the created function is called repeatedly.
8991

9092
## Arguments
9193

92-
| argument | type | description |
93-
| -------- | ---- | ----------- |
94-
| `fn` | `(...args: any) => any` | A function whose return value you'd like to be cached. |
95-
| `name`* | string | Any arbitrary string that you'd like to use as the rest of the cache key. |
94+
| argument | type | description |
95+
| -------- | ----------------------- | ------------------------------------------------------------------------- |
96+
| `fn` | `(...args: any) => any` | A function whose return value you'd like to be cached. |
97+
| `name`\* | string | Any arbitrary string that you'd like to use as the rest of the cache key. |
9698

97-
*Since the internal cache is shared by all the functions using `cache`, the string should be unique for each function passed to `cache`.
99+
\*Since the internal cache is shared by all the functions using `cache`, the string should be unique for each function passed to `cache`.
98100
If the same key is used with multiple functions, one function might return the cached result of the other.
99101

100102
## Methods
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: createAsyncStore
3+
---
4+
5+
Similar to [createAsync](/solid-router/reference/data-apis/create-async) except it uses a deeply reactive store. Perfect for applying fine-grained changes to large model data that updates.
6+
7+
```jsx
8+
const todos = createAsyncStore(() => getTodos());
9+
```

src/routes/solid-router/reference/data-apis/data.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"action.mdx",
55
"cache.mdx",
66
"create-async.mdx",
7+
"create-async-store.mdx",
78
"response-helpers.mdx"
89
]
910
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"title": "Reference",
3-
"pages": ["components", "data-apis", "load-functions", "primitives"]
3+
"pages": ["components", "data-apis", "preload-functions", "primitives"]
44
}

src/routes/solid-router/reference/load-functions/data.json

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"title": "Preload Functions",
3+
"pages": ["preload.mdx"]
4+
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
---
2-
title: Load
2+
title: Preload
33
---
44

55
With smart caches waterfalls are still possible with view logic and with lazy loaded code.
6-
With load functions, fetching the data parallel to loading the route is possible to allow use of the data as soon as possible.
7-
The load function can be called when the Route is loaded or eagerly when links are hovered.
6+
With preload functions, fetching the data parallel to loading the route is possible to allow use of the data as soon as possible.
7+
The preload function can be called when the Route is loaded or eagerly when links are hovered.
88

9-
As its only argument, the load function is passed an object that can used to access route information:
9+
As its only argument, the preload function is passed an object that can used to access route information:
1010

1111
```js
1212
import { lazy } from "solid-js";
1313
import { Route } from "@solidjs/router";
1414

1515
const User = lazy(() => import("./pages/users/[id].js"));
1616

17-
// load function
18-
function loadUser({ params, location }) {
19-
// do loading
17+
// preload function
18+
function preloadUser({ params, location }) {
19+
// do preloading
2020
}
2121

2222
// Pass it in the route definition
23-
<Route path="/users/:id" component={User} load={loadUser} />;
23+
<Route path="/users/:id" component={User} preload={preloadUser} />;
2424
```
2525

2626
| key | type | description |
@@ -29,18 +29,18 @@ function loadUser({ params, location }) {
2929
| location | `{ pathname, search, hash, query, state, key}` | An object that used to get more information about the path (corresponds to [`useLocation()`](/solid-router/reference/primitives/use-location)) |
3030
| intent | `"initial", "navigate", "native", "preload"` | Indicates why this function is being called. <ul><li>"initial" - the route is being initially shown (ie page load)</li><li>"native" - navigate originated from the browser (eg back/forward)</li><li>"navigate" - navigate originated from the router (eg call to navigate or anchor clicked)</li><li>"preload" - not navigating, just preloading (eg link hover)</li></ul> |
3131

32-
A common pattern is to export the load function and data wrappers that correspond to a route in a dedicated `route.data.js` file.
32+
A common pattern is to export the preload function and data wrappers that correspond to a route in a dedicated `route.data.js` file.
3333
This imports the data functions without loading anything else.
3434

3535
```js
3636
import { lazy } from "solid-js";
3737
import { Route } from "@solidjs/router";
38-
import loadUser from "./pages/users/[id].data.js";
38+
import preloadUser from "./pages/users/[id].data.js";
3939
const User = lazy(() => import("/pages/users/[id].js"));
4040

4141
// In the Route definition
42-
<Route path="/users/:id" component={User} load={loadUser} />;
42+
<Route path="/users/:id" component={User} preload={preloadUser} />;
4343
```
4444

45-
The return value of the `load` function is passed to the page component when called at anytime other than `preload`.
46-
This initializes things in there, or alternatively the following new Data APIs can be used:
45+
The return value of the `preload` function is passed to the page component when called at anytime other than `preload`.
46+
This initializes things in there, or alternatively the following new `Data APIs` can be used.

src/routes/solid-router/reference/primitives/data.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"use-match.mdx",
88
"use-navigate.mdx",
99
"use-params.mdx",
10-
"use-search-params.mdx"
10+
"use-search-params.mdx",
11+
"use-current-matches.mdx",
12+
"use-preload-route.mdx"
1113
]
1214
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: useCurrentMatches
3+
---
4+
5+
`useCurrentMatches` returns all the matches for the current matched route. Useful for getting all the route information.
6+
7+
For example if you stored breadcrumbs on your route definition you could retrieve them like so:
8+
9+
```js
10+
const matches = useCurrentMatches();
11+
const breadcrumbs = createMemo(() =>
12+
matches().map((m) => m.route.info.breadcrumb)
13+
);
14+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: usePreloadRoute
3+
---
4+
5+
`usePreloadRoute` returns a function that can be used to preload a route manually. This is what happens automatically with link hovering and similar focus based behavior, but it is available here as an API.
6+
7+
```js
8+
const preload = usePreloadRoute();
9+
10+
preload(`/users/settings`, { preloadData: true });
11+
```

src/routes/solid-start/building-your-application/data-loading.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default function Page() {
2323
return (await response.json()) as User[];
2424
});
2525

26-
return <For each={users()}>{(user) => <li>{user.name}</li>}</For>;
26+
return <For each={users()}>{(user) => <li>{user.name}</li>}</For>;
2727
}
2828
```
2929

@@ -60,7 +60,7 @@ export default function Page() {
6060

6161
With this method, however, there are some caveats to be aware of:
6262

63-
1. The [`load`](/solid-router/reference/load-functions/load) function is called **once** per route, which is the first time the user comes to that route.
63+
1. The [`preload`](/solid-router/reference/preload-functions/preload) function is called **once** per route, which is the first time the user comes to that route.
6464
Following that, the fine-grained resources that remain alive synchronize with state/url changes to refetch data when needed.
6565
If the data needs a refresh, the [`refetch`](/guides/fetching-data#refetch) function returned in the `createResource` can be used.
6666
2. Before the route is rendered, the `load` function is called.

0 commit comments

Comments
 (0)