| Client | Server |
|---|---|
| Use React hooks such as useState, useEffect, useReducer | Fetch Data |
| Interactivity within the component, with event listeners (onClick()) | Store sensitive information on server (tokens, API keys, etc.) |
| Use custom hooks that depend on state, effects. | Access backend resources directly |
| Keep large dependencies on the server |
Data Fetching Strategies can be declared on a Page-by-Page basis
This is the default behavior ( unless specified otherwise )
great solution for pages that dont change often ( such as blog posts or help documents )
- Data is fecthed once at build time
- Fetched data is cached and reused on subsquent requests
sequenceDiagram
actor User
box gray Build Time
participant Cache
participant Data
end
box Another group
end
Cache->>Data: Server Side Request
User->>Cache: Request
Following example pulls data from the cache rather than a live request
async function fetchData() {
const res = await fetch( 'https://reqbin.com/echo' ); // default is a static site generation
const data = await res.json();
return data;
}- Data is fecthed and cached at build time
- Fetched data is extracted from the cached and reused on subsquent requests until it is expired.
- Once data is expired, the server is contacted and the data is refreshed.
sequenceDiagram
actor User
box gray Build Time
participant Cache
participant Data
end
Cache->>Data: Server Side Request
activate Cache
User->>Cache: Request
Cache->>Cache: Expire
Cache->>Data: Server request
Following example pulls data from the cache rather than a live request, and refreshes every n seconds
async function fetchData() {
const res = await fetch(
'https://reqbin.com/echo',
{ next: { revalidate: 15 } } ); // will call a fresh request every 15 seconds
const data = await res.json();
return data;
}- No caching at all.
- All data is fetched from the server
sequenceDiagram
actor User
box gray Server
participant Data
end
User->>Data: Request
Following example pulls data from the server, every time
async function fetchData() {
const res = await fetch(
'https://reqbin.com/echo',
{ cache: 'no-store' }
); // No cache, every request
const data = await res.json();
return data;
}