11---
22title : preload
33use_cases : >-
4- route preloading, hover prefetch, parallel loading, performance optimization,
5- lazy routes, code splitting
4+ route preload, route data, route definitions
65tags :
76 - preload
8- - performance
97 - routing
10- - prefetch
11- - lazy
12- - optimization
8+ - data
139version : " 1.0"
1410description : >-
15- Preload route data and code in parallel to eliminate waterfalls. Enable hover
16- prefetching for instant page transitions.
11+ preload is a route definition property for route data setup.
1712---
1813
19- ` preload ` is a route-definition property that runs during route preloading and rendering .
14+ ` preload ` is a [ ` Route ` ] ( /solid-router/reference/components/route ) property for preparing route data before the route component renders or while a route is being preloaded .
2015
2116## Import
2217
@@ -27,81 +22,87 @@ import { Route } from "@solidjs/router";
2722## Type
2823
2924``` tsx
30- type RoutePreloadFunc < T = unknown > = ( args : RoutePreloadFuncArgs ) => T ;
25+ type Intent = " initial " | " native " | " navigate " | " preload " ;
3126
3227interface RoutePreloadFuncArgs {
3328 params: Params ;
3429 location: Location ;
35- intent: " initial " | " native " | " navigate " | " preload " ;
30+ intent: Intent ;
3631}
32+
33+ type RoutePreloadFunc <T = unknown > = (args : RoutePreloadFuncArgs ) => T ;
3734```
3835
3936## Parameters
4037
4138### ` params `
4239
4340- ** Type:** ` Params `
41+ - ** Required:** Yes
4442
45- An object containing the parameters for the matched route.
46- It corresponds to the value returned by the [ ` useParams ` primitive ] ( /solid-router/reference/primitives/use-params ) .
43+ Route params for the matched route.
44+ The value has the same shape as [ ` useParams ` ] ( /solid-router/reference/primitives/use-params ) .
4745
4846### ` location `
4947
5048- ** Type:** ` Location `
49+ - ** Required:** Yes
5150
52- The router's location object for the destination URL.
53- It corresponds to the value returned by the [ ` useLocation ` primitive] ( /solid-router/reference/primitives/use-location ) .
51+ [ ` Location ` ] ( /solid-router/reference/primitives/use-location ) for the route being loaded or preloaded.
5452
5553### ` intent `
5654
5755- ** Type:** ` "initial" | "native" | "navigate" | "preload" `
56+ - ** Required:** Yes
5857
59- A string indicating the context in which the function is called.
60-
61- | Value | Meaning |
62- | ---------- | ------------------------------------------------------------------ |
63- | ` preload ` | Preloading route code or data. |
64- | ` navigate ` | Client navigation to the route. |
65- | ` native ` | Native history navigation, such as back or forward. |
66- | ` initial ` | The first render for the current request or page load. |
58+ Reason the router called the preload function, such as initial render, router navigation, native history navigation, or route preloading.
6759
6860## Return value
6961
70- The value returned by ` preload ` is passed to the route's component as the ` data ` prop.
62+ - ** Type: ** ` T `
7163
72- | Intent | Return value behavior |
73- | ----------------------------------- | --------------------------------------------- |
74- | ` preload ` | Ignored. |
75- | ` initial ` , ` navigate ` , or ` native ` | Captured and passed to the component as ` data ` . |
64+ Returns the route data value.
65+ During route context creation, Solid Router passes this value to the matched route component as ` props.data ` .
7666
7767## Behavior
7868
79- - During route preloading, the router calls ` preload ` for each matched route.
80- - ` load ` is a deprecated alias for ` preload ` on route definitions.
81- - Route component preloads run before the route's ` preload ` function when the component exposes a ` .preload() ` method.
69+ - During route context creation, Solid Router calls ` preload ` with the matched params, current location, and current router intent or ` "initial" ` .
70+ - Manual route preloading calls ` preload ` with ` intent: "preload" ` only when ` preloadData ` is truthy.
71+ - If a route definition has no ` preload ` , Solid Router uses the deprecated ` load ` property when one is present.
72+ - The route component's static ` preload ` method runs before the route-level ` preload ` function.
8273
8374## Examples
8475
76+ ### Basic usage
77+
8578``` tsx
86- import { Route , query , createAsync } from " @solidjs/router" ;
79+ import { Route , query } from " @solidjs/router" ;
8780
88- const getProductQuery = query (async (id : string ) => {
89- // ... Fetches a product from the server.
81+ const getProduct = query (async (id : string ) => {
82+ const response = await fetch (` /api/products/${id } ` );
83+ return response .json ();
9084}, " product" );
9185
92- function ProductPage(props ) {
93- const product = createAsync (() => getProductQuery (props .params .id ));
94-
95- return <div >{ product ()?.title } </div >;
86+ function preloadProduct({ params }) {
87+ void getProduct (params .id );
9688}
9789
98- function preloadData({ params } ) {
99- getProductQuery ( params .id ) ;
90+ function ProductPage( props ) {
91+ return < h1 >Product { props . params .id } </ h1 > ;
10092}
10193
102- function ProductRoutes() {
94+ export default function ProductRoutes() {
10395 return (
104- <Route path = " /products/:id" component = { ProductPage } preload = { preloadData } />
96+ <Route
97+ path = " /products/:id"
98+ component = { ProductPage }
99+ preload = { preloadProduct }
100+ />
105101 );
106102}
107103```
104+
105+ ## Related
106+
107+ - [ ` Route ` ] ( /solid-router/reference/components/route )
108+ - [ ` usePreloadRoute ` ] ( /solid-router/reference/primitives/use-preload-route )
0 commit comments