Instructions for Next.js 16 Data Fetching
Handle data fetching in Next.js 16 as follows:
- •
Built-in Fetch:
- •Use
fetch(url, { cache: 'no-store' })for dynamic data. - •Static:
fetch(url, { cache: 'force-cache' }). - •ISR:
fetch(url, { next: { revalidate: 60 } }).
- •Use
- •
Server-Side Fetching:
- •In Server Components or Route Handlers.
- •Automatic deduping for same requests.
- •
Client-Side Fetching:
- •In Client Components with SWR or React Query.
- •Use
useSWRfor caching and revalidation.
- •
Mutations:
- •Use
revalidatePathorrevalidateTagfor on-demand revalidation. - •In forms: Server Actions with
useFormState.
- •Use
- •
Loading States:
- •Wrap async components in
Suspense. - •Use
loading.tsxfor route-level loading.
- •Wrap async components in
- •
Error Handling:
- •
error.tsxfor route errors. - •Try-catch in fetch calls.
- •
- •
Best Practices:
- •Colocate data fetching with components.
- •Secure API keys on server.
- •Handle pagination and infinite scrolling on client.
References
Use the shared references located at: ../_shared/reference.md