@@ -14,12 +14,16 @@ import {
14
14
15
15
type Callback = ( ...args : any [ ] ) => Promise < any >
16
16
17
+ let noStoreFetchIdx = 0
18
+
17
19
async function cacheNewResult < T > (
18
20
result : T ,
19
21
incrementalCache : IncrementalCache ,
20
22
cacheKey : string ,
21
23
tags : string [ ] ,
22
- revalidate : number | false | undefined
24
+ revalidate : number | false | undefined ,
25
+ fetchIdx : number ,
26
+ fetchUrl : string
23
27
) : Promise < unknown > {
24
28
await incrementalCache . set (
25
29
cacheKey ,
@@ -38,6 +42,8 @@ async function cacheNewResult<T>(
38
42
revalidate,
39
43
fetchCache : true ,
40
44
tags,
45
+ fetchIdx,
46
+ fetchUrl,
41
47
}
42
48
)
43
49
return
@@ -104,8 +110,12 @@ export function unstable_cache<T extends Callback>(
104
110
// the keyspace smaller than the execution space
105
111
const invocationKey = `${ fixedKey } -${ JSON . stringify ( args ) } `
106
112
const cacheKey = await incrementalCache . fetchCacheKey ( invocationKey )
113
+ const fetchUrl = `unstable_cache ${ cb . name ? ` ${ cb . name } ` : cacheKey } `
114
+ const fetchIdx = ( store ? store . nextFetchId : noStoreFetchIdx ) ?? 1
107
115
108
116
if ( store ) {
117
+ store . nextFetchId = fetchIdx + 1
118
+
109
119
// We are in an App Router context. We try to return the cached entry if it exists and is valid
110
120
// If the entry is fresh we return it. If the entry is stale we return it but revalidate the entry in
111
121
// the background. If the entry is missing or invalid we generate a new entry and return it.
@@ -156,6 +166,7 @@ export function unstable_cache<T extends Callback>(
156
166
revalidate : options . revalidate ,
157
167
tags,
158
168
softTags : implicitTags ,
169
+ fetchIdx,
159
170
} )
160
171
161
172
if ( cacheEntry && cacheEntry . value ) {
@@ -198,7 +209,9 @@ export function unstable_cache<T extends Callback>(
198
209
incrementalCache ,
199
210
cacheKey ,
200
211
tags ,
201
- options . revalidate
212
+ options . revalidate ,
213
+ fetchIdx ,
214
+ fetchUrl
202
215
)
203
216
} )
204
217
// @TODO This error handling seems wrong. We swallow the error?
@@ -232,10 +245,13 @@ export function unstable_cache<T extends Callback>(
232
245
incrementalCache ,
233
246
cacheKey ,
234
247
tags ,
235
- options . revalidate
248
+ options . revalidate ,
249
+ fetchIdx ,
250
+ fetchUrl
236
251
)
237
252
return result
238
253
} else {
254
+ noStoreFetchIdx += 1
239
255
// We are in Pages Router or were called outside of a render. We don't have a store
240
256
// so we just call the callback directly when it needs to run.
241
257
// If the entry is fresh we return it. If the entry is stale we return it but revalidate the entry in
@@ -295,7 +311,9 @@ export function unstable_cache<T extends Callback>(
295
311
incrementalCache ,
296
312
cacheKey ,
297
313
tags ,
298
- options . revalidate
314
+ options . revalidate ,
315
+ fetchIdx ,
316
+ fetchUrl
299
317
)
300
318
return result
301
319
}
0 commit comments