Unverified Commit 752c15e7 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

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
parent e733853c
...@@ -14,12 +14,16 @@ import { ...@@ -14,12 +14,16 @@ import {
type Callback = (...args: any[]) => Promise<any> type Callback = (...args: any[]) => Promise<any>
let noStoreFetchIdx = 0
async function cacheNewResult<T>( async function cacheNewResult<T>(
result: T, result: T,
incrementalCache: IncrementalCache, incrementalCache: IncrementalCache,
cacheKey: string, cacheKey: string,
tags: string[], tags: string[],
revalidate: number | false | undefined revalidate: number | false | undefined,
fetchIdx: number,
fetchUrl: string
): Promise<unknown> { ): Promise<unknown> {
await incrementalCache.set( await incrementalCache.set(
cacheKey, cacheKey,
...@@ -38,6 +42,8 @@ async function cacheNewResult<T>( ...@@ -38,6 +42,8 @@ async function cacheNewResult<T>(
revalidate, revalidate,
fetchCache: true, fetchCache: true,
tags, tags,
fetchIdx,
fetchUrl,
} }
) )
return return
...@@ -104,8 +110,12 @@ export function unstable_cache<T extends Callback>( ...@@ -104,8 +110,12 @@ export function unstable_cache<T extends Callback>(
// the keyspace smaller than the execution space // the keyspace smaller than the execution space
const invocationKey = `${fixedKey}-${JSON.stringify(args)}` const invocationKey = `${fixedKey}-${JSON.stringify(args)}`
const cacheKey = await incrementalCache.fetchCacheKey(invocationKey) const cacheKey = await incrementalCache.fetchCacheKey(invocationKey)
const fetchUrl = `unstable_cache ${cb.name ? ` ${cb.name}` : cacheKey}`
const fetchIdx = (store ? store.nextFetchId : noStoreFetchIdx) ?? 1
if (store) { if (store) {
store.nextFetchId = fetchIdx + 1
// We are in an App Router context. We try to return the cached entry if it exists and is valid // We are in an App Router context. We try to return the cached entry if it exists and is valid
// If the entry is fresh we return it. If the entry is stale we return it but revalidate the entry in // If the entry is fresh we return it. If the entry is stale we return it but revalidate the entry in
// the background. If the entry is missing or invalid we generate a new entry and return it. // 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,6 +166,7 @@ export function unstable_cache<T extends Callback>(
revalidate: options.revalidate, revalidate: options.revalidate,
tags, tags,
softTags: implicitTags, softTags: implicitTags,
fetchIdx,
}) })
if (cacheEntry && cacheEntry.value) { if (cacheEntry && cacheEntry.value) {
...@@ -198,7 +209,9 @@ export function unstable_cache<T extends Callback>( ...@@ -198,7 +209,9 @@ export function unstable_cache<T extends Callback>(
incrementalCache, incrementalCache,
cacheKey, cacheKey,
tags, tags,
options.revalidate options.revalidate,
fetchIdx,
fetchUrl
) )
}) })
// @TODO This error handling seems wrong. We swallow the error? // @TODO This error handling seems wrong. We swallow the error?
...@@ -232,10 +245,13 @@ export function unstable_cache<T extends Callback>( ...@@ -232,10 +245,13 @@ export function unstable_cache<T extends Callback>(
incrementalCache, incrementalCache,
cacheKey, cacheKey,
tags, tags,
options.revalidate options.revalidate,
fetchIdx,
fetchUrl
) )
return result return result
} else { } else {
noStoreFetchIdx += 1
// We are in Pages Router or were called outside of a render. We don't have a store // We are in Pages Router or were called outside of a render. We don't have a store
// so we just call the callback directly when it needs to run. // so we just call the callback directly when it needs to run.
// If the entry is fresh we return it. If the entry is stale we return it but revalidate the entry in // 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,7 +311,9 @@ export function unstable_cache<T extends Callback>(
incrementalCache, incrementalCache,
cacheKey, cacheKey,
tags, tags,
options.revalidate options.revalidate,
fetchIdx,
fetchUrl
) )
return result return result
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment