- docs/agent/external: Vercel react-best-practices and composition-patterns (MIT), Addy Osmani accessibility (MIT), Anthropic frontend-design (Apache-2.0), official Payload skill at v3.92.1 (MIT) — text only, each with its licence, SOURCE.md (repo, commit) and an override note in front of the SKILL.md (ipal-kit first, Payload fetching over swr, DESIGN.md tokens, Payload 3 not 4); manifest.json with sha256 per file - scripts/vendor-external-skills.mjs (maintainer, not shipped): fetches by full commit SHA only, rejects non-text or executable files, regenerates the manifest; --check verifies it - ipal-skills --external [names]: copies ext-* after checking every file against the manifest, downloads nothing, never touches ext-* folders without ipal-kit's SOURCE.md, removes only retired ones it installed; an unknown name stops the run before anything is copied - AGENT.md §14, ipal-setup stage 2 (optional step), README: usage, overrides, how to update the pins - test/externalSkills.test.ts: pins are SHAs, no branch fetch, checksums, text only, licences, override note, installer leaves other folders alone Co-Authored-By: Claude Opus 5.5 <[email protected]>
77 lines
2.2 KiB
Markdown
77 lines
2.2 KiB
Markdown
---
|
|
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)
|