Bird
Raised Fist0
NextJSframework~8 mins

Data cache behavior in NextJS - Performance & Optimization

Choose your learning style10 modes available
Performance: Data cache behavior
MEDIUM IMPACT
This affects page load speed and interaction responsiveness by controlling how data is reused or refetched in Next.js apps.
Fetching data on client-side navigation
NextJS
import useSWR from 'swr'; const fetcher = url => fetch(url).then(res => res.json()); const { data } = useSWR('/api/data', fetcher);
Caches data in memory and reuses it on navigation, reducing network calls and speeding up rendering.
📈 Performance Gainsingle network request per data change, faster UI updates, reduces blocking by 50-80%
Fetching data on client-side navigation
NextJS
useEffect(() => { fetch('/api/data').then(res => res.json()).then(setData); }, []);
Fetches data on every client navigation without caching, causing repeated network requests and slower UI updates.
📉 Performance Costblocks rendering for 100-300ms per navigation, triggers multiple network requests
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
No cache, fetch on every navigationMinimal0High due to delayed paint[X] Bad
Client-side cache with SWR or React QueryMinimal0Low, immediate paint with cached data[OK] Good
Rendering Pipeline
Data cache behavior affects the critical rendering path by reducing the need for network fetches before painting content. Cached data allows the browser to skip waiting for server responses, speeding up style calculation and layout.
→Network
→Style Calculation
→Layout
→Paint
⚠️ BottleneckNetwork latency and blocking data fetches before paint
Core Web Vital Affected
LCP, INP
This affects page load speed and interaction responsiveness by controlling how data is reused or refetched in Next.js apps.
Optimization Tips
1Cache data to avoid repeated network requests on navigation.
2Use libraries like SWR or React Query for automatic caching and revalidation.
3Monitor network requests in DevTools to ensure caching reduces fetches.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of caching data in a Next.js app?
AReduces repeated network requests and speeds up page rendering
BIncreases bundle size significantly
CTriggers more reflows during rendering
DBlocks rendering until all data is refetched
DevTools: Network
How to check: Open DevTools, go to Network tab, navigate pages and observe repeated API calls for data endpoints.
What to look for: Multiple identical fetch requests indicate no caching; fewer requests with fast responses indicate effective caching.

Practice

(1/5)
1. In Next.js, what is the main benefit of using data caching?
easy
A. It increases the size of the app bundle
B. It forces the app to fetch data on every user action
C. It disables server-side rendering
D. It makes the app faster by reusing previously fetched data

Solution

  1. Step 1: Understand what caching does

    Caching stores data so it can be reused without fetching again.
  2. Step 2: Identify the benefit in Next.js context

    Reusing data avoids repeated fetches, making the app faster.
  3. Final Answer:

    It makes the app faster by reusing previously fetched data -> Option D
  4. Quick Check:

    Data caching = faster app [OK]
Hint: Caching saves time by reusing data, not refetching [OK]
Common Mistakes:
  • Thinking caching increases app size
  • Confusing caching with disabling server rendering
  • Assuming caching forces more data fetches
2. Which Next.js function is used to fetch data at build time and cache it for static pages?
easy
A. getServerSideProps
B. getInitialProps
C. getStaticProps
D. useEffect

Solution

  1. Step 1: Recall Next.js data fetching methods

    getStaticProps fetches data at build time for static generation.
  2. Step 2: Match function to caching behavior

    getStaticProps caches data during build, unlike getServerSideProps which runs on each request.
  3. Final Answer:

    getStaticProps -> Option C
  4. Quick Check:

    Build time data fetch = getStaticProps [OK]
Hint: Static props run once at build, caching data [OK]
Common Mistakes:
  • Confusing getServerSideProps with build time fetching
  • Using useEffect which runs on client side
  • Thinking getInitialProps is for static caching
3. Consider this Next.js code snippet using getStaticProps with revalidate: 10. What happens when a user visits the page after 15 seconds since last build?
medium
A. The page triggers a background rebuild and shows cached data immediately
B. The page shows stale cached data and does not update
C. The page blocks rendering until new data is fetched
D. The page fetches data on the client side only

Solution

  1. Step 1: Understand revalidate option in getStaticProps

    revalidate sets seconds after which Next.js regenerates the page in background.
  2. Step 2: Analyze behavior after 15 seconds (greater than 10)

    Next.js serves cached page immediately and triggers background rebuild for next visitor.
  3. Final Answer:

    The page triggers a background rebuild and shows cached data immediately -> Option A
  4. Quick Check:

    revalidate triggers background rebuild = The page triggers a background rebuild and shows cached data immediately [OK]
Hint: Revalidate serves cached page, rebuilds in background [OK]
Common Mistakes:
  • Thinking page blocks rendering until rebuild
  • Assuming stale data is never updated
  • Confusing client-side fetch with static regeneration
4. You notice your Next.js page using getServerSideProps is always slow. Which caching mistake might cause this?
medium
A. Not caching data on the server or client side, causing fetch on every request
B. Setting revalidate to 0 in getStaticProps
C. Using getStaticProps instead of getServerSideProps
D. Using client-side fetching inside getServerSideProps

Solution

  1. Step 1: Identify caching behavior of getServerSideProps

    getServerSideProps runs on every request, so no built-in caching unless implemented.
  2. Step 2: Recognize impact of missing caching

    Without caching, data fetch happens every time, causing slowness.
  3. Final Answer:

    Not caching data on the server or client side, causing fetch on every request -> Option A
  4. Quick Check:

    No caching with getServerSideProps = slow page [OK]
Hint: No caching on server fetch causes slow responses [OK]
Common Mistakes:
  • Confusing getStaticProps with getServerSideProps
  • Thinking revalidate affects getServerSideProps
  • Trying client fetch inside server function
5. You want to cache user profile data that updates frequently but must show fresh info within 5 seconds. Which Next.js caching strategy fits best?
hard
A. Use getStaticProps with revalidate: 60 to regenerate every minute
B. Use getServerSideProps with server-side caching that expires after 5 seconds
C. Fetch data only on client side with no caching
D. Use getStaticProps without revalidate for static caching

Solution

  1. Step 1: Analyze data freshness requirement

    Data must update within 5 seconds, so caching must expire quickly.
  2. Step 2: Match caching strategy to freshness need

    getServerSideProps with server cache expiring after 5 seconds allows fresh data and caching.
  3. Step 3: Eliminate other options

    getStaticProps with 60s revalidate is too slow; client-only fetch lacks caching; static without revalidate never updates.
  4. Final Answer:

    Use getServerSideProps with server-side caching that expires after 5 seconds -> Option B
  5. Quick Check:

    Fast update needs server cache with short expiry [OK]
Hint: Fast-changing data needs server cache with short expiry [OK]
Common Mistakes:
  • Choosing static props with long revalidate time
  • Ignoring caching and fetching only on client
  • Using static props without revalidate for fresh data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard