Open phymo opened 1 month ago
you can call a fetch function for the same data in multiple places in a React component tree while only executing it once.
Once the route has been rendered and the rendering pass is complete, memory is "reset" and all request memoization entries are cleared.
Good to know:
GET
method in fetch
requestsIn the browser, the cache option of fetch indicates how a request will interact with the browser's HTTP cache, in Next.js, the cache option indicates how a server-side request will interact with the server's Data Cache.
time-based revalidation fetch('https://...', { next: { revalidate: 3600 } })
on-demand revalidation: Data can be revalidated on-demand by path (revalidatePath
) or by cache tag (revalidateTag
).
// Opt out of caching for an individual `fetch` request
fetch(`https://...`, { cache: 'no-store' })
// Opt out of caching for all data requests in the route segment
export const dynamic = 'force-dynamic'
Next.js automatically renders and caches routes at build time. By default, the Full Route Cache is persistent.
Caching on server
- React renders Server Components into a special data format, optimized for streaming, called the React Server Component Payload.
- Next.js uses the React Server Component Payload and Client Component JavaScript instructions to render HTML on the server.
![]()
Caching on client
At request time, on the client:
- The HTML is used to immediately show a fast non-interactive initial preview of the Client and Server Components.
- The React Server Components Payload is used to reconcile the Client and rendered Server Component trees, and update the DOM.
- The JavaScript instructions are used to hydrate Client Components and make the application interactive. The React Server Component Payload is stored in the client-side Router Cache - a separate in-memory cache, split by individual route segment
Server Action
:
revalidatePath
or revalidateTag
cookies.set
or cookies.delete
router.refresh
Caching behavior changes depending on: