What if your website could read your users' minds and load pages before they even click?
Why Route prefetching behavior in NextJS? - Purpose & Use Cases
Imagine clicking a link on a website and waiting several seconds for the new page to load, especially on slow connections or complex sites.
Manually loading pages only after a click causes delays, making users impatient and frustrated. It feels like waiting in line without knowing how long it will take.
Route prefetching loads the next page's data and code quietly in the background before you click, so navigation feels instant and smooth.
<a href="/about">About Us</a><Link href="/about" prefetch={true}>About Us</Link>Users experience near-instant page loads, improving satisfaction and engagement on your site.
When browsing an online store, route prefetching loads product pages ahead, so clicking on a product shows details immediately without waiting.
Manual page loading causes frustrating delays.
Route prefetching loads pages early in the background.
This makes navigation feel fast and seamless for users.
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
