Skip to main content
rule CX Codex

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

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/server-cache-react.md
Open on GitHub

What 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.md

The paths frontmatter field limits the files

Step 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

Similar IDE rules

All IDE rules for Frontend and UI →
runtime-cache official Vercel Labs A skill for Codex and ChatGPT that teaches the agent Vercel Runtime Cache — an ephemeral key-value store with tag-based invalidation. Triggers when working with cache in Vercel Functions, Middleware, or Builds. CX Codex GP ChatGPT +1 6installs nextjs-cache-handler-mcp leejpsd The nextjs-cache-handler-mcp MCP server lets an AI agent inspect a Next.js Redis cache, trace tag state, model SWR reads, and invalidate tags with a confirmation gate. CX Codex +1 ★ 6repo stars nextjs-cache-architecture mohamed-hossam1 nextjs-cache-architecture structures caching architecture in Next.js 16+ App Router projects. It sets up the "use cache" directive, a typed tag registry, and Suspense boundaries. CX Codex +1 5,100installs cache-components laguagu A skill for mastering Next.js Cache Components and Partial Prerendering in version 16.4+. Advises on the use cache directive, setting cache lifetime with cacheLife(), tagging data with cacheTag(), and invalidating via updateTag(). CX Codex +1 222installs next-cache-components official Vercel Labs next-cache-components is a Codex skill for Cache Components in Next.js 16+. It activates for PPR work, caching changes, or migrations from unstable_cache. CX Codex GP ChatGPT +1 4installs next-cache-components-optimizer vercel next-cache-components-optimizer optimizes Next.js App Shell UI on hard and soft navigations. It sets up failing Playwright instant() checks and guides routes to green. CX Codex +1 22,000installs

From the same set130

The whole set →
rerender-lazy-state-init The rerender-lazy-state-init rule recommends lazy useState initialization for costly starting values. It helps when a React component reads stored settings or builds a data structure. CX Codex +1 ★ 7,191repo stars async-defer-await async-defer-await — a rule that moves await operations into code branches where they're actually needed, preventing blockage of execution paths that may exit early. CX Codex +1 ★ 7,191repo stars server-dedup-props server-dedup-props — a rule for Claude Code about React Server Components: RSC-to-client serialization deduplicates by reference, not value. Server-side transforms like toSorted or filter create new references and double the payload. CX Codex +1 ★ 7,191repo stars rendering-usetransition-loading rendering-usetransition-loading — a rule that swaps hand-rolled loading flags in React components for useTransition. Useful when a spinner runs on its own useState. CX Codex +1 ★ 7,191repo stars architecture-avoid-boolean-props The architecture-avoid-boolean-props rule keeps component variants out of boolean props and moves them into composition. Use it when conditional UI branches start spreading across a component. CX Codex +1 ★ 7,191repo stars rendering-hydration-no-flicker rendering-hydration-no-flicker prevents server render failures and interface flickering when displaying content tied to browser storage. CX Codex +1 ★ 7,191repo stars bundle-barrel-imports The bundle-barrel-imports rule steers imports away from barrel files and toward source modules. It helps when a package exposes many re-exports and slows loading. CX Codex +1 ★ 7,191repo stars patterns-explicit-variants patterns-explicit-variants — a rule that pushes one component per mode instead of a single component buried under boolean props. The call site then says what renders. CX Codex +1 ★ 7,191repo stars

Project sponsors

Companies that keep this analytics open