← Back
Edit Post
Title
Description
Request deduplication with cache(), combining with "use cache" for cross-request caching.
Content
Markdown supported
# React cache() React's `cache()` deduplicates requests within a single render pass. ## Example: getPostBySlug From `data/queries/post-queries.ts`: ```tsx import { cache } from 'react'; export const getPostBySlug = cache(async (slug: string) => { const post = await prisma.post.findUnique({ where: { slug } }); if (!post) notFound(); return post; }); ``` Multiple components can call `getPostBySlug(slug)` independently—only one query executes. ## Combining with "use cache" `cache()` deduplicates within a render. `"use cache"` caches across requests: ```tsx export const getPublishedPostBySlug = cache(async (slug: string) => { 'use cache'; cacheTag(`post-${slug}`); return await prisma.post.findUnique({ where: { slug } }); }); ``` Both work together—`cache()` prevents duplicate queries during rendering, `"use cache"` stores results for future requests.
Published
Save Changes
Cancel