Web Development

Data Fetching in Next.js: A Developer's Guide

May 11, 2025
4 min read
Shivansh Sharma

Data Fetching in Next.js: A Developer's Guide

Data fetching in next js is the fundamental key to build dynamic and powerful web apps. In Next JS, by understanding the concept of data fetching, you can actually enhance your app’s performance and user- experience!!

Data Fetching in Next.js

Understanding Data Fetching in Next.js

Next js offers a wide variety of strategies for fetching the data, each for different types of scenarios:

  • Static Generation (SSG): It pre-renders pages at build time, this makes it ideal for content that doesn't change frequently.

  • Server-Side Rendering (SSR): Renders pages on each request, this is useful for dynamic content that changes often.

  • Client-Side Rendering (CSR): Fetches data on the client side after the page is loaded. Suitable for user-specific data.

With the entry of the App Router and React Server Components, Next js has further improved data fetching, allowing you to make more efficient and scalable applications!!

The Role of React Server Components

React server components (RSC) are a game-changer for sure!! They enable you to fetch data on your server seamlessly:

  • Direct Database Access: You can query your database directly within server components without exposing sensitive information to the client!

  • Simplified Code: Use async/await syntax without the need for useEffect or useState.

  • Performance Boost: Server components reduce the amount of JavaScript sent to the client, improving your website’s load times. Cool, right??

Example:

// app/page.tsx
export default async function Page() {
  const data = await fetchDataFromDatabase();
  return <div>{data.title}</div>;
}

In this example, fetchDataFromDatabase is a server-side function that fetch data directly from your database.

Client-Side Data Fetching

Server-side fetching is powerful but there are some cases where client-side data fetching will be super useful:

  • User-Specific Data: Personalized dashboards or user profiles.

  • Real-Time Updates: Chat applications or live feeds.

Example using useEffect:

import { useEffect, useState } from 'react';

function UserProfile() {
  const [user, setUser] = useState(null);

  useEffect(() => {
    fetch('/api/user')
      .then((res) => res.json())
      .then((data) => setUser(data));
  }, []);

  if (!user) return <div>Loading...</div>;
  return <div>{user.name}</div>;
}

For more advanced projects, consider using libraries like SWR or React Query, which offer features like caching, revalidation, and much more!!

Caching and Revalidation Strategies

Next js also extends its native fetch API to support caching and revalidation!!

  • cache: 'force-cache': Cache the response indefinitely.

  • cache: 'no-store': Disable caching.

  • next.revalidate: Set a revalidation time in seconds.

Example:

const res = await fetch('https://api.example.com/data', {
  next: { revalidate: 60 }, // Revalidate every 60 seconds
});

This approach ensures you have fresh data without unnecessary re-fetching.

Building a Dashboard (Practical Example)

Let's apply these concepts to build a simple dashboard:

  • Create a server component to fetch data:
// app/dashboard/page.tsx
import { fetchDashboardData } from '@/lib/data';

export default async function Dashboard() {
  const data = await fetchDashboardData();
  return (
    <div>
      <h1>Dashboard</h1>
      <p>Total Users: {data.totalUsers}</p>
    </div>
  );
}
  • Define the data fetching function:
// lib/data.ts
export async function fetchDashboardData() {
  const res = await fetch('https://api.example.com/dashboard', {
    next: { revalidate: 300 }, // Revalidate every 5 minutes
  });
  return res.json();
}

This setup ensures that your dashboard only displays up-to-date information with minimal performance stress!

Best Practices To Boost Your Productivity

Data Fetching in Next.js

  • Use Server Components for Static or Public Data: Leverage server components to fetch data that doesn't change per user.

  • Use Client Components for Interactive Features: Use client components when you need interactivity or access to browser APIs.

  • Optimize Your API Calls: Avoid unnecessary API calls by caching responses and using revalidation strategies.

  • Error Handling: Implement proper error handling to manage failed data fetches gracefully.


In Conclusion

Data fetching in next js, especially with the app router and react server components, offer a more flexible approach to build dynamic web apps. By understanding when and how to fetch data, you can create flexible, scalable, and user-friendly applications!!

Don’t forget! Your biggest responsibility is to choose the right data fetching strategy as per your application needs. Happy Coding!!

Tags

nextjsreactdata fetching

Shivansh Sharma

A passionate web dev.