What if your website could remember pages so well, it feels like magic when you click around?
Why Router cache on client in NextJS? - Purpose & Use Cases
Imagine clicking links on a website and waiting for each page to fully reload every time, even if you visited that page just moments ago.
Manually reloading pages wastes time and data. It feels slow and clunky because the browser fetches everything again, even if nothing changed. This frustrates users and makes the site feel outdated.
Router cache on client stores pages you visited so when you click back or revisit, the page loads instantly without waiting for the server. This makes navigation smooth and fast, like flipping through a photo album you've already seen.
window.location.href = '/about'; // full page reload every timerouter.push('/about'); // uses cached page for instant load
It enables seamless, lightning-fast navigation that feels natural and keeps users engaged without annoying delays.
Think of an online store where you browse product details, then go back to the list instantly without waiting, making shopping smooth and enjoyable.
Manual page reloads cause slow, frustrating user experiences.
Client router cache stores visited pages for instant revisits.
This makes websites feel fast, smooth, and modern.
Practice
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 BQuick Check:
Router cache = faster navigation [OK]
- Thinking router cache increases server load
- Confusing cache with disabling navigation
- Assuming cache blocks page access
Solution
Step 1: Recall Next.js router methods
Next.js provides a method calledrouter.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 AQuick Check:
Prefetch method caches pages [OK]
- Using non-existent methods like router.load()
- Confusing fetch with prefetch
- Assuming cache() is a router method
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?
Solution
Step 1: Understand router.prefetch usage
The code callsrouter.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 DQuick Check:
Prefetch + push = instant load [OK]
- Thinking prefetch disables navigation
- Assuming page reloads from server always
- Believing prefetch causes errors inside components
import { useRouter } from 'next/router';
export default function Page() {
const router = useRouter();
function handleClick() {
router.prefetch('/contact')
}
return <button onClick={handleClick}>Load Contact</button>;
}Solution
Step 1: Check router.prefetch usage
Callingrouter.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 anduseRouteris properly used inside the functional component.Final Answer:
No error; code correctly prefetches on button click -> Option CQuick Check:
Prefetch in event handler is valid [OK]
- Thinking prefetch must run only on mount
- Assuming missing semicolon breaks code
- Believing useRouter is invalid in functional components
/about and /contact when the homepage loads?Solution
Step 1: Understand prefetch timing
Prefetching pages on client should happen after component mounts to avoid blocking rendering, souseEffectwith empty dependencies is ideal.Step 2: Avoid direct calls in component body or pushing pages
Callingrouter.prefetch()directly in the component body runs on every render, which is inefficient. Usingrouter.push()navigates instead of prefetching.getServerSidePropsruns on server, not client cache.Final Answer:
Call router.prefetch('/about') and router.prefetch('/contact') inside a useEffect hook with empty dependency array -> Option AQuick Check:
Prefetch in useEffect = best practice [OK]
- Prefetching directly in component body causing repeated calls
- Using router.push instead of prefetch
- Trying to prefetch on server side with getServerSideProps
Ready to prove it?
Pass the test and this pattern moves unknown
