Skip to content

Commit 833eff7

Browse files
atilafassinaCopilotkodiakhq[bot]mangsLadyBluenotes
authored
content: Expand notes on preloading data (#1306)
Co-authored-by: Copilot <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: Eric L. Goldstein <[email protected]> Co-authored-by: Sarah Gerrard <[email protected]> Co-authored-by: Sarah <[email protected]>
1 parent 88a2b21 commit 833eff7

File tree

5 files changed

+137
-18
lines changed

5 files changed

+137
-18
lines changed

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

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

464-
```jsx
465-
// src/pages/users/[id].data.js
464+
```tsx title="src/pages/users/[id].data.js"
466465
import { query } from "@solidjs/router";
467466

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

512-
```jsx
513-
// [id].jsx
511+
```tsx title="[id].tsx"
514512
import { createAsync } from "@solidjs/router";
515513
import { getUser } from "./[id].data";
516514

src/routes/reference/component-apis/lazy.mdx

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,81 @@ description: >-
1616
performance. Components load on-demand and integrate with Suspense.
1717
---
1818

19-
```ts
19+
The `lazy` helper wraps a dynamic import and returns a component that loads on demand.
20+
Lazy components accept the same props as their eager counterparts and integrate with `<Suspense>` boundaries.
21+
22+
## Import
23+
24+
```tsx
2025
import { lazy } from "solid-js"
21-
import type { Component } from "solid-js"
26+
```
27+
28+
## Type
2229

30+
```tsx
2331
function lazy<T extends Component<any>>(
24-
fn: () => Promise<{ default: T }>
32+
fn: () => Promise<{ default: T }>
2533
): T & { preload: () => Promise<T> }
2634
```
2735

28-
Used to lazy load components to allow for code splitting.
29-
Components are not loaded until rendered.
30-
Lazy loaded components can be used the same as its statically imported counterpart, receiving props etc.
31-
Lazy components trigger `<Suspense>`
36+
## Parameters
37+
38+
### `fn`
39+
40+
- **Type:** `() => Promise<{ default: T }>`
41+
- **Required:** Yes
42+
43+
Dynamic import that resolves to the component module, exposing the component as the `default` export.
44+
45+
## Return value
46+
47+
`lazy` returns a renderable component compatible with `T`.
48+
The component exposes a `preload()` method that resolves the underlying module.
49+
50+
| Property | Type | Description |
51+
| -------- | ---- | ----------- |
52+
| `preload` | `() => Promise<T>` | Loads the module without rendering and returns the resolved component. |
53+
54+
## Examples
55+
56+
### Basic usage
57+
58+
```tsx title="app.tsx"
59+
import { lazy } from "solid-js"
60+
61+
const ComponentA = lazy(() => import("./ComponentA"))
62+
63+
function App(props: { title: string }) {
64+
return <ComponentA title={props.title} />
65+
}
66+
```
67+
68+
### Preloading nested lazy components
3269

3370
```tsx
34-
// wrap import
35-
const ComponentA = lazy(() => import("./ComponentA"));
71+
import { lazy } from "solid-js"
72+
import type { Component } from "solid-js"
73+
74+
const Nested = lazy(() => import("./Nested"))
3675

37-
// use in JSX
38-
<ComponentA title={props.title} />
76+
const ComponentWithPreload = () => {
77+
const [showNested, setShowNested] = createSignal(false)
78+
79+
return (
80+
<div>
81+
<button
82+
onMouseEnter={() => Nested.preload()}
83+
onClick={() => setShowNested(true)}
84+
>Preload Nested Component</button>
85+
<Show when={showNested()}>
86+
<Nested />
87+
</Show>
88+
</div>
89+
)
90+
}
3991
```
92+
93+
## See also
94+
95+
- [`Suspense`](https://docs.solidjs.com/reference/component-apis/suspense)
96+
- [Router preloading guide](/solid-router/advanced-concepts/preloading)
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: Preloading
3+
---
4+
5+
Preloading smooths navigation by resolving route code and data before a user completes a transition.
6+
Solid Router listens for intent signals, such as hover and focus, and primes the matching route after a short delay to balance responsiveness and network cost.
7+
Understanding the timing and scope of this work lets you decide when to rely on the default behaviour and when to layer custom strategies.
8+
9+
| user action | route behaviour |
10+
| ----------- | --------------- |
11+
| hover | waits roughly 20 ms before preloading |
12+
| focus | preloads immediately |
13+
14+
## How Solid Router Detects Intent
15+
16+
Anchors registered with Solid Router emit hover and focus events that feed a small scheduler.
17+
The router debounces the hover signal for 20ms to ignore incidental pointer passes while still reacting quickly to purposeful movement.
18+
When the delay elapses, the router loads the route module and runs its preload routine so that navigation has the assets it needs when the user commits.
19+
20+
Route modules can export a [`preload`](/solid-router/reference/preload-functions/preload) function that receives params, search values, and router context.
21+
The function lets you seed caches, warm derived computations, or coordinate streaming behaviours without blocking the eventual render.
22+
23+
> [!NOTE]
24+
> SSR invokes route `preload` functions during the initial server render and resumes them on the client during hydration.
25+
> Keep these functions pure so the hydrated client does not need to undo server work when it takes over.
26+
27+
## Imperative Preloading Hooks
28+
29+
Not every interaction funnels through an anchor element.
30+
The [`usePreloadRoute`](/solid-router/reference/primitives/use-preload-route) primitive exposes the same scheduling behaviour for imperative flows like flyout previews, timers, or observer driven experiences.
31+
32+
This helper mirrors the router behaviour by resolving the module, optionally running the loader, and caching the result for the eventual navigation.
33+
Empirical tuning of delay values helps you avoid excessive prefetching in dense UIs while still keeping high intent interactions snappy.
34+
35+
## Coordinating Nested Lazy Components
36+
37+
Nested lazy components live outside the router hierarchy, so route preloading does not automatically warm them.
38+
The component API [`lazy()`](/reference/component-apis/lazy) exposes a `preload()` method that resolves a component without rendering it.
39+
Calling both the route preload and the nested component preload can keep large detail panels responsive when a user hovers or focuses on the entry point.
40+
41+
Balancing manual preloading requires observing real user flows so you avoid prefetching large bundles that the user never requests.
42+
Profiling tools help you spot whether preloading reduces long tasks or simply shifts work earlier without net gains.
43+
44+
To learn more about lazy loading components, see the [lazy documentation](/reference/component-apis/lazy#preloading-data-in-nested-lazy-components).

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,30 @@ description: >-
1515
prefetching route data before navigation in your SolidJS app.
1616
---
1717

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

20-
```js
20+
```ts
2121
const preload = usePreloadRoute();
2222

2323
preload(`/users/settings`, { preloadData: true });
2424
```
25+
26+
## Usage
27+
28+
Routes are preloaded by default within Solid Router contexts.
29+
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.
30+
31+
## Type Signature
32+
33+
### Parameters
34+
35+
| Parameter | Type | Required | Description |
36+
| --------- | -------- | -------- | ------------------------------------ |
37+
| `to` | `To` | Yes | The route path to preload |
38+
| `options` | `object` | No | Configuration options for preloading |
39+
40+
### Options
41+
42+
| Option | Type | Default | Description |
43+
| ------------- | --------- | ------- | ------------------------------------------------------------------- |
44+
| `preloadData` | `boolean` | `false` | Whether to preload the route's data in addition to the route itself |

0 commit comments

Comments
 (0)