Skip to content

Commit 4a50173

Browse files
committed
add notes about route and component preloading
1 parent f07b8f3 commit 4a50173

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,7 @@ The preload function is then passed in the `<Route>` definition:
446446
You can export preload functions and data wrappers that correspond to routes from a dedicated `[route].data.js` or `[route].data.ts` file.
447447
This pattern provides a way to import the data function without loading anything else.
448448

449-
```jsx
450-
// src/pages/users/[id].data.js
449+
```tsx title="src/pages/users/[id].data.js"
451450
import { query } from "@solidjs/router";
452451

453452
export const getUser = query(async (id) => {
@@ -494,8 +493,7 @@ render(
494493
`[id].jsx` contains the component that gets rendered.
495494
When you wrap the function within [`createAsync`](/solid-router/reference/data-apis/create-async) with the imported function, it will yield [a signal](/routes/concepts/signals) once the anticipated promise resolves.
496495

497-
```jsx
498-
// [id].jsx
496+
```tsx title="[id].tsx"
499497
import { createAsync } from "@solidjs/router";
500498
import { getUser } from "./[id].data";
501499

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"title": "Advanced concepts",
3-
"pages": ["lazy-loading.mdx"]
3+
"pages": ["preloading.mdx", "lazy-loading.mdx"]
44
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Preloading
3+
---
4+
5+
When using the [`<A>`](/solid-router/reference/components/a) component from Solid Router, routes are preloaded by default on link hover/focus to improve perceived performance.
6+
7+
To enhance preloading, you can define the `preload` function on your route definition.
8+
When on a [SolidStart](/solid-start) application, this function can also run on the server during the initial page load to fetch data before rendering. When in a Single-Page Application (SPA), it will load the route's component and its `preload` function when the user hovers or focuses on a link.
9+
10+
| user action | route behavior |
11+
| ----------- | -------------------------------------- |
12+
| hover | with a 300ms delay to avoid excessive preloading |
13+
| focus | immediately |
14+
15+
## Imperative Preloading
16+
17+
You can also use the [`usePreloadRoute`](/solid-router/references/use-preload-route) helper to preload routes programmatically in response to events other than link hover/focus, such as button clicks or timers.
18+
This helper will load only the route's component by default, but it can receive a configuration object to also load the data.
19+
20+
## Preloading and Lazy Loading
21+
22+
When a route has nested lazy components, such components will not be part of the route hierarchy, so they **will not** be preloaded with the route. To preload such components, you can use the [`usePreloadRoute`](/solid-router/references/use-preload-route) helper in the parent component to load them when needed.

src/routes/solid-router/reference/primitives/use-preload-route.mdx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@
22
title: usePreloadRoute
33
---
44

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.
5+
`usePreloadRoute` returns a function that can be used to preload a route manually.
66

7-
```js
7+
```ts
88
const preload = usePreloadRoute();
99

1010
preload(`/users/settings`, { preloadData: true });
1111
```
12+
13+
## Usage
14+
15+
Routes are preloaded by default when using the [`<A>`](/solid-router/reference/components/a)` component.
16+
This helper is useful when you want to preload a route in response to some other event, such as a button click or a timer.
17+
18+
## Type Signature
19+
20+
### Parameters
21+
22+
| Parameter | Type | Required | Description |
23+
| --------- | -------- | -------- | ------------------------------------ |
24+
| `to` | `To` | Yes | The route path to preload |
25+
| `options` | `object` | No | Configuration options for preloading |
26+
27+
### Options
28+
29+
| Option | Type | Default | Description |
30+
| ------------- | --------- | ------- | ------------------------------------------------------------------- |
31+
| `preloadData` | `boolean` | `false` | Whether to preload the route's data in addition to the route itself |

0 commit comments

Comments
 (0)