In this tutorial, you will learn Dynamic Data Fetching in Next.js
What is Dynamic Data Fetching?
It means Data is fetched at request time, and the page is rendered on the server for every incoming request.
How does it work?
1. Data is fetched on each request.
2. HTML is generated dynamically.
Common methods
1. getServerSideProps() (Pages Router)
2. fetch() with cache: ‘no-store’ (App Router)
3. Dynamic Server Components
Example:
async function getUser() {
const res = await fetch('https://api.example.com/user', {
cache: 'no-store'
});
return res.json();
}
export default async function Page() {
const user = await getUser();
return <h1>Welcome, {user.name}</h1>;
}
Advantages
There are many advantages.
1. It always has fresh data.
2. It is used for real-time updates.
Disadvantages
There are many disadvantages.
1. It is slower than static pages.
2. Higher server cost.
Dynamic Data Fetching in Next.js – Interview Questions
Q 1: What is dynamic data fetching?
Q 2: How do you enable dynamic fetching?
Q 3: When should dynamic fetching be used?
Q 4: Does dynamic fetching affect performance?
Q 5: Is dynamic data SEO-friendly?
Dynamic Data Fetching in Next.js – Objective Questions (MCQs)
Q1. Dynamic data fetching means data is fetched:
Q2. Which fetch option disables caching?
Q3. Dynamic data fetching is best for:
Q4. Which rendering type uses dynamic data fetching?
Q5. Dynamic data fetching ensures: