unstable_cache
Answered
Argentine ant posted this in #help-forum
Argentine antOP
Hi 👋,
just a quick question about unstable_cache!
https://nextjs.org/docs/app/api-reference/functions/unstable_cache
specifically about key parts,
1) say we have some dynamic data, such as
many thanks!
just a quick question about unstable_cache!
https://nextjs.org/docs/app/api-reference/functions/unstable_cache
specifically about key parts,
1) say we have some dynamic data, such as
page that is passed into the function, do we have to specify this in the key parts? i.e. [service-data-${page}], or would service-data suffice? Or does this cause an issue when calling revalidateTag many thanks!
Answered by joulev
yes. that key you put in the third parameter
const getData = unstable_cache(
async (...) => ...,
[],
{ tags: ["data"] }
)revalidateTag("data")10 Replies
@Argentine ant Hi 👋,
just a quick question about unstable_cache!
https://nextjs.org/docs/app/api-reference/functions/unstable_cache
specifically about key parts,
1) say we have some dynamic data, such as `page` that is passed into the function, do we have to specify this in the key parts? i.e. `[`service-data-${page}`]`, or would `service-data` suffice? Or does this cause an issue when calling `revalidateTag`
many thanks!
the keyparts is not for
the keypart is only there to differentiate different cache items for the same unstable_cache run on the same parameters. you don't need to specify anything in the array most of the time.
revalidateTag. you need to add to the tag array in the third parameter instead.the keypart is only there to differentiate different cache items for the same unstable_cache run on the same parameters. you don't need to specify anything in the array most of the time.
@joulev the keyparts is not for `revalidateTag`. you need to add to the tag array in the third parameter instead.
the keypart is only there to differentiate different cache items for the same unstable_cache run on the same parameters. you don't need to specify anything in the array most of the time.
Argentine antOP
thanks for the reply - i realised this after i posted!
so just to check, for everywhere that I use unstable_cache, i should add in the params of the function into the key? otherwise wouldn't be able to revalidate?
so just to check, for everywhere that I use unstable_cache, i should add in the params of the function into the key? otherwise wouldn't be able to revalidate?
@Argentine ant thanks for the reply - i realised this after i posted!
so just to check, for everywhere that I use unstable_cache, i should add in the params of the function into the key? otherwise wouldn't be able to revalidate?
the params of the function are automatically keys already, hence what i said: you don't really need to use this option most of the time.
basically, nextjs (at the moment) generates a single string key from the information collected in
basically, nextjs (at the moment) generates a single string key from the information collected in
unstable_cache:// cb is the async function in unstable_cache
const fixedKey = `${cb.toString()}-${
Array.isArray(keyParts) && keyParts.join(',')
}`
// this is the key that is used
const invocationKey = `${fixedKey}-${JSON.stringify(args)}`@joulev the params of the function are automatically keys already, hence what i said: you don't really need to use this option most of the time.
basically, nextjs (at the moment) generates a single string key from the information collected in `unstable_cache`:
tsx
// cb is the async function in unstable_cache
const fixedKey = `${cb.toString()}-${
Array.isArray(keyParts) && keyParts.join(',')
}`
// this is the key that is used
const invocationKey = `${fixedKey}-${JSON.stringify(args)}`
Argentine antOP
yeah that makes sense, but for revalidation we'd need to run revalidateTag with a specific key right?
@Argentine ant yeah that makes sense, but for revalidation we'd need to run revalidateTag with a specific key right?
yes. that key you put in the third parameter
const getData = unstable_cache(
async (...) => ...,
[],
{ tags: ["data"] }
)revalidateTag("data")Answer
@joulev yes. that key you put in the third parameter
tsx
const getData = unstable_cache(
async (...) => ...,
[],
{ tags: ["data"] }
)
tsx
revalidateTag("data")
Argentine antOP
so for the async function here, it would make sense to turn the arguments into a string and append, like so?
{tags: [data-${argument1}]}@Argentine ant so for the async function here, it would make sense to turn the arguments into a string and append, like so?
`{tags: [`data-${argument1}`]}`
the tag can be a string template, nothing wrong with it yes
const getData = (id: number) =>
unstable_cache(
async (id: number) => ...,
[],
{ tags: [`data-${id}`] },
)(id)i use this pattern a lot
Argentine antOP
perfect, thanks!
oh, i didn't really think about doing that haha