Skip to content

Commit 5d9263b

Browse files
committed
rename load to preload
1 parent bd9f19a commit 5d9263b

7 files changed

Lines changed: 81 additions & 63 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/router": minor
3+
---
4+
5+
rename load to preload

README.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A router lets you change your view based on the URL in the browser. This allows
1010

1111
Solid Router is a universal router for SolidJS - it works whether you're rendering on the client or on the server. It was inspired by and combines paradigms of React Router and the Ember Router. Routes can be defined directly in your app's template using JSX, but you can also pass your route configuration directly as an object. It also supports nested routing, so navigation can change a part of a component, rather than completely replacing it.
1212

13-
It supports all of Solid's SSR methods and has Solid's transitions baked in, so use it freely with suspense, resources, and lazy components. Solid Router also allows you to define a load function that loads parallel to the routes ([render-as-you-fetch](https://epicreact.dev/render-as-you-fetch/)).
13+
It supports all of Solid's SSR methods and has Solid's transitions baked in, so use it freely with suspense, resources, and lazy components. Solid Router also allows you to define a preload function that loads parallel to the routes ([render-as-you-fetch](https://epicreact.dev/render-as-you-fetch/)).
1414

1515
- [Getting Started](#getting-started)
1616
- [Set Up the Router](#set-up-the-router)
@@ -380,25 +380,25 @@ You can nest indefinitely - just remember that only leaf nodes will become their
380380
</Route>
381381
```
382382

383-
## Load Functions
383+
## Preload Functions
384384

385-
Even with smart caches it is possible that we have waterfalls both with view logic and with lazy loaded code. With load functions, we can instead start fetching the data parallel to loading the route, so we can use the data as soon as possible. The load function is called when the Route is loaded or eagerly when links are hovered.
385+
Even with smart caches it is possible that we have waterfalls both with view logic and with lazy loaded code. With preload functions, we can instead start fetching the data parallel to loading the route, so we can use the data as soon as possible. The preload function is called when the Route is loaded or eagerly when links are hovered.
386386

387-
As its only argument, the load function is passed an object that you can use to access route information:
387+
As its only argument, the preload function is passed an object that you can use to access route information:
388388

389389
```js
390390
import { lazy } from "solid-js";
391391
import { Route } from "@solidjs/router";
392392

393393
const User = lazy(() => import("./pages/users/[id].js"));
394394

395-
// load function
396-
function loadUser({params, location}) {
397-
// do loading
395+
// preload function
396+
function preloadUser({params, location}) {
397+
// do preloading
398398
}
399399

400400
// Pass it in the route definition
401-
<Route path="/users/:id" component={User} load={loadUser} />;
401+
<Route path="/users/:id" component={User} preload={preloadUser} />;
402402
```
403403

404404
| key | type | description |
@@ -408,24 +408,24 @@ function loadUser({params, location}) {
408408
| intent | `"initial", "navigate", "native", "preload"` | Indicates why this function is being called. <ul><li>"initial" - the route is being initially shown (ie page load)</li><li>"native" - navigate originated from the browser (eg back/forward)</li><li>"navigate" - navigate originated from the router (eg call to navigate or anchor clicked)</li><li>"preload" - not navigating, just preloading (eg link hover)</li></ul> |
409409

410410

411-
A common pattern is to export the load function and data wrappers that corresponds to a route in a dedicated `route.data.js` file. This way, the data function can be imported without loading anything else.
411+
A common pattern is to export the preload function and data wrappers that corresponds to a route in a dedicated `route.data.js` file. This way, the data function can be imported without loading anything else.
412412

413413
```js
414414
import { lazy } from "solid-js";
415415
import { Route } from "@solidjs/router";
416-
import loadUser from "./pages/users/[id].data.js";
416+
import preloadUser from "./pages/users/[id].data.js";
417417
const User = lazy(() => import("/pages/users/[id].js"));
418418

419419
// In the Route definition
420-
<Route path="/users/:id" component={User} load={loadUser} />;
420+
<Route path="/users/:id" component={User} preload={preloadUser} />;
421421
```
422422

423-
The return value of the `load` function is passed to the page component when called at anytime other than `"preload"`, so you can initialize things in there, or alternatively use our new Data APIs:
423+
The return value of the `preload` function is passed to the page component when called at anytime other than `"preload"` intent, so you can initialize things in there, or alternatively use our new Data APIs:
424424

425425

426426
## Data APIs
427427

428-
Keep in mind these are completely optional. To use but showcase the power of our load mechanism.
428+
Keep in mind these are completely optional. To use but showcase the power of our preload mechanism.
429429

430430
### `cache`
431431

@@ -441,11 +441,11 @@ It is expected that the arguments to the cache function are serializable.
441441
This cache accomplishes the following:
442442

443443
1. It does just deduping on the server for the lifetime of the request.
444-
2. It does preload cache in the browser which lasts 10 seconds. When a route is preloaded on hover or when load is called when entering a route it will make sure to dedupe calls.
444+
2. It does preload cache in the browser which lasts 5 seconds. When a route is preloaded on hover or when preload is called when entering a route it will make sure to dedupe calls.
445445
3. We have a reactive refetch mechanism based on key. So we can tell routes that aren't new to retrigger on action revalidation.
446446
4. It will serve as a back/forward cache for browser navigation up to 5 mins. Any user based navigation or link click bypasses it. Revalidation or new fetch updates the cache.
447447

448-
Using it with load function might look like:
448+
Using it with preload function might look like:
449449

450450
```js
451451
import { lazy } from "solid-js";
@@ -454,13 +454,13 @@ import { getUser } from ... // the cache function
454454

455455
const User = lazy(() => import("./pages/users/[id].js"));
456456

457-
// load function
458-
function loadUser({params, location}) {
457+
// preload function
458+
function preloadUser({params, location}) {
459459
void getUser(params.id)
460460
}
461461

462462
// Pass it in the route definition
463-
<Route path="/users/:id" component={User} load={loadUser} />;
463+
<Route path="/users/:id" component={User} preload={preloadUser} />;
464464
```
465465

466466
Inside your page component you:
@@ -770,7 +770,7 @@ The Component for defining Routes:
770770
| component | `Component` | Component that will be rendered for the matched segment |
771771
| matchFilters | `MatchFilters` | Additional constraints for matching against the route |
772772
| children | `JSX.Element` | Nested `<Route>` definitions |
773-
| load | `RouteLoadFunc` | Function called during preload or when the route is navigated to. |
773+
| preload | `RoutePreloadFunc` | Function called during preload or when the route is navigated to. |
774774

775775
## Router Primitives
776776

@@ -929,7 +929,7 @@ Related without Outlet component it has to be passed in manually. At which point
929929

930930
### `data` functions & `useRouteData`
931931

932-
These have been replaced by a load mechanism. This allows link hover preloads (as the load function can be run as much as wanted without worry about reactivity). It support deduping/cache APIs which give more control over how things are cached. It also addresses TS issues with getting the right types in the Component without `typeof` checks.
932+
These have been replaced by a preload mechanism. This allows link hover preloads (as the preload function can be run as much as wanted without worry about reactivity). It support deduping/cache APIs which give more control over how things are cached. It also addresses TS issues with getting the right types in the Component without `typeof` checks.
933933

934934
That being said you can reproduce the old pattern largely by turning off preloads at the router level and then injecting your own Context:
935935

@@ -939,15 +939,15 @@ import { Route } from "@solidjs/router";
939939

940940
const User = lazy(() => import("./pages/users/[id].js"));
941941

942-
// load function
943-
function loadUser({params, location}) {
942+
// preload function
943+
function preloadUser({params, location}) {
944944
const [user] = createResource(() => params.id, fetchUser);
945945
return user;
946946
}
947947

948948
// Pass it in the route definition
949949
<Router preload={false}>
950-
<Route path="/users/:id" component={User} load={loadUser} />
950+
<Route path="/users/:id" component={User} preload={preloadUser} />
951951
</Router>
952952
```
953953

src/data/cache.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
startTransition
99
} from "solid-js";
1010
import { getRequestEvent, isServer } from "solid-js/web";
11-
import { useNavigate, getIntent, getInLoadFn } from "../routing.js";
11+
import { useNavigate, getIntent, getInPreloadFn } from "../routing.js";
1212
import { CacheEntry } from "../types.js";
1313

1414
const LocationHeader = "Location";
@@ -69,7 +69,7 @@ export function cache<T extends (...args: any) => any>(fn: T, name: string): Cac
6969
const cachedFn = ((...args: Parameters<T>) => {
7070
const cache = getCache();
7171
const intent = getIntent();
72-
const inLoadFn = getInLoadFn();
72+
const inPreloadFn = getInPreloadFn();
7373
const owner = getOwner();
7474
const navigate = owner ? useNavigate() : undefined;
7575
const now = Date.now();
@@ -118,7 +118,7 @@ export function cache<T extends (...args: any) => any>(fn: T, name: string): Cac
118118
: handleResponse(false)(cached[1]);
119119
!isServer && intent === "navigate" && startTransition(() => cached[3][1](cached[0])); // update version
120120
}
121-
inLoadFn && "then" in res && res.catch(() => {});
121+
inPreloadFn && "then" in res && res.catch(() => {});
122122
return res;
123123
}
124124
let res =
@@ -152,7 +152,7 @@ export function cache<T extends (...args: any) => any>(fn: T, name: string): Cac
152152
? res.then(handleResponse(false), handleResponse(true))
153153
: handleResponse(false)(res);
154154
}
155-
inLoadFn && "then" in res && res.catch(() => {});
155+
inPreloadFn && "then" in res && res.catch(() => {});
156156
// serialize on server
157157
if (
158158
isServer &&

src/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ export type {
2727
Params,
2828
PathMatch,
2929
RouteSectionProps,
30-
RouteLoadFunc,
31-
RouteLoadFuncArgs,
30+
RoutePreloadFunc,
31+
RoutePreloadFuncArgs,
3232
RouteDefinition,
3333
RouteDescription,
3434
RouteMatch,
3535
RouterIntegration,
3636
RouterUtils,
3737
SetParams,
38-
BeforeLeaveEventArgs
38+
BeforeLeaveEventArgs,
39+
RouteLoadFunc,
40+
RouteLoadFuncArgs,
3941
} from "./types.js";

src/routers/components.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ import {
2020
getRouteMatches,
2121
RouteContextObj,
2222
RouterContextObj,
23-
setInLoadFn
23+
setInPreloadFn
2424
} from "../routing.js";
2525
import type {
2626
MatchFilters,
2727
RouteContext,
28-
RouteLoadFunc,
2928
RouteDefinition,
3029
RouterIntegration,
3130
RouterContext,
3231
Branch,
33-
RouteSectionProps
32+
RouteSectionProps,
33+
RoutePreloadFunc
3434
} from "../types.js";
3535

3636
export type BaseRouterProps = {
@@ -39,10 +39,12 @@ export type BaseRouterProps = {
3939
* A component that wraps the content of every route.
4040
*/
4141
root?: Component<RouteSectionProps>;
42-
rootLoad?: RouteLoadFunc;
42+
rootPreload?: RoutePreloadFunc;
4343
singleFlight?: boolean;
4444
children?: JSX.Element | RouteDefinition | RouteDefinition[];
4545
transformUrl?: (url: string) => string;
46+
/** @deprecated use rootPreload */
47+
rootLoad?: RoutePreloadFunc;
4648
};
4749

4850
export const createRouterComponent = (router: RouterIntegration) => (props: BaseRouterProps) => {
@@ -61,7 +63,7 @@ export const createRouterComponent = (router: RouterIntegration) => (props: Base
6163
router.create && router.create(routerState);
6264
return (
6365
<RouterContextObj.Provider value={routerState}>
64-
<Root routerState={routerState} root={props.root} load={props.rootLoad}>
66+
<Root routerState={routerState} root={props.root} preload={props.rootPreload || props.rootLoad}>
6567
{(context = getOwner()!) && null}
6668
<Routes routerState={routerState} branches={branches()} />
6769
</Root>
@@ -72,18 +74,18 @@ export const createRouterComponent = (router: RouterIntegration) => (props: Base
7274
function Root(props: {
7375
routerState: RouterContext;
7476
root?: Component<RouteSectionProps>;
75-
load?: RouteLoadFunc;
77+
preload?: RoutePreloadFunc;
7678
children: JSX.Element;
7779
}) {
7880
const location = props.routerState.location;
7981
const params = props.routerState.params;
8082
const data = createMemo(
8183
() =>
82-
props.load &&
84+
props.preload &&
8385
untrack(() => {
84-
setInLoadFn(true);
85-
props.load!({ params, location, intent: getIntent() || "initial" });
86-
setInLoadFn(false);
86+
setInPreloadFn(true);
87+
props.preload!({ params, location, intent: getIntent() || "initial" });
88+
setInPreloadFn(false);
8789
})
8890
);
8991
return (
@@ -169,10 +171,12 @@ const createOutlet = (child: () => RouteContext | undefined) => {
169171
export type RouteProps<S extends string, T = unknown> = {
170172
path?: S | S[];
171173
children?: JSX.Element;
172-
load?: RouteLoadFunc<T>;
174+
preload?: RoutePreloadFunc<T>;
173175
matchFilters?: MatchFilters<S>;
174176
component?: Component<RouteSectionProps<T>>;
175177
info?: Record<string, any>;
178+
/** @deprecated use preload */
179+
load?: RoutePreloadFunc<T>;
176180
};
177181

178182
export const Route = <S extends string, T = unknown>(props: RouteProps<S, T>) => {
@@ -196,8 +200,8 @@ function dataOnly(event: RequestEvent, routerState: RouterContext, branches: Bra
196200
if (!prevMatches[match] || matches[match].route !== prevMatches[match].route)
197201
event.router!.dataOnly = true;
198202
const { route, params } = matches[match];
199-
route.load &&
200-
route.load({
203+
route.preload &&
204+
route.preload({
201205
params,
202206
location: routerState.location,
203207
intent: "preload"

src/routing.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ export const useBeforeLeave = (listener: (e: BeforeLeaveEventArgs) => void) => {
125125
};
126126

127127
export function createRoutes(routeDef: RouteDefinition, base: string = ""): RouteDescription[] {
128-
const { component, load, children, info } = routeDef;
128+
const { component, preload, load, children, info } = routeDef;
129129
const isLeaf = !children || (Array.isArray(children) && !children.length);
130130

131131
const shared = {
132132
key: routeDef,
133133
component,
134-
load,
134+
preload: preload || load,
135135
info
136136
};
137137

@@ -271,12 +271,12 @@ let intent: Intent | undefined;
271271
export function getIntent() {
272272
return intent;
273273
}
274-
let inLoadFn = false;
275-
export function getInLoadFn() {
276-
return inLoadFn;
274+
let inPreloadFn = false;
275+
export function getInPreloadFn() {
276+
return inPreloadFn;
277277
}
278-
export function setInLoadFn(value: boolean) {
279-
inLoadFn = value;
278+
export function setInPreloadFn(value: boolean) {
279+
inPreloadFn = value;
280280
}
281281

282282
export function createRouterContext(
@@ -469,12 +469,12 @@ export function createRouterContext(
469469
route.component &&
470470
(route.component as MaybePreloadableComponent).preload &&
471471
(route.component as MaybePreloadableComponent).preload!();
472-
const { load } = route;
473-
inLoadFn = true;
472+
const { preload } = route;
473+
inPreloadFn = true;
474474
options.preloadData &&
475-
load &&
475+
preload &&
476476
runWithOwner(getContext!(), () =>
477-
load({
477+
preload({
478478
params,
479479
location: {
480480
pathname: url.pathname,
@@ -487,7 +487,7 @@ export function createRouterContext(
487487
intent: "preload"
488488
})
489489
);
490-
inLoadFn = false;
490+
inPreloadFn = false;
491491
}
492492
intent = prevIntent;
493493
}
@@ -507,15 +507,15 @@ export function createRouteContext(
507507
match: () => RouteMatch
508508
): RouteContext {
509509
const { base, location, params } = router;
510-
const { pattern, component, load } = match().route;
510+
const { pattern, component, preload } = match().route;
511511
const path = createMemo(() => match().path);
512512

513513
component &&
514514
(component as MaybePreloadableComponent).preload &&
515515
(component as MaybePreloadableComponent).preload!();
516-
inLoadFn = true;
517-
const data = load ? load({ params, location, intent: intent || "initial" }) : undefined;
518-
inLoadFn = false;
516+
inPreloadFn = true;
517+
const data = preload ? preload({ params, location, intent: intent || "initial" }) : undefined;
518+
inPreloadFn = false;
519519

520520
const route: RouteContext = {
521521
parent,

0 commit comments

Comments
 (0)