Skip to content

Commit d81473a

Browse files
committedJun 17, 2024
usePreloadRoute method pre-release
1 parent 10b7d8c commit d81473a

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed
 

‎.changeset/shy-rats-kiss.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/router": patch
3+
---
4+
5+
usePreloadRoute method pre-release

‎README.md

+10
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,16 @@ const matches = useCurrentMatches();
873873
const breadcrumbs = createMemo(() => matches().map(m => m.route.info.breadcrumb))
874874
```
875875

876+
### usePreloadRoute
877+
878+
`usePreloadRoute` returns a function that can be used to preload a route manual. This is what happens automatically with link hovering and similar focus based behavior, but it is available here as an API.
879+
880+
```js
881+
const preload = usePreloadRoute();
882+
883+
preload(`/users/settings`, { preloadData: true });
884+
```
885+
876886
### useBeforeLeave
877887

878888
`useBeforeLeave` takes a function that will be called prior to leaving a route. The function will be called with:

‎src/data/events.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function setupNativeEvents(preload = true, explicitLinks = false, actionB
7575
url.pathname = transformUrl(url.pathname);
7676
}
7777
if (!preloadTimeout[url.pathname])
78-
router.preloadRoute(url, a.getAttribute("preload") !== "false");
78+
router.preloadRoute(url, { preloadData: a.getAttribute("preload") !== "false" });
7979
}
8080

8181
function handleAnchorIn(evt: Event) {
@@ -87,7 +87,7 @@ export function setupNativeEvents(preload = true, explicitLinks = false, actionB
8787
}
8888
if (preloadTimeout[url.pathname]) return;
8989
preloadTimeout[url.pathname] = setTimeout(() => {
90-
router.preloadRoute(url, a.getAttribute("preload") !== "false");
90+
router.preloadRoute(url, { preloadData: a.getAttribute("preload") !== "false" });
9191
delete preloadTimeout[url.pathname];
9292
}, 200) as any;
9393
}

‎src/routing.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export const useHref = (to: () => string | undefined) => {
7777
export const useNavigate = () => useRouter().navigatorFactory();
7878
export const useLocation = <S = unknown>() => useRouter().location as Location<S>;
7979
export const useIsRouting = () => useRouter().isRouting;
80+
export const usePreloadRoute = () => useRouter().preloadRoute
8081

8182
export const useMatch = <S extends string>(path: () => S, matchFilters?: MatchFilters<S>) => {
8283
const location = useLocation();
@@ -459,7 +460,7 @@ export function createRouterContext(
459460
}
460461
}
461462

462-
function preloadRoute(url: URL, preloadData: boolean) {
463+
function preloadRoute(url: URL, options: { preloadData?: boolean } = {}) {
463464
const matches = getRouteMatches(branches(), url.pathname);
464465
const prevIntent = intent;
465466
intent = "preload";
@@ -470,7 +471,7 @@ export function createRouterContext(
470471
(route.component as MaybePreloadableComponent).preload!();
471472
const { load } = route;
472473
inLoadFn = true;
473-
preloadData &&
474+
options.preloadData &&
474475
load &&
475476
runWithOwner(getContext!(), () =>
476477
load({

‎src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export interface RouterContext {
167167
renderPath(path: string): string;
168168
parsePath(str: string): string;
169169
beforeLeave: BeforeLeaveLifecycle;
170-
preloadRoute: (url: URL, preloadData: boolean) => void;
170+
preloadRoute: (url: URL, options: { preloadData?: boolean }) => void;
171171
singleFlight: boolean;
172172
submissions: Signal<Submission<any, any>[]>;
173173
}

0 commit comments

Comments
 (0)
Please sign in to comment.