Data Fetching Basics
β± Estimated reading time: 2 min
Data fetching is a core feature of Next.js that allows you to load data from APIs, databases, or external services efficiently. Next.js provides different data-fetching methods depending on where and how the data is needed.
1. Data Fetching in the App Router
In the App Router (app/ directory), data fetching is handled primarily using the fetch() API.
By default, data is fetched on the server, making applications faster and SEO-friendly.
2. Server-Side Data Fetching
Basic Example
-
Runs on the server
-
Data is not exposed to the browser
-
Ideal for SEO and secure data
3. Static Data Fetching (SSG)
By default, fetch() uses static rendering.
-
Data is fetched at build time
-
Best for blogs, landing pages, and static content
-
Very fast performance
4. Dynamic Data Fetching
To fetch fresh data on every request, use:
-
Data is fetched on each request
-
Suitable for dashboards and real-time data
5. Revalidation (ISR)
You can revalidate data after a specific time.
-
Data refreshes every 60 seconds
-
Combines speed and freshness
6. Client-Side Data Fetching
For interactive or user-driven data, fetch data on the client.
7. When to Use Which Method
| Use Case | Method |
|---|---|
| SEO content | Server-side fetch |
| Static pages | Build-time fetch |
| Real-time data | Dynamic fetch |
| User interaction | Client-side fetch |
Conclusion
Next.js data fetching is flexible and optimized by default. By choosing the right fetching strategyβstatic, dynamic, or client-sideβyou can build fast, scalable, and SEO-friendly applications.
Register Now
Share this Post
β Back to Tutorials