Bird
Raised Fist0
NextJSframework~20 mins

Route prefetching behavior in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Route Prefetching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a Next.js Link with prefetch enabled is rendered?

Consider a Next.js app with a <Link> component that has prefetch enabled (default behavior). What does Next.js do when this component appears on the page?

NextJS
<Link href="/about">About</Link>
ANext.js prefetches only the CSS for the /about page but not the JavaScript.
BNext.js waits until the user clicks the link before fetching the /about page resources.
CNext.js disables prefetching if the link is outside the viewport.
DNext.js automatically fetches the JavaScript and data for the /about page in the background after the page loads.
Attempts:
2 left
💡 Hint

Think about how Next.js improves navigation speed by loading pages early.

❓ state_output
intermediate
2:00remaining
What is the effect of disabling prefetch on a Next.js Link?

Given this code snippet, what will be the user experience difference compared to default prefetch behavior?

<Link href="/contact" prefetch={false}>Contact</Link>
AThe link will not navigate to /contact at all because prefetch is false.
BThe /contact page resources are fetched immediately when the page loads, same as default.
CThe /contact page resources are only fetched when the user clicks the link, causing a slight delay on navigation.
DThe /contact page resources are fetched only when the link is hovered by the mouse.
Attempts:
2 left
💡 Hint

Disabling prefetch means Next.js won't load the page early.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly disables prefetching for a Next.js Link?

Choose the correct way to disable prefetching on a Next.js Link component.

A<Link href="/blog" prefetch="false">Blog</Link>
B<Link href="/blog" prefetch={false}>Blog</Link>
C<Link href="/blog" prefetch={0}>Blog</Link>
D<Link href="/blog" prefetch={null}>Blog</Link>
Attempts:
2 left
💡 Hint

Remember that prefetch expects a boolean value.

🔧 Debug
advanced
2:00remaining
Why does prefetch not work on a dynamic route Link in Next.js?

Given a dynamic route /posts/[id] and this Link:

<Link href="/posts/[id]" as="/posts/123">Post 123</Link>

Why might Next.js not prefetch this page?

ANext.js cannot prefetch dynamic routes without explicit <code>href</code> parameters matching the dynamic segments.
BPrefetching is disabled by default on all dynamic routes.
CThe <code>as</code> prop disables prefetching automatically.
DPrefetching only works on static HTML pages, not on dynamic routes.
Attempts:
2 left
💡 Hint

Think about how Next.js needs to know the exact path to prefetch.

🧠 Conceptual
expert
2:00remaining
When does Next.js trigger route prefetching for Link components?

Which of the following best describes when Next.js triggers prefetching for a Link component?

ANext.js prefetches routes for Link components that are visible in the viewport after the page load event.
BNext.js prefetches routes only when the Link component is focused via keyboard navigation.
CNext.js prefetches routes only when the user hovers over the Link component.
DNext.js prefetches all Link routes on the page immediately during server-side rendering.
Attempts:
2 left
💡 Hint

Consider how Next.js balances performance and user experience by prefetching only visible links.

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

  1. Step 1: Understand what prefetching means

    Prefetching means loading resources ahead of time before they are needed.
  2. 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.
  3. Final Answer:

    To load pages in the background before the user navigates to them -> Option C
  4. 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

  1. Step 1: Recall the prefetch prop usage

    The prefetch prop accepts a boolean to enable or disable prefetching.
  2. Step 2: Identify the correct syntax to disable prefetching

    Setting prefetch={false} disables automatic prefetching on the Link.
  3. Final Answer:

    <Link href='/about' prefetch={false}>About</Link> -> Option A
  4. 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

  1. Step 1: Check the prefetch prop usage

    The prefetch prop is present without a value, which means it is true.
  2. Step 2: Understand Next.js default behavior

    Next.js prefetches linked pages by default, so explicitly adding prefetch confirms this behavior.
  3. Final Answer:

    The '/profile' page is prefetched in the background to speed up navigation -> Option B
  4. 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

  1. Step 1: Check the import source of useRouter

    In Next.js 14+, useRouter for client navigation is imported from 'next/navigation', not 'next/router'.
  2. Step 2: Understand impact on router.prefetch

    Importing from 'next/router' gives an outdated router without prefetch support, so prefetch won't work.
  3. Final Answer:

    The useRouter import path is incorrect; it should be from 'next/navigation' -> Option D
  4. 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

  1. Step 1: Disable automatic prefetching on the Link

    Set prefetch={false} to prevent Next.js from prefetching immediately.
  2. Step 2: Prefetch manually on user hover

    Use onMouseEnter event to call router.prefetch() to load the page only when user shows interest.
  3. Final Answer:

    Set prefetch={false} on Link and call router.prefetch() inside an onMouseEnter event handler -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard