Next.js Discord

Discord Forum

How to use unstable cache with dynamic ID

Unanswered
AM posted this in #help-forum
Open in Discord
AMOP
I do have this code:

export const getCachedGig = unstable_cache(
  (gigId: number) => gigsService.getGig(gigId),
  [`gig:${gigId}`],
  {
    tags: [`gig:${gigId}`],
  },
);


but not able to compile it, how to have unstable cache with id for details similar how react query works? thanks

7 Replies

@Yi Lon Ma https://nextjs.org/docs/app/api-reference/functions/unstable_cache#example
AMOP
ah ty so much so it goes something like this:

export const getCachedGig = (gigId: number) =>
  unstable_cache(() => gigsService.getGig(gigId), [`gig:${gigId}`], {
    tags: [`gig:${gigId}`],
  });
@Yi Lon Ma yes
AMOP
thanks but the invocation is bit weird like so 😄

export async function getGig(id: number): Promise<GigResponse | null> {
  return getCachedGig(id)();
}
yea since it returns a function
you can just call it inside the getCachedGig itself
@Yi Lon Ma you can just call it inside the getCachedGig itself
AMOP
yeah cool input done now will test it out hopefully i revalidateTags correctly lol