0
0
NextJSframework~30 mins

Fetch caching behavior in NextJS - Mini Project: Build & Apply

Choose your learning style9 modes available
Fetch Caching Behavior in Next.js
📖 Scenario: You are building a Next.js app that fetches user data from an API. To improve performance, you want to control how the data is cached when fetching it.
🎯 Goal: Create a Next.js Server Component that fetches user data from https://jsonplaceholder.typicode.com/users with caching disabled, then add a cache duration, and finally enable revalidation to update the cache every 10 seconds.
📋 What You'll Learn
Create a Server Component named UsersList that fetches user data with cache: 'no-store' option.
Add a variable cacheDuration set to 60000 (milliseconds).
Modify the fetch to use next: { revalidate: 10 } option to enable cache revalidation every 10 seconds.
Export the UsersList component as default.
💡 Why This Matters
🌍 Real World
Controlling fetch caching in Next.js helps improve app performance and user experience by reducing unnecessary network requests and keeping data fresh.
💼 Career
Understanding fetch caching is important for Next.js developers to optimize data fetching strategies in real-world web applications.
Progress0 / 4 steps
1
Create the initial fetch with caching disabled
Create a Server Component named UsersList. Inside it, use await fetch to get data from 'https://jsonplaceholder.typicode.com/users' with the option { cache: 'no-store' }. Then parse the response as JSON into a variable called users. Return a <ul> element with each user's name inside <li>.
NextJS
Need a hint?

Use fetch with { cache: 'no-store' } to disable caching.

2
Add a cache duration variable
Above the UsersList component, create a constant variable named cacheDuration and set it to 60000 (milliseconds).
NextJS
Need a hint?

Define cacheDuration as a constant with value 60000.

3
Modify fetch to enable cache revalidation every 10 seconds
Inside the UsersList component, change the fetch options to use next: { revalidate: 10 } instead of { cache: 'no-store' }.
NextJS
Need a hint?

Use next: { revalidate: 10 } in fetch options to enable cache revalidation every 10 seconds.

4
Export the UsersList component as default
Ensure the UsersList component is exported as the default export at the bottom of the file.
NextJS
Need a hint?

Make sure the component is exported as default using export default.