Skip to content

Commit 6d0be9e

Browse files
committedFeb 16, 2024
fix cache serialization to match returned value
1 parent 76b2689 commit 6d0be9e

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed
 

‎.changeset/eighty-jobs-nail.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/router": patch
3+
---
4+
5+
fix cache serialization to match returned value

‎src/data/cache.ts

+28-24
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,16 @@ export function cacheKeyOp(key: string | string[] | void, fn: (cacheEntry: Cache
5252
}
5353
}
5454

55-
export type CachedFunction<T extends (...args: any) => any> = T extends (...args: infer A) => infer R ? ([] extends A ? (...args: never[]) => R : T) & {
56-
keyFor: (...args: A) => string;
57-
key: string;
58-
} : never;
55+
export type CachedFunction<T extends (...args: any) => any> = T extends (
56+
...args: infer A
57+
) => infer R
58+
? ([] extends A ? (...args: never[]) => R : T) & {
59+
keyFor: (...args: A) => string;
60+
key: string;
61+
}
62+
: never;
5963

60-
export function cache<T extends (...args: any) => any>(
61-
fn: T,
62-
name: string,
63-
): CachedFunction<T> {
64+
export function cache<T extends (...args: any) => any>(fn: T, name: string): CachedFunction<T> {
6465
// prioritize GET for server functions
6566
if ((fn as any).GET) fn = (fn as any).GET;
6667
const cachedFn = ((...args: Parameters<T>) => {
@@ -108,8 +109,8 @@ export function cache<T extends (...args: any) => any>(
108109
let res = cached[1];
109110
if (intent !== "preload") {
110111
res =
111-
"then" in (cached[1])
112-
? (cached[1]).then(handleResponse(false), handleResponse(true))
112+
"then" in cached[1]
113+
? cached[1].then(handleResponse(false), handleResponse(true))
113114
: handleResponse(false)(cached[1]);
114115
!isServer && intent === "navigate" && startTransition(() => cached[3][1](cached[0])); // update version
115116
}
@@ -120,18 +121,6 @@ export function cache<T extends (...args: any) => any>(
120121
? sharedConfig.load!(key) // hydrating
121122
: fn(...(args as any));
122123

123-
// serialize on server
124-
if (
125-
isServer &&
126-
sharedConfig.context &&
127-
(sharedConfig.context as any).async &&
128-
!(sharedConfig.context as any).noHydrate
129-
) {
130-
const e = getRequestEvent();
131-
e && e.router!.dataOnly && (e.router!.data![key] = res);
132-
(!e || !e.serverOnly) && (sharedConfig.context as any).serialize(key, res);
133-
}
134-
135124
if (cached) {
136125
cached[0] = now;
137126
cached[1] = res;
@@ -148,12 +137,27 @@ export function cache<T extends (...args: any) => any>(
148137
cached[3].count++;
149138
cached[3][0](); // track
150139
}
140+
if (isServer) {
141+
const e = getRequestEvent();
142+
e && e.router!.dataOnly && (e.router!.data![key] = res);
143+
return res;
144+
}
151145
if (intent !== "preload") {
152146
res =
153-
"then" in (res)
154-
? (res).then(handleResponse(false), handleResponse(true))
147+
"then" in res
148+
? res.then(handleResponse(false), handleResponse(true))
155149
: handleResponse(false)(res);
156150
}
151+
// serialize on server
152+
if (
153+
isServer &&
154+
sharedConfig.context &&
155+
(sharedConfig.context as any).async &&
156+
!(sharedConfig.context as any).noHydrate
157+
) {
158+
const e = getRequestEvent();
159+
(!e || !e.serverOnly) && (sharedConfig.context as any).serialize(key, res);
160+
}
157161
return res;
158162

159163
function handleResponse(error: boolean) {

0 commit comments

Comments
 (0)
Please sign in to comment.