-
Notifications
You must be signed in to change notification settings - Fork 334
content: Expand notes on preloading data #1306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
4a50173
50d0a0c
a2ab463
f364e32
a4c79c1
7937b09
0fe3593
e640128
a46fd80
f805d73
5788b7d
3d47fe4
203df8d
b1a0fb0
4b4f2e3
143ba25
738edde
4be0415
f5fdd71
c186d75
0ec7ff5
48469c4
d29d5fe
e969cad
7eec91d
f291d4d
8057066
5410b66
488749c
1406b87
b4dcd8f
8458d45
2a63b2d
45d613e
488f277
a0d27d3
9a097df
d9bc799
243f68f
4a60374
3b51452
d30f180
05aed52
ebd7e4f
5a56fe7
6d1fd5a
3ee97d1
51bd179
5a76a74
4ea8416
6af3c13
2a9eafc
d646c27
955c3e6
0bc8489
ef334aa
98ce2d4
30cf407
5094134
b763734
3c85b53
3cff9dc
d9cceb1
717d07e
df2eb80
2f870f4
960d3f1
ec2e366
599dd02
f2322e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,24 +2,75 @@ | |
| title: lazy | ||
| --- | ||
|
|
||
| ```ts | ||
| Used to lazy load components to allow for code splitting. | ||
| Components are not loaded until rendered. | ||
|
|
||
| ```tsx title="app.tsx" | ||
| import { lazy } from "solid-js" | ||
|
|
||
| const ComponentA = lazy(() => import("./ComponentA")); | ||
|
|
||
| function App(props: { title: string }) { | ||
| return ( | ||
| <ComponentA title={props.title} /> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| Lazy loaded components can be used the same as its statically imported counterpart, receiving props etc. | ||
| Lazy components trigger `<Suspense>` | ||
|
|
||
| ## Preloading data in Nested Lazy Components | ||
|
|
||
| Top-level lazy components will automatically be preloaded as well as their preload functions. | ||
|
||
| Though nested lazy components will not be preloaded automatically because they are not part of the route hyerarchy. | ||
atilafassina marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| To preload such components, you can use the `preload` method exposed on the lazy component | ||
atilafassina marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```tsx title="component-with-preload.tsx" | ||
| import { lazy } from "solid-js" | ||
| import type { Component } from "solid-js" | ||
|
|
||
| const Nested = lazy(() => import("./Nested")) | ||
|
|
||
| const ComponentWithPreload: Component = () => { | ||
| // preload Nested component when needed | ||
| async function handlePreload() { | ||
| await Nested.preload() | ||
atilafassina marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return ( | ||
| <div> | ||
| <button onClick={handlePreload}>Preload Nested Component</button> | ||
|
||
| <Nested /> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ## Type Signature | ||
|
|
||
| ```tsx | ||
| function lazy<T extends Component<any>>( | ||
| fn: () => Promise<{ default: T }> | ||
| ): T & { preload: () => Promise<T> } | ||
| ``` | ||
|
|
||
| Used to lazy load components to allow for code splitting. | ||
| Components are not loaded until rendered. | ||
| Lazy loaded components can be used the same as its statically imported counterpart, receiving props etc. | ||
| Lazy components trigger `<Suspense>` | ||
| ### Type Parameters | ||
|
|
||
| ```tsx | ||
| // wrap import | ||
| const ComponentA = lazy(() => import("./ComponentA")); | ||
| | Name | Constraint | Description | | ||
| | ---- | ---------- | ----------- | | ||
| | `T` | `Component<any>` | The component type that will be lazily loaded (including its props). | ||
|
|
||
| ### Parameters | ||
|
|
||
| | Parameter | Type | Required | Description | | ||
| | --------- | ---- | -------- | ----------- | | ||
| | `fn` | `() => Promise<{ default: T }>` | Yes | A function that returns a dynamic import resolving to the component as the `default` export. | | ||
|
|
||
| ### Returns | ||
|
|
||
| | Type | Description | | ||
| | ---- | ----------- | | ||
| | `T & { preload: () => Promise<T> }` | A renderable component compatible with `T` that also exposes a `preload()` method to eagerly load the module. | | ||
|
|
||
| // use in JSX | ||
| <ComponentA title={props.title} /> | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
| "title": "Advanced concepts", | ||
| "pages": ["lazy-loading.mdx"] | ||
| "pages": ["preloading.mdx", "lazy-loading.mdx"] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| --- | ||
| title: Preloading | ||
| --- | ||
|
|
||
| Anchors in Solid Router will preload routes by default on link hover/focus to improve perceived performance. | ||
|
|
||
| To enhance preloading, you can define the `preload` function on your route definition. | ||
| 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. | ||
|
|
||
| | user action | route behavior | | ||
| | ----------- | -------------------------------------- | | ||
| | hover | with a 300ms delay to avoid excessive preloading | | ||
atilafassina marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| | focus | immediately | | ||
|
||
|
|
||
| ## Imperative Preloading | ||
|
|
||
| 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. | ||
| This helper will load only the route's component by default, but it can receive a configuration object to also load the data. | ||
|
|
||
| ## Preloading and Lazy Loading | ||
|
|
||
| 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/reference/primitives/use-preload-route) helper in the parent component to load them when needed. | ||
|
||
|
|
||
| To learn more about lazy loading components, see the [`lazy`](/reference/component-apis/lazy#preloading-data-in-nested-lazy-components) documentation. | ||
Uh oh!
There was an error while loading. Please reload this page.