Skip to content

Commit 752c15e

Browse files
authored
Add metrics names for unstable_cache (#60802)
This ensures we add params for `unstable_cache` so it can be identified the same was as fetches instead of being unlabeled. Closes NEXT-2143
1 parent e733853 commit 752c15e

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

packages/next/src/server/web/spec-extension/unstable-cache.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ import {
1414

1515
type Callback = (...args: any[]) => Promise<any>
1616

17+
let noStoreFetchIdx = 0
18+
1719
async function cacheNewResult<T>(
1820
result: T,
1921
incrementalCache: IncrementalCache,
2022
cacheKey: string,
2123
tags: string[],
22-
revalidate: number | false | undefined
24+
revalidate: number | false | undefined,
25+
fetchIdx: number,
26+
fetchUrl: string
2327
): Promise<unknown> {
2428
await incrementalCache.set(
2529
cacheKey,
@@ -38,6 +42,8 @@ async function cacheNewResult<T>(
3842
revalidate,
3943
fetchCache: true,
4044
tags,
45+
fetchIdx,
46+
fetchUrl,
4147
}
4248
)
4349
return
@@ -104,8 +110,12 @@ export function unstable_cache<T extends Callback>(
104110
// the keyspace smaller than the execution space
105111
const invocationKey = `${fixedKey}-${JSON.stringify(args)}`
106112
const cacheKey = await incrementalCache.fetchCacheKey(invocationKey)
113+
const fetchUrl = `unstable_cache ${cb.name ? ` ${cb.name}` : cacheKey}`
114+
const fetchIdx = (store ? store.nextFetchId : noStoreFetchIdx) ?? 1
107115

108116
if (store) {
117+
store.nextFetchId = fetchIdx + 1
118+
109119
// We are in an App Router context. We try to return the cached entry if it exists and is valid
110120
// If the entry is fresh we return it. If the entry is stale we return it but revalidate the entry in
111121
// 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>(
156166
revalidate: options.revalidate,
157167
tags,
158168
softTags: implicitTags,
169+
fetchIdx,
159170
})
160171

161172
if (cacheEntry && cacheEntry.value) {
@@ -198,7 +209,9 @@ export function unstable_cache<T extends Callback>(
198209
incrementalCache,
199210
cacheKey,
200211
tags,
201-
options.revalidate
212+
options.revalidate,
213+
fetchIdx,
214+
fetchUrl
202215
)
203216
})
204217
// @TODO This error handling seems wrong. We swallow the error?
@@ -232,10 +245,13 @@ export function unstable_cache<T extends Callback>(
232245
incrementalCache,
233246
cacheKey,
234247
tags,
235-
options.revalidate
248+
options.revalidate,
249+
fetchIdx,
250+
fetchUrl
236251
)
237252
return result
238253
} else {
254+
noStoreFetchIdx += 1
239255
// We are in Pages Router or were called outside of a render. We don't have a store
240256
// so we just call the callback directly when it needs to run.
241257
// 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>(
295311
incrementalCache,
296312
cacheKey,
297313
tags,
298-
options.revalidate
314+
options.revalidate,
315+
fetchIdx,
316+
fetchUrl
299317
)
300318
return result
301319
}

0 commit comments

Comments
 (0)