JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsnextjs
PrevNext

Learn the concept

Data Fetching

nextjs
mid
data-fetching

How does data fetching work in Next.js?

nextjs
data-fetching
ssr
ssg
isr
swr
caching
Quick Answer

In the App Router, data is fetched directly in Server Components using `async/await` with opt-in caching via \"use cache\". The Pages Router uses special functions like `getStaticProps` (build time) and `getServerSideProps` (request time).

Detailed Explanation

App Router Data Fetching:

Server Components can be async and fetch data directly:

TSX
async function Page() {
  const data = await fetch('...');
}

Caching in Next.js 16 (Opt-In Model):

  • fetch() is NOT cached by default — caching depends on rendering context
  • Use "use cache" directive for explicit caching
  • cacheLife() for time-based revalidation profiles
  • cacheTag() for on-demand revalidation with revalidateTag()

Legacy Caching Options:

  • { cache: 'no-store' } - No caching (SSR)
  • { next: { revalidate: 60 } } - ISR (revalidate after 60s)

Pages Router Data Fetching:

  • getStaticProps - Build time (SSG)
  • getStaticPaths - Dynamic routes for SSG
  • getServerSideProps - Request time (SSR)

Client-Side Fetching:

  • Use SWR or React Query for client components
  • useEffect + useState (manual approach)

Parallel vs Sequential:

  • Fetch calls in sequence by default
  • Use Promise.all for parallel fetches

Code Examples

App Router - Server Component fetchingTSX
// app/posts/page.tsx - Server Component

// NOT cached by default in Next.js 16 — use \"use cache\" for caching
async function getPosts() {
  const res = await fetch('https://api.example.com/posts');
  if (!res.ok) throw new Error('Failed to fetch posts');
  return res.json();
}

export default async function PostsPage() {
  const posts = await getPosts();
  
  return (
    <ul>
      {posts.map((post: any) => (
        <li key={post.id}>{post.title}</li>
      ))}
    </ul>
  );
}

// With revalidation (ISR)
async function getPostsISR() {
  const res = await fetch('https://api.example.com/posts', {
    next: { revalidate: 3600 }, // Revalidate every hour
  });
  return res.json();
}

// Force dynamic (SSR)
async function getPostsSSR() {
  const res = await fetch('https://api.example.com/posts', {
    cache: 'no-store', // Always fetch fresh
  });
  return res.json();
}

Real-World Applications

Use Cases

Content Management System

Fetching CMS content in Server Components with on-demand revalidation when editors publish changes

Real-Time Dashboard

Combining server-side data fetching for initial load with SWR for client-side real-time updates

API Aggregation Layer

Using parallel fetches in Server Components to aggregate data from multiple microservices into a single page

Mini Projects

Data Fetching Patterns Demo

intermediate

Build a page demonstrating sequential vs parallel fetching, caching with use cache, and client-side SWR polling

CMS-Powered Blog

intermediate

Build a blog that fetches from a headless CMS with use cache and cacheTag for on-demand revalidation on publish

Industry Examples

Sanity

Provides a Next.js toolkit for server-side CMS data fetching with real-time preview and on-demand revalidation

Supabase

Integrates with Next.js Server Components for direct database queries without API middleware

Resources

Data Fetching in App Router

docs

SWR - React Hooks for Data Fetching

docs

Related Questions

Explain the different rendering strategies in Next.js (SSR, SSG, ISR) and when to use each approach.

senior
rendering

How does caching and revalidation work in Next.js?

senior
caching
Previous
What are the key differences between Pages Router and App Router in Next.js?
Next
What's the difference between Server Components and Client Components in Next.js?
PrevNext