Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply 62 React and Next.js performance optimization rules from Vercel Engineering
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/client-swr-dedup.md
1---2title: Use SWR for Automatic Deduplication3impact: MEDIUM-HIGH4impactDescription: automatic deduplication5tags: client, swr, deduplication, data-fetching6---78## Use SWR for Automatic Deduplication910SWR enables request deduplication, caching, and revalidation across component instances.1112**Incorrect (no deduplication, each instance fetches):**1314```tsx15function UserList() {16const [users, setUsers] = useState([])17useEffect(() => {18fetch('/api/users')19.then(r => r.json())20.then(setUsers)21}, [])22}23```2425**Correct (multiple instances share one request):**2627```tsx28import useSWR from 'swr'2930function UserList() {31const { data: users } = useSWR('/api/users', fetcher)32}33```3435**For immutable data:**3637```tsx38import { useImmutableSWR } from '@/lib/swr'3940function StaticContent() {41const { data } = useImmutableSWR('/api/config', fetcher)42}43```4445**For mutations:**4647```tsx48import { useSWRMutation } from 'swr/mutation'4950function UpdateButton() {51const { trigger } = useSWRMutation('/api/user', updateUser)52return <button onClick={() => trigger()}>Update</button>53}54```5556Reference: [https://swr.vercel.app](https://swr.vercel.app)57