Bird
Raised Fist0
NextJSframework~20 mins

Opting out of caching in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Next.js Caching Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when opting out of caching in a Next.js Server Component?

Consider a Next.js Server Component that fetches data but opts out of caching using export const revalidate = 0;. What happens when you refresh the page multiple times?

NextJS
export const revalidate = 0;

export default async function Page() {
  const time = new Date().toISOString();
  return <p>Current time: {time}</p>;
}
AThe time updates on every refresh because caching is disabled.
BThe time stays the same on every refresh because data is cached indefinitely.
CThe page throws an error because revalidate cannot be zero.
DThe time updates only once per minute due to default caching.
Attempts:
2 left
💡 Hint

Think about what setting revalidate = 0 means for data fetching in Next.js.

📝 Syntax
intermediate
1:30remaining
Which code snippet correctly opts out of caching in Next.js 14+?

Choose the correct way to disable caching for a Next.js Server Component.

Aexport const cache = false;
Bexport const noCache = true;
Cexport const revalidate = false;
Dexport const revalidate = 0;
Attempts:
2 left
💡 Hint

Look for the official Next.js export that controls cache revalidation timing.

🔧 Debug
advanced
2:30remaining
Why does this Next.js page still cache data despite setting revalidate to 0?

Review the code below. The developer expects no caching, but the page shows stale data on refresh. What is the likely cause?

NextJS
export const revalidate = 0;

export default async function Page() {
  const data = await fetch('https://api.example.com/data', { cache: 'force-cache' }).then(res => res.json());
  return <p>{data.message}</p>;
}
AThe fetch uses <code>cache: 'force-cache'</code> which overrides revalidate and caches the response.
BThe revalidate export must be a function, not a number.
CThe API endpoint is down, causing stale data to appear.
DNext.js ignores revalidate when fetch is used inside Server Components.
Attempts:
2 left
💡 Hint

Check the fetch options and how they interact with Next.js caching.

🧠 Conceptual
advanced
2:00remaining
What is the effect of export const dynamic = 'force-dynamic' in Next.js?

In Next.js 14+, what does setting export const dynamic = 'force-dynamic' do for a page or layout?

AIt caches the page for 10 seconds before revalidating.
BIt forces the page to be rendered dynamically on every request, disabling static caching.
CIt disables the page from rendering on the server and forces client-side rendering.
DIt forces the page to be statically generated at build time.
Attempts:
2 left
💡 Hint

Consider how dynamic rendering differs from static generation.

❓ state_output
expert
3:00remaining
What is the rendered output of this Next.js Server Component with caching disabled and a client component inside?

Analyze the code below. What will the user see when the page loads and after clicking the button?

NextJS
export const revalidate = 0;

'use client';
import { useState } from 'react';

function ClientCounter() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}

export default function Page() {
  const time = new Date().toISOString();
  return (
    <>
      <p>Server time: {time}</p>
      <ClientCounter />
    </>
  );
}
AThe server time never updates on refresh, and the button does not increment the count.
BThe page shows an error because client components cannot be used inside server components with revalidate set to 0.
CThe page shows the current server time that updates on every refresh, and the button increments count on clicks without refreshing the time.
DThe button increments count but the server time also updates on every click.
Attempts:
2 left
💡 Hint

Think about how server and client components behave together and how caching affects server-rendered data.

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

  1. Step 1: Understand Next.js caching control

    Next.js uses the revalidate export to control caching duration for static pages.
  2. Step 2: Disable caching by setting revalidate to zero

    Setting revalidate = 0 tells Next.js to never cache and always fetch fresh data.
  3. Final Answer:

    Set export const revalidate = 0; in the page file -> Option D
  4. 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

  1. Step 1: Check correct export syntax

    In Next.js, caching control requires exporting a constant named revalidate.
  2. Step 2: Use number zero without quotes

    The value must be a number 0, not a string, and must be exported properly.
  3. Final Answer:

    export const revalidate = 0; -> Option B
  4. 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?
export const revalidate = 0;

export default function Page() {
  return <h1>Fresh Content</h1>;
}
medium
A. The page is never cached and always fetches fresh content
B. The page is cached and refreshed every 10 seconds
C. The page is cached indefinitely
D. The page throws a runtime error due to invalid revalidate value

Solution

  1. Step 1: Identify the revalidate value

    The code sets revalidate = 0, which disables caching.
  2. Step 2: Understand effect on page behavior

    With caching disabled, the page always fetches fresh content on each request.
  3. Final Answer:

    The page is never cached and always fetches fresh content -> Option A
  4. Quick Check:

    revalidate = 0 means no caching [OK]
Hint: revalidate 0 means always fresh, no cache [OK]
Common Mistakes:
  • Assuming default caching applies
  • Thinking 0 means cache forever
  • Confusing with ISR default timing
4. You wrote this code to disable caching but the page still caches:
const revalidate = 0;

export default function Page() {
  return <div>No Cache</div>;
}

What is the error?
medium
A. Missing export keyword for revalidate
B. revalidate cannot be zero
C. Page component must be a class
D. Return statement syntax is incorrect

Solution

  1. Step 1: Check revalidate declaration

    The variable revalidate must be exported to affect caching.
  2. Step 2: Identify missing export

    Without export, Next.js ignores the variable and uses default caching.
  3. Final Answer:

    Missing export keyword for revalidate -> Option A
  4. 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

  1. Step 1: Understand conditional caching needs

    Disabling caching only for authenticated users requires dynamic rendering based on request context.
  2. Step 2: Use dynamic rendering with auth check

    Setting export const dynamic = 'force-dynamic'; forces server-side rendering, allowing auth checks per request.
  3. Step 3: Why other options fail

    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.
  4. Final Answer:

    Use server-side rendering with export const dynamic = 'force-dynamic'; and check auth inside -> Option C
  5. Quick Check:

    Dynamic rendering enables conditional caching [OK]
Hint: Use dynamic rendering to control caching per user [OK]
Common Mistakes:
  • Trying to set revalidate conditionally inside getStaticProps
  • Disabling caching globally instead of per user
  • Misusing cache headers in _app.js
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