reactjsnext.jsmemoizationreact-server-componentsreact-cache

Can I tag data access for manual cache revalidation without using fetch with Next app router?


I would like to implement a revalidation endpoint in a Next project with the app router, so I think my options would be revalidatePath and revalidateTag.

The data access I want revalidated works with a 3rd party library which requests resources from a CMS without a direct invocation of fetch, also I placed the operation inside a React cached function.

I could create an api in Next to hold the data access, then call the api through fetch from the components to leverage the tag option that comes bundled with fetch in Next, but I'd like to avoid exposing a public api for this operation.

Can I tag for manual revalidation a cached React function in Next? In general, is there a way to tag data access operations other then passing the tag option to fetch?


Solution

  • After testing a bit more I realized that the function cached with React.cache is not memoizing at the data cache level, the function still runs (once) with every request (even if invoked multiple times).

    I guess the only way to cache the data access operation would be to stick it inside a next api...


    2024/01/23 edit:

    The actual real solution is to use unstable_cache and pass revalidate and/or tags options.

    Hopefully this will soon lose the unstable prefix.