Route prefetching helps your app load pages faster by getting them ready before you click. It makes navigation smooth and quick.
Route prefetching behavior in NextJS
import Link from 'next/link'; <Link href="/about" prefetch={true}>About</Link>
The prefetch prop on Link controls if Next.js preloads the page.
By default, Next.js prefetches pages in the viewport when using next/link.
<Link href="/contact" prefetch={false}>Contact</Link>
<Link href="/blog">Blog</Link>import { useEffect } from 'react'; import { useRouter } from 'next/router'; const Component = () => { const router = useRouter(); useEffect(() => { router.prefetch('/dashboard'); }, [router]); return <div>Dashboard Prefetch Example</div>; };
This component shows three links with different prefetch behaviors. The About page is prefetched manually on load, Contact disables prefetch, and Blog uses default prefetching.
import Link from 'next/link'; import { useRouter } from 'next/router'; import { useEffect } from 'react'; export default function Home() { const router = useRouter(); useEffect(() => { // Manually prefetch the /about page router.prefetch('/about'); }, [router]); return ( <main> <h1>Welcome to Home</h1> <nav> <ul> <li><Link href="/about">About (prefetched manually)</Link></li> <li><Link href="/contact" prefetch={false}>Contact (no prefetch)</Link></li> <li><Link href="/blog">Blog (default prefetch)</Link></li> </ul> </nav> </main> ); }
Prefetching only works in production builds, not in development mode.
Prefetching uses idle browser time to avoid slowing down the main work.
Too much prefetching can waste bandwidth, so use it wisely.
Route prefetching loads pages in the background to speed up navigation.
You can control prefetching with the prefetch prop on Link or manually with router.prefetch().
Use prefetching to improve user experience but avoid overusing it to save bandwidth.
Practice
Solution
Step 1: Understand what prefetching means
Prefetching means loading resources ahead of time before they are needed.Step 2: Apply this to Next.js routing
Next.js prefetches pages in the background to make navigation faster when the user clicks a link.Final Answer:
To load pages in the background before the user navigates to them -> Option CQuick Check:
Prefetching = background loading [OK]
- Thinking prefetching blocks navigation
- Confusing prefetching with disabling navigation
- Assuming prefetching increases initial load size
Link component?Solution
Step 1: Recall the
Theprefetchprop usageprefetchprop accepts a boolean to enable or disable prefetching.Step 2: Identify the correct syntax to disable prefetching
Settingprefetch={false}disables automatic prefetching on theLink.Final Answer:
<Link href='/about' prefetch={false}>About</Link> -> Option AQuick Check:
Disable prefetch with prefetch={false} [OK]
- Using just 'prefetch' without value disables prefetching
- Using non-existent props like noPrefetch
- Passing string values instead of boolean
import Link from 'next/link';
export default function Home() {
return (
<div>
<Link href='/profile' prefetch>Go to Profile</Link>
</div>
);
}What happens when this component renders in the browser?
Solution
Step 1: Check the
Theprefetchprop usageprefetchprop is present without a value, which means it is true.Step 2: Understand Next.js default behavior
Next.js prefetches linked pages by default, so explicitly addingprefetchconfirms this behavior.Final Answer:
The '/profile' page is prefetched in the background to speed up navigation -> Option BQuick Check:
prefetch present = prefetch enabled [OK]
- Thinking prefetch defaults to false
- Assuming link causes full page reload
- Confusing syntax with errors
import { useRouter } from 'next/router';
import { useEffect } from 'react';
export default function Page() {
const router = useRouter();
useEffect(() => {
router.prefetch('/dashboard');
}, []);
return <div>Welcome</div>;
}However, the prefetch does not seem to work. What is the likely cause?
Solution
Step 1: Check the import source of
In Next.js 14+,useRouteruseRouterfor client navigation is imported from 'next/navigation', not 'next/router'.Step 2: Understand impact on
Importing from 'next/router' gives an outdated router withoutrouter.prefetchprefetchsupport, so prefetch won't work.Final Answer:
TheuseRouterimport path is incorrect; it should be from 'next/navigation' -> Option DQuick Check:
Correct import = 'next/navigation' [OK]
- Importing useRouter from 'next/router' instead of 'next/navigation'
- Not awaiting async prefetch (not required)
- Thinking prefetch only works in event handlers
Solution
Step 1: Disable automatic prefetching on the
SetLinkprefetch={false}to prevent Next.js from prefetching immediately.Step 2: Prefetch manually on user hover
UseonMouseEnterevent to callrouter.prefetch()to load the page only when user shows interest.Final Answer:
Set prefetch={false} on Link and call router.prefetch() inside an onMouseEnter event handler -> Option AQuick Check:
Manual prefetch on hover = prefetch={false} + onMouseEnter [OK]
- Prefetching both automatically and manually causing wasted bandwidth
- Calling prefetch in useEffect instead of on hover
- Relying on default prefetching without control
Ready to prove it?
Pass the test and this pattern moves unknown
