Bird
Raised Fist0
NextJSframework~20 mins

Router cache on client in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Next.js Router Cache Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
How does Next.js cache pages on the client?

Consider a Next.js app using the App Router. When you navigate between pages, how does Next.js handle caching on the client side?

ANext.js caches the JavaScript and JSON data for pages in memory, enabling fast client-side transitions without full reloads.
BNext.js caches the entire HTML of each page in the browser's local storage for instant reloads.
CNext.js disables caching on the client to always fetch fresh data from the server on navigation.
DNext.js caches only CSS files on the client, but reloads page data every time.
Attempts:
2 left
💡 Hint

Think about how client-side navigation avoids full page reloads.

❓ state_output
intermediate
2:00remaining
What happens to client cache after a server action updates data?

In a Next.js app using server actions and the App Router, you update data on the server. What happens to the cached page data on the client after this update?

AThe client cache is cleared immediately and the page reloads automatically.
BThe client cache remains unchanged until the user manually refreshes the page.
CThe client cache is automatically invalidated and refetched on next navigation to show updated data.
DThe client cache duplicates the old and new data causing inconsistent UI.
Attempts:
2 left
💡 Hint

Think about how Next.js keeps UI data fresh after server changes.

📝 Syntax
advanced
2:00remaining
Identify the correct way to prefetch a route in Next.js App Router

Which code snippet correctly prefetches a route to warm the client cache before navigation?

NextJS
import { useRouter } from 'next/navigation';

export default function PrefetchExample() {
  const router = useRouter();

  function prefetchPage() {
    // Which line correctly prefetches '/dashboard'?
  }

  return <button onClick={prefetchPage}>Prefetch Dashboard</button>;
}
Arouter.prefetchPage('/dashboard');
Brouter.prefetch('/dashboard');
Crouter.load('/dashboard');
Drouter.cache('/dashboard');
Attempts:
2 left
💡 Hint

Check the official Next.js navigation API for prefetching.

🔧 Debug
advanced
2:00remaining
Why does client cache not update after navigation in this Next.js code?

Given this Next.js App Router code snippet, why does the client cache not update after navigating to '/profile'?

NextJS
import Link from 'next/link';

export default function Nav() {
  return <Link href="/profile" prefetch={false}>Go to Profile</Link>;
}
ABecause the <code>href</code> value is incorrect and causes navigation failure.
BBecause the <code>Link</code> component requires an explicit <code>cache</code> prop to update cache.
CBecause Next.js disables client cache by default for all links.
DBecause <code>prefetch</code> is set to false, Next.js does not cache the page data before navigation.
Attempts:
2 left
💡 Hint

Look at the prefetch prop and what it controls.

🧠 Conceptual
expert
3:00remaining
How does Next.js App Router handle cache consistency with server components and client navigation?

Explain how Next.js App Router ensures cache consistency between server components and client-side navigation when data changes frequently.

ANext.js uses a cache invalidation system that tracks data dependencies and refetches server components on navigation or mutations to keep client cache fresh.
BNext.js relies on browser cache headers only and does not manage cache consistency internally.
CNext.js duplicates server component data in local storage and syncs it periodically with the server.
DNext.js disables caching for server components and always fetches fresh data on every client navigation.
Attempts:
2 left
💡 Hint

Think about how Next.js manages data freshness with server components and client cache.

Practice

(1/5)
1. What is the main purpose of router cache on the client in Next.js?
easy
A. To disable client-side navigation completely
B. To speed up navigation by storing visited pages locally
C. To increase server load by fetching pages repeatedly
D. To prevent users from accessing certain pages

Solution

  1. Step 1: Understand router cache concept

    Router cache stores pages visited by the user on the client side to avoid reloading them from the server.
  2. Step 2: Identify the effect on navigation speed

    By storing pages locally, navigation becomes faster and smoother because the browser doesn't need to fetch the page again.
  3. Final Answer:

    To speed up navigation by storing visited pages locally -> Option B
  4. Quick Check:

    Router cache = faster navigation [OK]
Hint: Router cache means storing pages locally for faster navigation [OK]
Common Mistakes:
  • Thinking router cache increases server load
  • Confusing cache with disabling navigation
  • Assuming cache blocks page access
