server-cache-react — Claude Code rule: what it does, how to install
server-cache-react instructs the assistant to deduplicate server-side operations with React.cache() and prevent cache misses from inline object arguments.
MIT ★ 7,191 updated 2026-10-24
.claude/rules/server-cache-react.mdWhat server-cache-react does
This rule guides the assistant to apply React.cache() for per-request deduplication across the component tree. It targets database calls via Prisma or Drizzle, auth lookups, and disk reads.
Because React.cache() relies on Object.is shallow checks, inline object arguments generate fresh references and cause cache misses every time. The rule instructs the model to pass primitive values or stable object references instead.
Next.js patches native fetch with its own memoization layer, making React.cache() redundant for HTTP requests. For all other non-fetch async workloads, explicit caching remains mandatory.
What it helps with
- deduplicate database queries within a single server request
- prevent cache misses caused by inline object arguments
- memoize repeated user session and permission lookups
- optimize non-fetch asynchronous workloads across component trees
Example prompts for the agent
- Wrap the user session lookup in React.cache
- Check why this React.cache function misses on every call
- Write a memoized Prisma query helper for user profiles
How to install server-cache-react
Manually: put the files in place
.claude/rules/server-cache-react.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/server-cache-react.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/server-cache-react.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/server-cache-react.mdManually: put the files in place
.clinerules/server-cache-react.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about server-cache-react
How to install server-cache-react in Claude Code?
Save the rule file to .claude/rules/server-cache-react.md.
What does server-cache-react do?
It directs the coding agent to deduplicate server-side async operations with React.cache() and avoid passing inline objects as arguments.
Is React.cache needed for fetch in Next.js?
Next.js deduplicates identical fetch requests automatically. You still need React.cache() for ORM queries, disk access, and custom async helpers.
Source text
server-cache-react.md · MIT
---
title: Per-Request Deduplication with React.cache()
impact: MEDIUM
impactDescription: deduplicates within request
tags: server, cache, react-cache, deduplication
---
## Per-Request Deduplication with React.cache()
Use `React.cache()` for server-side request deduplication. Authentication and database queries benefit most.
**Usage:**
```typescript
import { cache } from 'react'
export const getCurrentUser = cache(async () => {
const session = await auth()
if (!session?.user?.id) return null
return await db.user.findUnique({
where: { id: session.user.id }
})
})
```
Within a single request, multiple calls to `getCurrentUser()` execute the query only once.
**Avoid inline objects as arguments:**
`React.cache()` uses shallow equality (`Object.is`) to determine cache hits. Inline objects create new references each call, preventing cache hits.
**Incorrect (always cache miss):**
```typescript
const getUser = cache(async (params: { uid: number }) => {
return await db.user.findUnique({ where: { id: params.uid } })
})
// Each call creates new object, never hits cache
getUser({ uid: 1 })
getUser({ uid: 1 }) // Cache miss, runs query again
```
**Correct (cache hit):**
```typescript
const getUser = cache(async (uid: number) => {
return await db.user.findUnique({ where: { id: uid } })
})
// Primitive args use value equality
getUser(1)
getUser(1) // Cache hit, returns cached result
```
If you must pass objects, pass the same reference:
```typescript
const params = { uid: 1 }
getUser(params) // Query runs
getUser(params) // Cache hit (same reference)
```
**Next.js-Specific Note:**
In Next.js, the `fetch` API is automatically extended with request memoization. Requests with the same URL and options are automatically deduplicated within a single request, so you don't need `React.cache()` for `fetch` calls. However, `React.cache()` is still essential for other async tasks:
- Database queries (Prisma, Drizzle, etc.)
- Heavy computations
- Authentication checks
- File system operations
- Any non-fetch async work
Use `React.cache()` to deduplicate these operations across your component tree.
Reference: [React.cache documentation](https://react.dev/reference/react/cache)
© tech-leads-club · MIT · tech-leads-club/agent-skills