📖 Scenario: You are building a simple Next.js app that fetches user data from an API. To make the app faster and reduce repeated network calls, you will use caching techniques.
🎯 Goal: Create a Next.js component that fetches user data and caches it to avoid repeated API calls, demonstrating why caching is central to Next.js performance.
📋 What You'll Learn
Create a data fetching function that returns a fixed user object
Add a cache variable to store the fetched data
Use the cache to return data if available instead of fetching again
Render the user data in a React component
💡 Why This Matters
🌍 Real World
Caching user data in Next.js apps helps reduce slow network calls and improves user experience by showing data faster.
💼 Career
Understanding caching is important for Next.js developers to build fast, scalable web apps that handle data efficiently.
Progress0 / 4 steps
1
Create a data fetching function
Create an async function called fetchUserData that returns an object with name set to "Alice" and age set to 30.
NextJS
Hint
Use an async function that returns a fixed object with name and age.
2
Add a cache variable
Add a variable called userCache and set it to null to hold cached user data.
NextJS
Hint
Use let userCache = null; to create a cache variable.
3
Use cache in data fetching
Modify fetchUserData to first check if userCache is not null. If so, return userCache. Otherwise, fetch the data, store it in userCache, and then return it.
NextJS
Hint
Check if userCache has data before fetching. Save fetched data to userCache.
4
Render cached data in a Next.js component
Create a React functional component called UserProfile that uses useEffect and useState to call fetchUserData once and display the user's name and age inside a <div>.
NextJS
Hint
Use useState to hold user data and useEffect to fetch once on mount. Render user name and age inside paragraphs.
Practice
(1/5)
1. Why is caching important in Next.js for website performance?
easy
A. It stores parts of the website to load pages faster.
B. It deletes all data after every page load.
C. It slows down the website to save energy.
D. It disables JavaScript on the page.
Solution
Step 1: Understand caching purpose
Caching saves website parts so they don't need to be rebuilt every time.
Step 2: Connect caching to Next.js performance
Next.js uses caching to serve pages quickly by reusing saved content.
Final Answer:
It stores parts of the website to load pages faster. -> Option A
Quick Check:
Caching = Faster page loads [OK]
Hint: Caching means saving parts to speed up loading [OK]
Common Mistakes:
Thinking caching deletes data after load
Believing caching slows down the site
Confusing caching with disabling JavaScript
2. Which Next.js feature controls how often cached pages update?
easy
A. revalidate
B. getStaticProps
C. useEffect
D. getServerSideProps
Solution
Step 1: Identify caching update control
Next.js uses the revalidate property to set cache refresh timing.
Step 2: Differentiate from other features
getStaticProps fetches data at build time, useEffect runs on client, getServerSideProps fetches on every request.
Final Answer:
revalidate -> Option A
Quick Check:
Cache update timing = revalidate [OK]
Hint: Look for 'revalidate' to set cache refresh time [OK]
Common Mistakes:
Confusing getStaticProps with cache timing
Using useEffect for server caching
Thinking getServerSideProps controls caching
3. What will happen if you set revalidate: 10 in getStaticProps?
medium
A. The page updates on every request.
B. The page updates every 10 seconds after first load.
C. The page never updates after build.
D. The page updates every 10 minutes.
Solution
Step 1: Understand revalidate value meaning
Setting revalidate: 10 means Next.js will refresh the cached page every 10 seconds.
Step 2: Compare with other options
It does not update on every request, never after build, nor every 10 minutes.
Final Answer:
The page updates every 10 seconds after first load. -> Option B
Quick Check:
revalidate: 10 means 10 seconds refresh [OK]
Hint: revalidate number = seconds between cache refresh [OK]
Common Mistakes:
Thinking revalidate is minutes, not seconds
Believing page never updates with revalidate
Confusing revalidate with server-side rendering
4. You set revalidate: '10' (string) instead of 10 (number). What happens?
medium
A. Next.js updates the page every 10 minutes.
B. Next.js treats it as 10 seconds anyway.
C. Next.js ignores revalidate and never updates the page.
D. Next.js throws a type error and fails to build.
Solution
Step 1: Check revalidate type requirement
Next.js expects revalidate as a number, not a string.
Step 2: Understand error on wrong type
Passing a string causes a type error and build failure.
Final Answer:
Next.js throws a type error and fails to build. -> Option D
Quick Check:
revalidate must be number, string causes error [OK]
Hint: Use number type for revalidate, not string [OK]
Common Mistakes:
Passing string instead of number for revalidate
Assuming Next.js auto-converts string to number
Ignoring build errors caused by wrong types
5. You want a page to show fresh data but still load fast. Which caching strategy fits best in Next.js?
hard
A. Disable caching completely.
B. Use getServerSideProps to fetch data on every request.
C. Set revalidate to a low number like 5 seconds.
D. Set revalidate to a very high number like 86400 seconds.
Solution
Step 1: Balance freshness and speed
Low revalidate means data updates often but still uses cache for speed.
Step 2: Compare other options
getServerSideProps fetches every time but is slower; very high revalidate delays updates; disabling cache hurts speed.
Final Answer:
Set revalidate to a low number like 5 seconds. -> Option C
Quick Check:
Low revalidate = fresh + fast [OK]
Hint: Low revalidate balances fresh data and speed [OK]
Common Mistakes:
Choosing getServerSideProps for speed
Setting revalidate too high losing freshness
Disabling cache hurting performance
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.