2. Which Next.js router method is used to prefetch a page and cache it on the client?
easy
A. router.prefetch()
B. router.load()
C. router.cache()
D. router.fetch()

Solution

  1. Step 1: Recall Next.js router methods

    Next.js provides a method called router.prefetch() to load pages in advance.
  2. Step 2: Match method to caching behavior

    router.prefetch() loads the page and stores it in the client cache for faster navigation.
  3. Final Answer:

    router.prefetch() -> Option A
  4. Quick Check:

    Prefetch method caches pages [OK]
Hint: Prefetch means load ahead to cache page [OK]
Common Mistakes:
  • Using non-existent methods like router.load()
  • Confusing fetch with prefetch
  • Assuming cache() is a router method
3. Given this code snippet in a Next.js component:
import { useRouter } from 'next/router';

export default function Home() {
  const router = useRouter();
  router.prefetch('/about');
  return <button onClick={() => router.push('/about')>Go to About</button>;
}

What happens when the user clicks the button?
medium
A. An error occurs because prefetch cannot be called inside the component
B. The /about page reloads from the server every time
C. The button does nothing because prefetch disables navigation
D. The /about page loads instantly from client cache

Solution

  1. Step 1: Understand router.prefetch usage

    The code calls router.prefetch('/about') inside the component, which loads and caches the /about page on the client.
  2. Step 2: Analyze button click behavior

    When clicking the button, router.push('/about') navigates to the cached page, making the load instant without server fetch.
  3. Final Answer:

    The /about page loads instantly from client cache -> Option D
  4. Quick Check:

    Prefetch + push = instant load [OK]
Hint: Prefetch before push means instant page load [OK]
Common Mistakes:
  • Thinking prefetch disables navigation
  • Assuming page reloads from server always
  • Believing prefetch causes errors inside components
4. Identify the error in this Next.js code that tries to prefetch a page:
import { useRouter } from 'next/router';

export default function Page() {
  const router = useRouter();

  function handleClick() {
    router.prefetch('/contact')
  }

  return <button onClick={handleClick}>Load Contact</button>;
}
medium
A. router.prefetch must be called outside event handlers
B. Missing semicolon after router.prefetch call causes syntax error
C. No error; code correctly prefetches on button click
D. useRouter cannot be used inside functional components

Solution

  1. Step 1: Check router.prefetch usage

    Calling router.prefetch('/contact') inside an event handler is valid and triggers prefetch when the button is clicked.
  2. Step 2: Verify syntax and hooks usage

    The code has correct syntax and useRouter is properly used inside the functional component.
  3. Final Answer:

    No error; code correctly prefetches on button click -> Option C
  4. Quick Check:

    Prefetch in event handler is valid [OK]
Hint: Prefetch can run inside event handlers safely [OK]
Common Mistakes:
  • Thinking prefetch must run only on mount
  • Assuming missing semicolon breaks code
  • Believing useRouter is invalid in functional components
5. You want to improve user experience by preloading multiple pages on client side in Next.js. Which approach correctly prefetches pages for /about and /contact when the homepage loads?
hard
A. Call router.prefetch('/about') and router.prefetch('/contact') inside a useEffect hook with empty dependency array
B. Call router.prefetch('/about') and router.prefetch('/contact') directly in the component body
C. Call router.push('/about') and router.push('/contact') inside useEffect
D. Use getServerSideProps to prefetch pages on the server

Solution

  1. Step 1: Understand prefetch timing

    Prefetching pages on client should happen after component mounts to avoid blocking rendering, so useEffect with empty dependencies is ideal.
  2. Step 2: Avoid direct calls in component body or pushing pages

    Calling router.prefetch() directly in the component body runs on every render, which is inefficient. Using router.push() navigates instead of prefetching. getServerSideProps runs on server, not client cache.
  3. Final Answer:

    Call router.prefetch('/about') and router.prefetch('/contact') inside a useEffect hook with empty dependency array -> Option A
  4. Quick Check:

    Prefetch in useEffect = best practice [OK]
Hint: Prefetch pages inside useEffect to run once on mount [OK]
Common Mistakes:
  • Prefetching directly in component body causing repeated calls
  • Using router.push instead of prefetch
  • Trying to prefetch on server side with getServerSideProps
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