Skip to content

Commit 5094134

Browse files
committed
update pages
1 parent 30cf407 commit 5094134

File tree

2 files changed

+79
-56
lines changed

2 files changed

+79
-56
lines changed

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

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,66 @@ description: >-
1616
performance. Components load on-demand and integrate with Suspense.
1717
---
1818

19-
Used to lazy load components to allow for code splitting.
20-
Components are not loaded until rendered or manually preloaded.
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
25+
import { lazy } from "solid-js"
26+
```
27+
28+
## Type
29+
30+
```tsx
31+
function lazy<T extends Component<any>>(
32+
fn: () => Promise<{ default: T }>
33+
): T & { preload: () => Promise<T> }
34+
```
35+
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
2157

2258
```tsx title="app.tsx"
2359
import { lazy } from "solid-js"
2460

25-
const ComponentA = lazy(() => import("./ComponentA"));
61+
const ComponentA = lazy(() => import("./ComponentA"))
2662

2763
function App(props: { title: string }) {
28-
return (
29-
<ComponentA title={props.title} />
30-
)
64+
return <ComponentA title={props.title} />
3165
}
3266
```
3367

34-
Lazy loaded components can be used the same as its statically imported counterpart, receiving props etc.
35-
Lazy components trigger `<Suspense>`
36-
37-
## Preloading data in Nested Lazy Components
38-
39-
Top-level lazy components will automatically be preloaded as well as their preload functions.
40-
However, nested lazy components will not be preloaded automatically because they are not part of the route hierarchy.
41-
To preload such components, you can use the `preload` method exposed on the lazy component.
68+
### Preloading nested lazy components
4269

43-
```tsx title="component-with-preload.tsx"
70+
```tsx
4471
import { lazy } from "solid-js"
4572
import type { Component } from "solid-js"
4673

4774
const Nested = lazy(() => import("./Nested"))
4875

4976
const ComponentWithPreload: Component = () => {
50-
// preload Nested component when needed
5177
async function handlePreload() {
52-
await Nested.preload()
78+
await Nested.preload()
5379
}
5480

5581
return (
@@ -59,32 +85,9 @@ const ComponentWithPreload: Component = () => {
5985
</div>
6086
)
6187
}
62-
63-
```
64-
65-
## Type Signature
66-
67-
```tsx
68-
function lazy<T extends Component<any>>(
69-
fn: () => Promise<{ default: T }>
70-
): T & { preload: () => Promise<T> }
7188
```
7289

73-
### Type Parameters
74-
75-
| Name | Constraint | Description |
76-
| ---- | ---------- | ----------- |
77-
| `T` | `Component<any>` | The component type that will be lazily loaded (including its props).
78-
79-
### Parameters
80-
81-
| Parameter | Type | Required | Description |
82-
| --------- | ---- | -------- | ----------- |
83-
| `fn` | `() => Promise<{ default: T }>` | Yes | A function that returns a dynamic import resolving to the component as the `default` export. |
84-
85-
### Returns
86-
87-
| Type | Description |
88-
| ---- | ----------- |
89-
| `T & { preload: () => Promise<T> }` | A renderable component compatible with `T` that also exposes a `preload()` method to eagerly load the module. |
90+
## See also
9091

92+
- [`Suspense`](https://docs.solidjs.com/reference/component-apis/suspense)
93+
- [Router preloading guide](/solid-router/advanced-concepts/preloading)

src/routes/solid-router/advanced-concepts/preloading.mdx

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,43 @@
22
title: Preloading
33
---
44

5-
Anchors in Solid Router will preload routes by default on link hover/focus to improve perceived performance.
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.
68

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 start fetching 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+
| user action | route behaviour |
10+
| ----------- | --------------- |
11+
| hover | waits roughly 20 ms before preloading |
12+
| focus | preloads immediately |
913

10-
| user action | route behavior |
11-
| ----------- | -------------------------------------- |
12-
| hover | with a 300ms delay to avoid excessive preloading |
13-
| focus | immediately |
14+
## How Solid Router Detects Intent
1415

15-
## Imperative Preloading
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.
1619

17-
You can also use the [`usePreloadRoute`](/solid-router/reference/primitives/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.
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.
1922

20-
## Preloading and Lazy Loading
23+
> [!NOTE]
24+
> [SolidStart](/solid-start) also 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.
2126
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 `preload()` function returned from calling the [`lazy()`](https://docs.solidjs.com/reference/component-apis/lazy) component API.
27+
## Imperative Preloading Hooks
2328

24-
To learn more about lazy loading components, see the [`lazy`](/reference/component-apis/lazy#preloading-data-in-nested-lazy-components) documentation.
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).

0 commit comments

Comments
 (0)