0
0
NextJSframework~10 mins

Route prefetching behavior in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Route prefetching behavior
Page loads
Next.js scans <Link> components
Prefetch triggered for linked routes
Browser downloads JS bundles for prefetched routes
Prefetched data cached
User clicks link
Route loads instantly from cache
Page rendered quickly
Prefetch for other links continues
Repeat for new links
Next.js automatically prefetches linked routes after page load to speed up navigation by downloading code in advance.
Execution Sample
NextJS
import Link from 'next/link';

export default function Home() {
  return <Link href="/about">About</Link>;
}
This code renders a link to the /about page which Next.js will prefetch automatically after the Home page loads.
Execution Table
StepActionPrefetch Triggered?Prefetched RouteCache StateUser InteractionPage Load Result
1Home page loadsNoNoneEmptyNoHome page rendered
2Next.js scans <Link> to /aboutYes/aboutPrefetching /about startedNoHome page rendered
3Browser downloads /about JS bundlesYes/aboutCached /about bundlesNoHome page rendered
4User clicks /about linkNo/aboutCached /about bundlesYes/about page loads instantly
5Next.js scans new links on /aboutYesOther linked routesPrefetching new routesNo/about page rendered
6User clicks another linkNoCached routeCached bundlesYesNew page loads instantly
💡 Prefetching stops when user navigates away or all linked routes are cached.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
Prefetch Triggered?NoYesYesNoYesNo
Prefetched RouteNone/about/about/aboutOther linked routesCached route
Cache StateEmptyPrefetching /about startedCached /about bundlesCached /about bundlesPrefetching new routesCached bundles
User InteractionNoNoNoYesNoYes
Page Load ResultHome page renderedHome page renderedHome page rendered/about page loads instantly/about page renderedNew page loads instantly
Key Moments - 3 Insights
Why does Next.js prefetch routes even before the user clicks the link?
Next.js prefetches linked routes after the page loads to make navigation faster by having the code ready in cache, as shown in execution_table step 2 and 3.
Does prefetching happen again after the user navigates to a new page?
Yes, Next.js scans new links on the new page and prefetches those routes, as seen in step 5 of the execution_table.
What happens if the user clicks a link before prefetching finishes?
The route will load normally but might be slower because the bundles are not cached yet; prefetching aims to avoid this delay by starting early.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the /about page bundles get cached?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Cache State' column in execution_table rows.
According to variable_tracker, what is the 'User Interaction' value after step 4?
AYes
BNo
CMaybe
DUnknown
💡 Hint
Look at the 'User Interaction' row and the 'After Step 4' column.
If the Home page had no <Link> components, how would the prefetch behavior change?
APrefetching would still happen for /about
BPrefetching would happen for all routes
CNo prefetching would occur
DPrefetching would happen only after user clicks
💡 Hint
Prefetching depends on scanning components as shown in concept_flow.
Concept Snapshot
Next.js Route Prefetching:
- Automatically prefetches linked routes after page load
- Downloads JS bundles in background
- Caches bundles for instant navigation
- Prefetch repeats on new pages
- Speeds up user experience by reducing wait time
Full Transcript
When a Next.js page loads, it looks for <Link> components on the page. For each link, Next.js starts prefetching the JavaScript bundles needed for that route in the background. This means the code is downloaded and cached before the user clicks the link. When the user clicks, the page loads instantly from cache, making navigation very fast. After navigating, Next.js repeats this process for new links on the new page. If there are no links, no prefetching happens. This behavior improves user experience by reducing wait times during navigation.