What is the purpose of router cache on the client in Next.js?
Router cache on the client stores previously visited pages to speed up navigation by avoiding full page reloads.
Click to reveal answer
beginner
How does Next.js use router cache to improve user experience?
Next.js prefetches linked pages and caches them on the client, so when users click links, pages load instantly without waiting for the server.
Click to reveal answer
intermediate
Which Next.js hook or method helps in prefetching pages for router cache?
The next/link component automatically prefetches pages in the background when visible, enabling router cache.
Click to reveal answer
beginner
What happens if router cache is disabled or not used in Next.js?
Without router cache, each navigation fetches fresh data from the server instead of using the cache, causing slower transitions and a less smooth user experience.
Click to reveal answer
intermediate
Can router cache cause stale data issues? How to handle it in Next.js?
Yes, cached pages might show old data. Use client-side data fetching hooks like useSWR or useEffect to refresh data after navigation.
Click to reveal answer
What does Next.js router cache store on the client?
ADatabase queries
BServer logs
CUser cookies
DPreviously visited pages
✗ Incorrect
Router cache stores pages visited before to speed up navigation.
Which Next.js component automatically prefetches pages for router cache?
A<code>next/link</code>
B<code>next/image</code>
C<code>next/script</code>
D<code>next/head</code>
✗ Incorrect
next/link prefetches linked pages to cache them.
What is a benefit of router cache on client in Next.js?
AFaster page transitions
BMore server load
CSlower navigation
DIncreased page size
✗ Incorrect
Router cache speeds up navigation by loading pages instantly.
How can you avoid stale data when using router cache in Next.js?
ADisable JavaScript
BClear browser cookies
CUse client-side data fetching hooks like <code>useSWR</code>
DUse server-side rendering only
✗ Incorrect
Client-side hooks refresh data after navigation to keep it fresh.
What happens if router cache is not used in Next.js navigation?
APages load instantly
BFetches fresh data from the server on every navigation
CPrefetching increases
DNavigation is disabled
✗ Incorrect
Without cache, every navigation fetches from the server instead of using the cache, causing slower transitions.
Explain how Next.js router cache works and why it improves navigation speed.
Think about how visiting a page once helps next visits load faster.
You got /4 concepts.
Describe how to handle data freshness when using router cache in Next.js.
Consider how to update data after loading a cached page.
You got /4 concepts.
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
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.
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.
Final Answer:
To speed up navigation by storing visited pages locally -> Option B
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
Step 1: Recall Next.js router methods
Next.js provides a method called router.prefetch() to load pages in advance.
Step 2: Match method to caching behavior
router.prefetch() loads the page and stores it in the client cache for faster navigation.
Final Answer:
router.prefetch() -> Option A
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
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.
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.
Final Answer:
The /about page loads instantly from client cache -> Option D
Quick Check:
Prefetch + push = instant load [OK]
Hint: Prefetch before push means instant page load [OK]
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
Step 1: Check router.prefetch usage
Calling router.prefetch('/contact') inside an event handler is valid and triggers prefetch when the button is clicked.
Step 2: Verify syntax and hooks usage
The code has correct syntax and useRouter is properly used inside the functional component.
Final Answer:
No error; code correctly prefetches on button click -> Option C
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
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.
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.
Final Answer:
Call router.prefetch('/about') and router.prefetch('/contact') inside a useEffect hook with empty dependency array -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.