Data cache helps your app load faster by saving information so it doesn't have to fetch it again. It makes your app feel quick and smooth.
Data cache behavior in NextJS
export async function getStaticProps() { const data = await fetch('https://api.example.com/data').then(res => res.json()) return { props: { data }, revalidate: 10 // seconds } }
getStaticProps runs at build time and caches the data for fast loading.
The revalidate option tells Next.js to update the cache after the given seconds.
export async function getStaticProps() { const data = await fetch('https://api.example.com/posts').then(res => res.json()) return { props: { posts: data }, revalidate: 60 } }
export async function getServerSideProps() { const data = await fetch('https://api.example.com/user').then(res => res.json()) return { props: { user: data } } }
import useSWR from 'swr' function Profile() { const { data, error } = useSWR('/api/user', fetcher) if (error) return <div>Failed to load</div> if (!data) return <div>Loading...</div> return <div>Hello {data.name}</div> }
This React component uses Next.js with the SWR library to cache user data on the client side. It shows loading text while fetching, an error message if fetching fails, and the user info when ready.
import useSWR from 'swr' const fetcher = url => fetch(url).then(res => res.json()) export default function UserProfile() { const { data, error } = useSWR('/api/user', fetcher) if (error) return <p>Failed to load user data.</p> if (!data) return <p>Loading user data...</p> return <main> <h1>User Profile</h1> <p>Name: {data.name}</p> <p>Email: {data.email}</p> </main> }
Next.js supports different caching methods: static generation, server-side rendering, and client-side caching.
Use revalidate in getStaticProps to keep static pages updated.
Client-side caching with SWR or React Query helps keep data fresh without full page reloads.
Data caching makes your app faster by saving and reusing data.
Next.js offers caching at build time, on each request, and on the client side.
Choose caching based on how often your data changes and how fresh it needs to be.
Practice
Solution
Step 1: Understand what caching does
Caching stores data so it can be reused without fetching again.Step 2: Identify the benefit in Next.js context
Reusing data avoids repeated fetches, making the app faster.Final Answer:
It makes the app faster by reusing previously fetched data -> Option DQuick Check:
Data caching = faster app [OK]
- Thinking caching increases app size
- Confusing caching with disabling server rendering
- Assuming caching forces more data fetches
Solution
Step 1: Recall Next.js data fetching methods
getStaticProps fetches data at build time for static generation.Step 2: Match function to caching behavior
getStaticProps caches data during build, unlike getServerSideProps which runs on each request.Final Answer:
getStaticProps -> Option CQuick Check:
Build time data fetch = getStaticProps [OK]
- Confusing getServerSideProps with build time fetching
- Using useEffect which runs on client side
- Thinking getInitialProps is for static caching
getStaticProps with revalidate: 10. What happens when a user visits the page after 15 seconds since last build?Solution
Step 1: Understand revalidate option in getStaticProps
revalidate sets seconds after which Next.js regenerates the page in background.Step 2: Analyze behavior after 15 seconds (greater than 10)
Next.js serves cached page immediately and triggers background rebuild for next visitor.Final Answer:
The page triggers a background rebuild and shows cached data immediately -> Option AQuick Check:
revalidate triggers background rebuild = The page triggers a background rebuild and shows cached data immediately [OK]
- Thinking page blocks rendering until rebuild
- Assuming stale data is never updated
- Confusing client-side fetch with static regeneration
getServerSideProps is always slow. Which caching mistake might cause this?Solution
Step 1: Identify caching behavior of getServerSideProps
getServerSideProps runs on every request, so no built-in caching unless implemented.Step 2: Recognize impact of missing caching
Without caching, data fetch happens every time, causing slowness.Final Answer:
Not caching data on the server or client side, causing fetch on every request -> Option AQuick Check:
No caching with getServerSideProps = slow page [OK]
- Confusing getStaticProps with getServerSideProps
- Thinking revalidate affects getServerSideProps
- Trying client fetch inside server function
Solution
Step 1: Analyze data freshness requirement
Data must update within 5 seconds, so caching must expire quickly.Step 2: Match caching strategy to freshness need
getServerSideProps with server cache expiring after 5 seconds allows fresh data and caching.Step 3: Eliminate other options
getStaticProps with 60s revalidate is too slow; client-only fetch lacks caching; static without revalidate never updates.Final Answer:
Use getServerSideProps with server-side caching that expires after 5 seconds -> Option BQuick Check:
Fast update needs server cache with short expiry [OK]
- Choosing static props with long revalidate time
- Ignoring caching and fetching only on client
- Using static props without revalidate for fresh data
Ready to prove it?
Pass the test and this pattern moves unknown
