Skip to content

Commit 884b8be

Browse files
committedApr 30, 2024
fix #374, fix #399 - suppress cache errors in load function
1 parent 5af3da8 commit 884b8be

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed
 

‎.changeset/funny-waves-wave.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/router": patch
3+
---
4+
5+
fix #374, fix #399 - suppress cache errors in load function

‎src/data/cache.ts

+6-5
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 } from "../routing.js";
11+
import { useNavigate, getIntent, getInLoadFn } from "../routing.js";
1212
import { CacheEntry } from "../types.js";
1313

1414
const LocationHeader = "Location";
@@ -69,6 +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();
7273
const owner = getOwner();
7374
const navigate = owner ? useNavigate() : undefined;
7475
const now = Date.now();
@@ -110,13 +111,13 @@ export function cache<T extends (...args: any) => any>(fn: T, name: string): Cac
110111
cached[0] = now;
111112
}
112113
let res = cached[1];
113-
if (intent !== "preload") {
114+
if (!inLoadFn) {
114115
res =
115116
"then" in cached[1]
116117
? cached[1].then(handleResponse(false), handleResponse(true))
117118
: handleResponse(false)(cached[1]);
118119
!isServer && intent === "navigate" && startTransition(() => cached[3][1](cached[0])); // update version
119-
}
120+
} else "then" in res && res.catch(() => {});
120121
return res;
121122
}
122123
let res =
@@ -144,12 +145,12 @@ export function cache<T extends (...args: any) => any>(fn: T, name: string): Cac
144145
const e = getRequestEvent();
145146
if (e && e.router!.dataOnly) return (e.router!.data![key] = res);
146147
}
147-
if (intent !== "preload") {
148+
if (!inLoadFn) {
148149
res =
149150
"then" in res
150151
? res.then(handleResponse(false), handleResponse(true))
151152
: handleResponse(false)(res);
152-
}
153+
} else "then" in res && res.catch(() => {});
153154
// serialize on server
154155
if (
155156
isServer &&

‎src/routers/components.tsx

+11-5
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ import {
1616
createBranches,
1717
createRouteContext,
1818
createRouterContext,
19+
getIntent,
1920
getRouteMatches,
2021
RouteContextObj,
21-
RouterContextObj
22+
RouterContextObj,
23+
setInLoadFn
2224
} from "../routing.js";
2325
import type {
2426
MatchFilters,
25-
Params,
2627
RouteContext,
2728
RouteLoadFunc,
2829
RouteDefinition,
2930
RouterIntegration,
3031
RouterContext,
3132
Branch,
32-
RouteSectionProps,
33-
Location
33+
RouteSectionProps
3434
} from "../types.js";
3535

3636
export type BaseRouterProps = {
@@ -76,7 +76,13 @@ function Root(props: {
7676
const location = props.routerState.location;
7777
const params = props.routerState.params;
7878
const data = createMemo(
79-
() => props.load && untrack(() => props.load!({ params, location, intent: "preload" }))
79+
() =>
80+
props.load &&
81+
untrack(() => {
82+
setInLoadFn(true);
83+
props.load!({ params, location, intent: getIntent() || "initial" });
84+
setInLoadFn(false);
85+
})
8086
);
8187
return (
8288
<Show when={props.root} keyed fallback={props.children}>

‎src/routing.ts

+11
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ let intent: Intent | undefined;
268268
export function getIntent() {
269269
return intent;
270270
}
271+
let inLoadFn = false;
272+
export function getInLoadFn() {
273+
return inLoadFn;
274+
}
275+
export function setInLoadFn(value: boolean) {
276+
inLoadFn = value;
277+
}
271278

272279
export function createRouterContext(
273280
integration: RouterIntegration,
@@ -457,6 +464,7 @@ export function createRouterContext(
457464
(route.component as MaybePreloadableComponent).preload &&
458465
(route.component as MaybePreloadableComponent).preload!();
459466
const { load } = route;
467+
inLoadFn = true;
460468
preloadData &&
461469
load &&
462470
runWithOwner(getContext!(), () =>
@@ -473,6 +481,7 @@ export function createRouterContext(
473481
intent: "preload"
474482
})
475483
);
484+
inLoadFn = false;
476485
}
477486
intent = prevIntent;
478487
}
@@ -498,7 +507,9 @@ export function createRouteContext(
498507
component &&
499508
(component as MaybePreloadableComponent).preload &&
500509
(component as MaybePreloadableComponent).preload!();
510+
inLoadFn = true;
501511
const data = load ? load({ params, location, intent: intent || "initial" }) : undefined;
512+
inLoadFn = false;
502513

503514
const route: RouteContext = {
504515
parent,

0 commit comments

Comments
 (0)
Please sign in to comment.