@@ -52,15 +52,16 @@ export function cacheKeyOp(key: string | string[] | void, fn: (cacheEntry: Cache
52
52
}
53
53
}
54
54
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 ;
59
63
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 > {
64
65
// prioritize GET for server functions
65
66
if ( ( fn as any ) . GET ) fn = ( fn as any ) . GET ;
66
67
const cachedFn = ( ( ...args : Parameters < T > ) => {
@@ -108,8 +109,8 @@ export function cache<T extends (...args: any) => any>(
108
109
let res = cached [ 1 ] ;
109
110
if ( intent !== "preload" ) {
110
111
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 ) )
113
114
: handleResponse ( false ) ( cached [ 1 ] ) ;
114
115
! isServer && intent === "navigate" && startTransition ( ( ) => cached [ 3 ] [ 1 ] ( cached [ 0 ] ) ) ; // update version
115
116
}
@@ -120,18 +121,6 @@ export function cache<T extends (...args: any) => any>(
120
121
? sharedConfig . load ! ( key ) // hydrating
121
122
: fn ( ...( args as any ) ) ;
122
123
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
-
135
124
if ( cached ) {
136
125
cached [ 0 ] = now ;
137
126
cached [ 1 ] = res ;
@@ -148,12 +137,27 @@ export function cache<T extends (...args: any) => any>(
148
137
cached [ 3 ] . count ++ ;
149
138
cached [ 3 ] [ 0 ] ( ) ; // track
150
139
}
140
+ if ( isServer ) {
141
+ const e = getRequestEvent ( ) ;
142
+ e && e . router ! . dataOnly && ( e . router ! . data ! [ key ] = res ) ;
143
+ return res ;
144
+ }
151
145
if ( intent !== "preload" ) {
152
146
res =
153
- "then" in ( res )
154
- ? ( res ) . then ( handleResponse ( false ) , handleResponse ( true ) )
147
+ "then" in res
148
+ ? res . then ( handleResponse ( false ) , handleResponse ( true ) )
155
149
: handleResponse ( false ) ( res ) ;
156
150
}
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
+ }
157
161
return res ;
158
162
159
163
function handleResponse ( error : boolean ) {
0 commit comments