Understanding Next.js Data Fetching (CSR, SSR, SSG, ISR)
Thorough explanation on Next.js data fetching method such as CSR, SSR, SSG, and ISR.

Search for a command to run...
Thorough explanation on Next.js data fetching method such as CSR, SSR, SSG, and ISR.

Well explained, with great examples, demo apps, and diagrams. Exemplary content 👏
Thanks!
Introduction Creating a React component is fairly easy, just a function like this, and it’s done. export default function Card() { return <div>card</div>; } then you can call them using JSX like <Card />. However, to do them correctly is the reaso...
Solve problems such as colocation, and error-prone code

Introduction There are two kinds of types in Swift, which are Value and Reference Types. These types and their characteristics sometimes can be hard to remember and understand. Through this post, I'll try to explain it using a mental model and analog...

These are the mental models that I use to really understand flexbox, and I hope these can help you to understand too.

Spotify API grants us access to know what music is currently playing. Awesome addition to your dev portfolio!

When I started to learn Next.js, I got overwhelmed with the list of abbreviations that looks similar, I didn't know what it is and what is the difference. It is quite confusing because when using Create React App, we usually only use 1 strategy to fetch data from API which is using useEffect.
Next.js has many data fetching strategies. Although initially Next.js was well known to be a Server-Side Rendering Framework, it turns out that Next.js has 4 methods of Data Fetching. Here is the short explanation each so you get familiar with the abbreviation of CSR, SSR, SSG, ISR.
useEffect, it will fetch the data from the API every single page request on the client-side (after the page is rendered, then the function will run).Don't worry if you didn't get that, because I will be explaining it thoroughly, just familiarize the words first.
I mentioned before that there is a special function that will run when using a specific data fetching method. Keep that in mind as I will show you what is that special function.
This code example will fetch a date-time from an API using axios, then render it on the page. It is useful to see the date-time so we can truly know when the API is hit.
Special Function: useEffect
export default function CSRPage() {
const [dateTime, setDateTime] = React.useState<string>();
React.useEffect(() => {
axios
.get('https://worldtimeapi.org/api/ip')
.then((res) => {
setDateTime(res.data.datetime);
})
.catch((error) => console.error(error));
}, []);
return (
<main>
<TimeSection dateTime={dateTime} />
</main>
);
}
Terms:
Video Description:
Special Function: getServerSideProps
export default function SSRPage({ dateTime }: SSRPageProps) {
return (
<main>
<TimeSection dateTime={dateTime} />
</main>
);
}
export const getServerSideProps: GetServerSideProps = async () => {
const res = await axios.get('https://worldtimeapi.org/api/ip');
return {
props: { dateTime: res.data.datetime },
};
};
Video Description:
Here is the difference between CSR vs SSR, keep an eye on delay and loading indicators.
Video Description:
I will probably create a new post about the pros and cons of each method, but when using CSR the SEO is not really great because the data is only fetched after the page renders. This is useful and convenient when we are creating a page with a gated authentication, as you don't really need SEO for pages like the dashboard, edit profile page, etc.
But, for the SSR, although it creates a delay, data that was fetched is injected and helps SEO. This is quite useful for a thread or post that we need to get traffic into, like Reddit or some sort.
Special function: getStaticProps
export default function SSGPage({ dateTime }: SSGPageProps) {
return (
<main>
<TimeSection dateTime={dateTime} />
</main>
);
}
export const getStaticProps: GetStaticProps = async () => {
const res = await axios.get('https://worldtimeapi.org/api/ip');
return {
props: { dateTime: res.data.datetime },
};
};
Video Description:
yarn build, the API will be hit ONLY when the application is building. This is why the time is at 13:39, while the real-time is 16:17.Special function: getStaticProps + revalidate
export default function ISR20Page({ dateTime }: ISR20PageProps) {
return (
<main>
<TimeSection dateTime={dateTime} />
</main>
);
}
export const getStaticProps: GetStaticProps = async () => {
const res = await axios.get('https://worldtimeapi.org/api/ip');
return {
props: { dateTime: res.data.datetime },
revalidate: 20,
};
};
Disclaimer: Revalidate time is set to 20 seconds.
Video Description:
Now, this is might be confusing for you, but here is the key I want you to look at.
revalidate key.Terms:
Video Description:
Note:
Now, this is a case where we are assuming that only 1 person is accessing the website. But, that reloads will happen every person visit while still respecting the cooldown state.
Nope.
When the cooldown is off, if no one visits the page, then that page will not rebuild, even after long past the 20s.
But, the first person that visits when the cooldown state is off, is going to trigger a rebuild. That person won't be seeing changes. But, the changes will be served for the next full reload.
That's all, folks!
If you have understood this material, I suggest you to read more about How to choose between them. I provide 4 metrics for you to consider and some example!
Originally posted on my personal site, find more blog posts and code snippets library I put up for easy access on my site 🚀
Like this post? Subscribe to my newsletter to get notified every time a new post is out!