Bird
Raised Fist0
NextJSframework~10 mins

Data cache behavior in NextJS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Data cache behavior
Request for Data
↓
Check Cache
↓
Return Cached
↓
Store in Cache
↓
Return Data
When Next.js requests data, it first checks the cache. If data is found (cache hit), it returns it immediately. If not (cache miss), it fetches from the server, stores it in cache, then returns it.
Execution Sample
NextJS
async function getData() {
  if (cache.has('key')) {
    return cache.get('key');
  }
  const data = await fetchDataFromAPI();
  cache.set('key', data);
  return data;
}
This function checks a cache for data by key, fetches from API if missing, stores it, then returns the data.
Execution Table
StepActionCache StateData ReturnedNotes
1Check if cache has 'key'{}NoneCache is empty, miss occurs
2Fetch data from API{}NoneData is being fetched asynchronously
3Store data in cache with 'key'{"key": "data"}NoneCache updated with new data
4Return data{"key": "data"}"data"Data returned to caller
5Next call: Check cache for 'key'{"key": "data"}"data"Cache hit, data returned immediately
💡 Execution stops after data is returned from cache or API fetch completes
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5
cache{}{}{"key": "data"}{"key": "data"}
dataundefinedundefined"data""data"
Key Moments - 2 Insights
Why does the function fetch data only once even if called multiple times?
Because after the first fetch, the data is stored in the cache (see Step 3 in execution_table). Subsequent calls find the data in cache (Step 5) and return it immediately without fetching again.
What happens if the cache is empty when getData is called?
The function detects a cache miss (Step 1), fetches data from the API (Step 2), stores it in cache (Step 3), then returns it (Step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the cache state after Step 3?
A{"key": "data"}
B{}
Cundefined
D{"key": undefined}
💡 Hint
Check the 'Cache State' column for Step 3 in the execution_table
At which step does the function return data for the first time?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Data Returned' column to see when data is first returned
If the cache already contains data, which step is skipped?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Refer to the 'Action' column to see when data fetching is bypassed
Concept Snapshot
Data cache behavior in Next.js:
- Check cache before fetching
- If cache hit, return cached data immediately
- If cache miss, fetch from server
- Store fetched data in cache
- Subsequent calls use cached data
This improves speed and reduces server load.
Full Transcript
In Next.js, when data is requested, the system first checks if the data is already stored in a cache. If it is, this is called a cache hit, and the cached data is returned immediately, making the response fast. If the data is not in the cache, called a cache miss, Next.js fetches the data from the server or API. After fetching, it stores the data in the cache for future requests. This way, the next time the same data is needed, it can be served quickly from the cache without fetching again. This process helps improve performance and reduce unnecessary server calls.

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