📖 Scenario: You are building a Next.js app that shows the current time on a page. You want to make sure the page always shows the latest time and does not use any cached version.
🎯 Goal: Create a Next.js server component page that displays the current time and opts out of caching so it updates on every request.
📋 What You'll Learn
Create a server component page file named page.tsx
Use the revalidate = 0 cache option to disable caching
Display the current time using new Date().toLocaleTimeString()
Export a default async function named Page that returns the JSX
💡 Why This Matters
🌍 Real World
Web apps often need to show live or frequently changing data, like stock prices or current time, where caching must be disabled to keep data fresh.
💼 Career
Understanding how to control caching in Next.js is important for building performant and accurate web applications that meet user expectations.
Progress0 / 4 steps
1
Create the page component file
Create a file named page.tsx and write an async function named Page that returns a simple JSX element with the text Current time: inside a <div>.
NextJS
Hint
Start by creating the basic page component that returns a div with the text 'Current time:'.
2
Add the revalidate = 0 cache option
Add the line export const revalidate = 0; at the top of page.tsx to disable caching and force the page to update on every request.
NextJS
Hint
Use export const revalidate = 0; to opt out of caching in Next.js.
3
Display the current time dynamically
Inside the Page function, create a variable named currentTime and set it to new Date().toLocaleTimeString(). Then update the JSX to display Current time: {currentTime} inside the <div>.
NextJS
Hint
Use JavaScript's new Date().toLocaleTimeString() to get the current time and show it in the JSX.
4
Complete the page with no caching
Ensure the file page.tsx exports revalidate = 0 and the default async function Page that returns the current time inside a <div>. This setup disables caching and shows the live time on every request.
NextJS
Hint
Double-check that the revalidate export and the Page function are correctly defined to disable caching.
Practice
(1/5)
1. In Next.js, how do you disable caching for a page to always fetch fresh content?
easy
A. Set cache: false in next.config.js
B. Use getStaticProps without any options
C. Add no-cache header in _app.js
D. Set export const revalidate = 0; in the page file
Solution
Step 1: Understand Next.js caching control
Next.js uses the revalidate export to control caching duration for static pages.
Step 2: Disable caching by setting revalidate to zero
Setting revalidate = 0 tells Next.js to never cache and always fetch fresh data.
Final Answer:
Set export const revalidate = 0; in the page file -> Option D
Quick Check:
revalidate = 0 disables caching [OK]
Hint: Remember: revalidate = 0 means no caching [OK]
Common Mistakes:
Confusing revalidate with cache settings in config
Trying to disable caching via headers only
Omitting the export keyword for revalidate
2. Which of the following is the correct syntax to disable caching in a Next.js page?
easy
A. export const revalidate = '0';
B. export const revalidate = 0;
C. const revalidate = 0;
D. export default revalidate = 0;
Solution
Step 1: Check correct export syntax
In Next.js, caching control requires exporting a constant named revalidate.
Step 2: Use number zero without quotes
The value must be a number 0, not a string, and must be exported properly.
Final Answer:
export const revalidate = 0; -> Option B
Quick Check:
Correct export with number zero disables caching [OK]
Hint: Use number 0 without quotes and export it [OK]
Common Mistakes:
Using string '0' instead of number 0
Not exporting the variable
Using default export incorrectly
3. Given this Next.js page code snippet, what will be the caching behavior?
The variable revalidate must be exported to affect caching.
Step 2: Identify missing export
Without export, Next.js ignores the variable and uses default caching.
Final Answer:
Missing export keyword for revalidate -> Option A
Quick Check:
revalidate must be exported to disable caching [OK]
Hint: Always export revalidate to control caching [OK]
Common Mistakes:
Forgetting to export revalidate
Thinking revalidate must be non-zero
Confusing functional and class components
5. You want a Next.js page to always show fresh data but only disable caching for authenticated users. Which approach correctly opts out of caching only for them?
hard
A. Set revalidate = 0 inside getStaticProps conditionally
B. Set export const revalidate = 0; unconditionally in the page file
C. Use server-side rendering with export const dynamic = 'force-dynamic'; and check auth inside
D. Add Cache-Control: no-store header in _app.js for all requests
Solution
Step 1: Understand conditional caching needs
Disabling caching only for authenticated users requires dynamic rendering based on request context.
Set export const revalidate = 0; unconditionally in the page file disables caching for all users; Set revalidate = 0 inside getStaticProps conditionally is invalid because revalidate is static; Add Cache-Control: no-store header in _app.js for all requests affects all requests globally.
Final Answer:
Use server-side rendering with export const dynamic = 'force-dynamic'; and check auth inside -> Option C