swr
SWR data-fetching expert guidance. Use when building React apps with client-side data fetching, caching, revalidation, mutations, optimistic UI, pagination, or infinite loading using the SWR library.
- License 许可
- MIT
- 最近更新
- 2026-09-22
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- swr
- metadata
- { "docs": [ "https://swr.vercel.app/docs" ], "sitemap": "https://swr.vercel.app/sitemap.xml", "priority": 4, "bashPatterns": [ "\\bnpm\\s+(install|i|add)\\s+[^\\n]*\\bswr\\b", "\\bpnpm\\s+(install|i|add)\\s+[^\\n]*\\bswr\\b", "\\bbun\\s+(install|i|add)\\s+[^\\n]*\\bswr\\b", "\\byarn\\s+add\\s+[^\\n]*\\bswr\\b" ], "pathPatterns": [ "lib/fetcher.*", "src/lib/fetcher.*", "utils/fetcher.*", "src/utils/fetcher.*", "hooks/use*SWR*", "src/hooks/use*SWR*", "hooks/useFetch*", "src/hooks/useFetch*" ], "promptSignals": { "allOf": [ [ "data fetching", "client" ], [ "cache", "revalidat" ] ], "anyOf": [ "mutation", "optimistic", "infinite loading", "pagination" ], "noneOf": [], "phrases": [ "swr", "useswr", "stale-while-revalidate" ], "minScore": 6 }, "importPatterns": [ "swr", "swr/*" ] }
- description
- SWR data-fetching expert guidance. Use when building React apps with client-side data fetching, caching, revalidation, mutations, optimistic UI, pagination, or infinite loading using the SWR library.
02 资源文件 1
- 参考文档agents/openai.yaml90 B
03 SKILL.md 正文
SWR — React Hooks for Data Fetching
You are an expert in SWR v2 (latest: 2.4.1), the React Hooks library for data fetching by Vercel. SWR implements the stale-while-revalidate HTTP cache invalidation strategy — serve from cache first, then revalidate in the background.
Installation
npm install swr
Core API
useSWR
import useSWR from 'swr'
const fetcher = (url: string) => fetch(url).then(res => res.json())
function Profile() {
const { data, error, isLoading, mutate } = useSWR('/api/user', fetcher)
if (isLoading) return <div>Loading...</div>
if (error) return <div>Error loading data</div>
return <div>Hello, {data.name}</div>
}
Key parameters:
key— unique string, array, or function identifying the resource (often a URL)fetcher— async function that receives the key and returns dataoptions— optional config object
Return values: data, error, isLoading, isValidating, mutate
useSWRMutation — Remote Mutations
import useSWRMutation from 'swr/mutation'
async function updateUser(url: string, { arg }: { arg: { name: string } }) {
return fetch(url, { method: 'POST', body: JSON.stringify(arg) }).then(res => res.json())
}
function Profile() {
const { trigger, isMutating } = useSWRMutation('/api/user', updateUser)
return (
<button disabled={isMutating} onClick={() => trigger({ name: 'New Name' })}>
Update
</button>
)
}
useSWRInfinite — Pagination & Infinite Loading
import useSWRInfinite from 'swr/infinite'
const getKey = (pageIndex: number, previousPageData: any[]) => {
if (previousPageData && !previousPageData.length) return null
return `/api/items?page=${pageIndex}`
}
function Items() {
const { data, size, setSize, isLoading } = useSWRInfinite(getKey, fetcher)
const items = data ? data.flat() : []
return (
<>
{items.map(item => <div key={item.id}>{item.name}</div>)}
<button onClick={() => setSize(size + 1)}>Load More</button>
</>
)
}
Global Configuration
Wrap your app (or a subtree) with SWRConfig to set defaults:
import { SWRConfig } from 'swr'
function App() {
return (
<SWRConfig value={{
fetcher: (url: string) => fetch(url).then(res => res.json()),
revalidateOnFocus: false,
dedupingInterval: 5000,
}}>
<Dashboard />
</SWRConfig>
)
}
Revalidation Strategies
| Strategy | Option | Default |
|---|---|---|
| On window focus | revalidateOnFocus |
true |
| On network recovery | revalidateOnReconnect |
true |
| On mount if stale | revalidateIfStale |
true |
| Polling | refreshInterval |
0 (disabled) |
| Manual | Call mutate() |
— |
Optimistic Updates
const { trigger } = useSWRMutation('/api/user', updateUser, {
optimisticData: (current) => ({ ...current, name: 'New Name' }),
rollbackOnError: true,
populateCache: true,
revalidate: false,
})
Conditional Fetching
Pass null or a falsy key to skip fetching:
const { data } = useSWR(userId ? `/api/user/${userId}` : null, fetcher)
Error Retry
SWR retries on error by default with exponential backoff. Customize with:
useSWR(key, fetcher, {
onErrorRetry: (error, key, config, revalidate, { retryCount }) => {
if (error.status === 404) return // Don't retry on 404
if (retryCount >= 3) return // Max 3 retries
setTimeout(() => revalidate({ retryCount }), 5000)
},
})
useSWRSubscription — Real-Time Data Sources
Subscribe to real-time data (WebSockets, SSE, etc.) with automatic deduplication:
import useSWRSubscription from 'swr/subscription'
function LivePrice({ symbol }: { symbol: string }) {
const { data } = useSWRSubscription(
`wss://stream.example.com/${symbol}`,
(key, { next }) => {
const ws = new WebSocket(key)
ws.onmessage = (event) => next(null, JSON.parse(event.data))
ws.onerror = (event) => next(event)
return () => ws.close()
}
)
return <span>{data?.price}</span>
}
The subscribe function receives a next(error, data) callback and must return a cleanup function. Multiple components using the same key share a single subscription.
Key Rules
- Keys must be unique — two
useSWRcalls with the same key share cache and deduplicate requests - Fetcher is optional when set via
SWRConfig mutate(key)globally revalidates any hook matching that key- Array keys like
useSWR(['/api/user', id], fetcher)— the fetcher receives the full array - Never call hooks conditionally — use conditional keys (
null) instead
04 同仓库更多 Skills
skill-installer
Install Codex skills into $CODEX_HOME/skills from a curated list or a GitHub repo path. Use when a user asks to list installable skills, install a curated skill, or install a skill from another repo (including private repos).
skill-creator
Create or update a Codex skill with appropriately scoped instructions and any needed supporting resources.
review-agent
Perform a read-only, defect-first review of a specified code change and return every actionable finding. Use when another agent delegates review of uncommitted changes, a base-branch diff, a commit, or custom review instructions.
plugin-creator
Create and scaffold plugin directories for Codex with a required `.codex-plugin/plugin.json`, optional plugin folders/files, valid manifest defaults, and personal-marketplace entries by default. Use when Codex needs to create a new personal plugin, add optional plugin structure, generate or update marketplace entries for plugin ordering and availability metadata, or update an existing local plugin during development with the CLI-driven cachebuster and reinstall flow.
openai-docs
Use for Codex models/pricing, scheduled tasks, skills, settings, setup, troubleshooting, customization, automations, and self-knowledge—including 'you,' 'your,' 'this app,' or 'this coding agent' when they refer to Codex—and for OpenAI APIs/products and ChatGPT Work. Also use for model choice/migration, prompting, SDKs, Responses, Realtime, agents, evals, and Chat/Work/Codex comparisons. Do not use for generic app/software tasks that merely mention Codex.
imagegen
Generate or edit raster images when the task benefits from AI-created bitmap visuals such as photos, illustrations, textures, sprites, mockups, or transparent-background cutouts. Use when Codex should create a brand-new image, transform an existing image, or derive visual variants from references, and the output should be a bitmap asset rather than repo-native code or vector. Do not use when the task is better handled by editing existing SVG/vector/code-native assets, extending an established icon or logo system, or building the visual directly in HTML/CSS/canvas.