Created
December 15, 2024 16:30
-
-
Save HarleySalas/f059cfc8150074c779b806764b8fe890 to your computer and use it in GitHub Desktop.
Combine React's cache with Next's unstable_cache, with a simpler API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { cache as reactCache } from 'react' | |
import { unstable_cache as nextCache } from 'next/cache' | |
type CacheOptions = { | |
revalidate?: number | false | |
tags?: string[] | ((...args: any[]) => string[]) | |
} | |
/** | |
* Combines React's cache (for deduping requests) with Next.js unstable_cache (for persistent caching). | |
* Allows dynamic tags based on function arguments for targeted revalidation. | |
* @see https://nextjs.org/docs/app/api-reference/functions/unstable_cache#parameters | |
* @param fn - The async function to cache | |
* @param options - Cache configuration (revalidate duration and tags) | |
* @param closureVars - Only needed if fn uses external variables not passed as args | |
*/ | |
export const cache = <TArgs extends any[], TResult>( | |
fn: (...args: TArgs) => Promise<TResult>, | |
options?: CacheOptions, | |
closureVars?: string[], | |
) => | |
reactCache((...args: TArgs) => | |
nextCache(fn, closureVars, { | |
revalidate: options?.revalidate, | |
tags: typeof options?.tags === 'function' ? options.tags(...args) : options?.tags, | |
})(...args), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Learn more about unstable_cache and React.Cache in NextJs
Usage examples
Simple case with static tags:
Dynamic tags based on input