Router cache on client helps your app load pages faster by remembering data and components you already visited. It avoids reloading everything from the server each time.
Router cache on client in NextJS
import { useRouter } from 'next/navigation'; const router = useRouter(); router.prefetch('/page');
router.prefetch() tells Next.js to load the page in the background before the user visits it.
This caching happens automatically for visited pages, but prefetch helps prepare pages early.
'use client'; import React from 'react'; import { useRouter } from 'next/navigation'; export default function Home() { const router = useRouter(); // Prefetch about page when component mounts React.useEffect(() => { router.prefetch('/about'); }, []); return <button onClick={() => router.push('/about')}>Go to About</button>; }
import Link from 'next/link'; export default function Nav() { return <nav> <Link href="/contact" prefetch={false}>Contact</Link> </nav>; }
This component preloads the About page when the Home page loads. Clicking the button navigates instantly because the page is cached.
'use client'; import { useRouter } from 'next/navigation'; import React from 'react'; export default function Home() { const router = useRouter(); React.useEffect(() => { // Prefetch the About page to cache it on client router.prefetch('/about'); }, []); return ( <main> <h1>Home Page</h1> <button onClick={() => router.push('/about')}>Go to About</button> </main> ); }
Next.js automatically caches pages you visit, so you usually don't need to manage cache manually.
Use router.prefetch() to improve speed for pages users are likely to visit next.
Be mindful of network usage when prefetching many pages.
Router cache on client speeds up navigation by storing visited pages.
Use router.prefetch() to load pages before users click links.
This improves user experience with faster page loads and smoother transitions.
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
