📖 Scenario: You are building a simple Next.js app with two pages: Home and About. You want to improve user experience by prefetching the About page route when the Home page loads. This means the About page will load faster when the user clicks its link.
🎯 Goal: Build a Next.js app that prefetches the About page route on the Home page using the next/link component's prefetch behavior.
📋 What You'll Learn
Create a Home page with a link to the About page using next/link
Add a configuration variable to control whether prefetching is enabled
Use the prefetch prop on the Link component to enable or disable prefetching based on the config
Add the About page with simple content
💡 Why This Matters
🌍 Real World
Prefetching routes in Next.js improves user experience by loading pages in the background before the user clicks, making navigation feel instant.
💼 Career
Understanding route prefetching is important for frontend developers working with Next.js to optimize app performance and user experience.
Progress0 / 4 steps
1
Create the Home page with a link to About
Create a file called app/page.tsx with a functional component named HomePage. Inside it, return a Link component from next/link that links to "/about" with the text "Go to About". Import Link from next/link at the top.
NextJS
Hint
Use the Link component from next/link to create the link to the About page.
2
Add a config variable to control prefetching
Inside app/page.tsx, add a constant named shouldPrefetch and set it to true. This will control if the About page link prefetches.
NextJS
Hint
Define a constant shouldPrefetch before the component function.
3
Use the prefetch prop on Link based on config
Update the Link component in HomePage to include the prefetch prop. Set it to the value of shouldPrefetch so that prefetching is enabled or disabled based on this variable.
NextJS
Hint
Add the prefetch attribute to the Link component and set it to shouldPrefetch.
4
Add the About page with simple content
Create a file called app/about/page.tsx with a default exported functional component named AboutPage. It should return a main element with an h1 heading that says "About Page".
NextJS
Hint
Create a simple functional component that returns a heading inside a main tag.
Practice
(1/5)
1. What is the main purpose of route prefetching in Next.js?
easy
A. To prevent users from navigating to certain pages
B. To disable client-side navigation
C. To load pages in the background before the user navigates to them
D. To increase the size of the initial JavaScript bundle
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 C
Quick Check:
Prefetching = background loading [OK]
Hint: Prefetching means loading before navigation happens [OK]
Common Mistakes:
Thinking prefetching blocks navigation
Confusing prefetching with disabling navigation
Assuming prefetching increases initial load size
2. Which of the following is the correct way to disable automatic prefetching on a Next.js Link component?
easy
A. <Link href='/about' prefetch={false}>About</Link>
B. <Link href='/about' prefetch>About</Link>
C. <Link href='/about' noPrefetch>About</Link>
D. <Link href='/about' prefetch='off'>About</Link>
Solution
Step 1: Recall the prefetch prop usage
The prefetch prop accepts a boolean to enable or disable prefetching.
Step 2: Identify the correct syntax to disable prefetching
Setting prefetch={false} disables automatic prefetching on the Link.
Final Answer:
<Link href='/about' prefetch={false}>About</Link> -> Option A
Quick Check:
Disable prefetch with prefetch={false} [OK]
Hint: Use prefetch={false} to turn off prefetching [OK]
Common Mistakes:
Using just 'prefetch' without value disables prefetching
Using non-existent props like noPrefetch
Passing string values instead of boolean
3. Consider this Next.js code snippet:
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?
medium
A. The '/profile' page is not prefetched because prefetch defaults to false
B. The '/profile' page is prefetched in the background to speed up navigation
C. The link will cause a full page reload when clicked
D. The component will throw a syntax error due to incorrect prefetch usage
Solution
Step 1: Check the prefetch prop usage
The prefetch prop 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 adding prefetch confirms this behavior.
Final Answer:
The '/profile' page is prefetched in the background to speed up navigation -> Option B
Quick Check:
prefetch present = prefetch enabled [OK]
Hint: Presence of prefetch prop means prefetching is on [OK]
Common Mistakes:
Thinking prefetch defaults to false
Assuming link causes full page reload
Confusing syntax with errors
4. You wrote this code to prefetch a route manually:
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?
medium
A. You must call router.prefetch inside an event handler, not useEffect
B. The router.prefetch method does not exist in Next.js
C. The router.prefetch method is asynchronous and needs to be awaited
D. The useRouter import path is incorrect; it should be from 'next/navigation'
Solution
Step 1: Check the import source of useRouter
In Next.js 14+, useRouter for client navigation is imported from 'next/navigation', not 'next/router'.
Step 2: Understand impact on router.prefetch
Importing from 'next/router' gives an outdated router without prefetch support, so prefetch won't work.
Final Answer:
The useRouter import path is incorrect; it should be from 'next/navigation' -> Option D
Quick Check:
Correct import = 'next/navigation' [OK]
Hint: Use 'next/navigation' for router.prefetch in Next.js 14+ [OK]
Common Mistakes:
Importing useRouter from 'next/router' instead of 'next/navigation'
Not awaiting async prefetch (not required)
Thinking prefetch only works in event handlers
5. You want to optimize bandwidth by prefetching only when a user hovers over a link. Which approach correctly implements this in Next.js?
hard
A. Set prefetch={false} on Link and call router.prefetch() inside an onMouseEnter event handler
B. Set prefetch on Link and call router.prefetch() inside useEffect
C. Remove prefetch from Link and rely on default prefetching
D. Use prefetch={true} on Link and disable prefetching globally in config
Solution
Step 1: Disable automatic prefetching on the Link
Set prefetch={false} to prevent Next.js from prefetching immediately.
Step 2: Prefetch manually on user hover
Use onMouseEnter event to call router.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 A
Quick Check:
Manual prefetch on hover = prefetch={false} + onMouseEnter [OK]
Hint: Disable auto prefetch, prefetch manually on hover event [OK]
Common Mistakes:
Prefetching both automatically and manually causing wasted bandwidth
Calling prefetch in useEffect instead of on hover
Relying on default prefetching without control
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.