
Next.js prefetch is a powerful feature that helps improve page loading times and user experience. It allows you to prefetch links on a page, so when the user clicks on them, the page loads instantly.
In development mode, Next.js prefetch works by automatically prefetching links when the page is loaded, but it doesn't actually load the page. This is because development mode is meant for rapid development and prototyping, not production-ready code.
This behavior is a deliberate design choice to prevent unnecessary page loads during development. By not actually loading the page, Next.js can save time and resources, making development faster and more efficient.
In production mode, Next.js prefetch works differently. It uses a technique called "link prefetching" to prefetch links on a page, which can improve page loading times and user experience.
Explore further: Router Prefetch Next Js
Optimization Techniques
Prefetching is a technique used to speed up the loading of web pages by preloading some content in the background before it's actually needed.
Prefetching proactively loads the anticipated pages or parts of a website, eliminating any noticeable delay. This technique is a hint to browsers that the user is likely to need the target resource for future navigations.
The prefetch keyword for the rel attribute of the link element is a specific instruction to browsers that they can likely improve the user experience by preemptively fetching and caching the resource.
By using prefetching, browsers can display content almost instantly when the user requests it, making for a smoother and more responsive user experience.
Prefetching During Development
Prefetching during development is a game-changer in Next.js 13. It happens automatically when you hover over a Link component in the user interface.
This means that as you navigate through your application during development, Next.js fetches and caches the content of the next page based on your interactions.
Turning Off
Turning off prefetching can be a great way to conserve bandwidth or address privacy concerns.

You can disable prefetching for specific links by using the prefetch attribute on the Link component and setting it to false. This prevents Next.js from prefetching the content when the user hovers over the link.
This can be especially useful if you're working on a project that requires careful management of bandwidth usage.
During Development
During development, prefetching routes in Next.js 13 is incredibly smooth. It happens as you hover over a Link component in the user interface.
This means that as you navigate through your application, Next.js automatically fetches and caches the content of the next page based on your interactions.
You get a seamless experience, giving you a glimpse of how the final performance will look and feel.
Production Builds and Prefetching
In production builds, Next.js 13 takes prefetching to a whole new level by automatically prefetching routes as they become visible in the viewport.
As soon as a Link comes into view, Next.js proactively fetches and caches the route content, ensuring that when the user clicks on a link to navigate, the page transition is instantaneous.
This feature can occur during the initial page load or when the user scrolls through the website, making it a seamless experience for users.
You might enjoy: Nextjs Loading Page Ssr
Custom Prefetching Solutions
You can build a custom component that does link prefetch on hover using the Link component from Next.js.
To add prefetching to custom routing components, use the prefetch method from useRouter.
Prefetching can be done inside the useEffect hook, and the route specified in the href property gets prefetched when that custom component is rendered.
The prefetch property on a custom component can be set to true to enable prefetching.
If the prefetch property is set to false, the route will not be prefetched.
You can programmatically prefetch routes using the prefetch method on the router object returned from useRouter.
By calling router.prefetch() with the full path of a route, you can prefetch that specific route.
This is particularly useful when you know beforehand that the user is likely to navigate to a particular page.
Prefetching in advance ensures a seamless and instant transition without any additional requests.
Next.js provides a powerful method to prefetch routes, making it easy to implement custom prefetching solutions.
Additional reading: Nextjs Load Component One by One Example
Link Component and Prefetching
The Link component in Next.js is a powerful tool for enabling prefetching and client-side navigation between routes. It's one of the primitives upon which you build applications, but its behavior has changed since Next.js 13 beta.
To add to the confusion, the prefetch prop on the Link component doesn't mean what you might think it does. In Next.js 12 or Pages Router, prefetch took only two values: true or false, but these values didn't really convey the mechanics.
Here's a summary of the prefetch prop values:
In Next.js 13, the prefetch prop is still optional, but it takes different values depending on whether it points to a static or dynamic page.
Link Component in App Router
The Link Component in the App Router has seen significant changes with the introduction of App Router in Next.js 13. The prefetch prop is still optional and takes three values: true, false, and null. The behavior of prefetching now depends on whether the route is static or dynamic.
Related reading: Next Js App Router Tailwind Spinner Loading Page
For static pages, setting prefetch to true will prefetch the full route when the link enters the viewport. However, for dynamic pages, setting prefetch to true will also prefetch the full route, making it more powerful and adjustable.
Prefetching is completely disabled when the prefetch prop is set to false. The new default value is null, which means the behavior depends on whether the route is static or dynamic.
Here's a summary of the prefetch prop values:
This new behavior in the Link Component in the App Router provides more flexibility and control over prefetching, making it easier to optimize your application's performance.
Client-Side Data Fetching
Client-Side Data Fetching can be a bit tricky, especially when it comes to navigation. If a page fetches data directly from an API on the client-side, prefetching the page only loads the JavaScript bundle.
This means that the actual data fetching from the API occurs after the user has navigated to the page. You can still see a loading state while the API request is in progress.
Recommended read: Next Js Build Collecting Page Data
API calls on navigation happen quickly, but the data fetching after navigation can be slower. This is because the JavaScript bundle is loaded first, and then the API request is made.
Here's a breakdown of what happens:
- API calls are made on navigation.
- Data fetching occurs after navigation.
This can cause a delay in the user seeing the data, which might not be ideal for a smooth user experience.
API Data Involvement and Prefetching
API data involvement and prefetching in Next.js can be a bit tricky. Prefetching involving data from an API becomes complex, depending on your application's structure and how it fetches data.
If a page fetches data directly from an API on the client-side, prefetching only loads the JavaScript bundle. This means the actual data fetching from the API occurs after the user has navigated to the page.
There are two key points to understand about API calls on navigation: API calls are made after navigation, and data fetching from the API can still result in a loading state while the request is in progress.
Here are the key points summarized:
- API calls are made after navigation.
- Data fetching from the API can still result in a loading state.
Hybrid Approach and Prefetching
In Next.js, a common pattern is to use a combination of getStaticProps (or getServerSideProps) for the initial page load, and then handle updates or dynamic content with client-side data fetching.
This approach benefits from prefetching for the initial load, and subsequent data updates are handled client-side as the user interacts with the page.
To achieve this hybrid approach, you can use the Link component from Next.js, which enables prefetching and client-side navigation between routes.
On a similar theme: Next Js Fetch Data save in Context and Next Route
Example and Implementation
In Next.js, prefetching is a way to pre-load pages in the background, so they're ready to go when the user needs them. This can be especially useful for pages that are frequently visited or for users with slower internet connections.
To enable prefetching in Next.js, you can use the `getStaticProps` method in your pages' code. This method allows you to generate static HTML for a page at build time, and it can also be used to prefetch pages in the background.
A different take: Next Js Pages
The `useEffect` hook can be used to prefetch pages when the user navigates to a new page. This is done by calling the `prefetch` function provided by Next.js.
Next.js provides a `useRouter` hook that allows you to access the router object and use its `prefetch` method to prefetch pages. This can be useful for cases where you need to prefetch a page based on some dynamic condition.
Prefetching can be disabled for a specific page by setting the `disablePrefetch` option to true in the page's `getStaticProps` method. This can be useful if you have a page that doesn't benefit from prefetching or if you want to test your app without prefetching enabled.
A unique perspective: Nextjs Prefetching
Accessibility and Prefetching
Improving accessibility is crucial, and our current solution completely ignores users navigating the application with keyboards and touch screens.
Adding handlers for onPointerEnter, onTouchStart, and onFocus can greatly enhance accessibility, as demonstrated by the example of improving it.
Calling prefetch on focus can trigger many unnecessary requests, such as prefetching links that are not currently in focus.
To mitigate this issue, suggestions are needed to improve the prefetching behavior when focusing on links.
Users may need to focus on multiple links before reaching the desired one, resulting in unnecessary prefetch requests.
Internet Speed and Prefetching
Prefetching in Next.js only works on fast internet connections.
You can use navigator.connection to check the user's internet speed before triggering prefetch.
Prefetching should be triggered only when the user has a stable and fast internet connection.
This ensures that the prefetching process doesn't slow down the user's experience.
Automatic Page Preloading
Automatic Page Preloading is a powerful feature in Next.js that helps improve user experience by loading pages in the background. This happens automatically when a Link component is included in a Next.js page and rendered into the viewport.
The prefetching process starts as soon as the link enters the viewport, and it's typically not noticeable to the user. Next.js intelligently detects which links are likely to be clicked by observing which links are in the viewport.
Here's what's included in the prefetching process:
- The JavaScript code (the bundled file for the page)
- Other resources like data needed for that page (if you're using a feature like getStaticProps or getServerSideProps)
Next.js only fetches the JavaScript, it doesn't execute it, so you're not downloading any additional content that the prefetched page might request until you visit the link.
Speeding Up Navigation
Speeding up navigation is a game-changer for user experience. With Next.js prefetch, you can display pages almost instantly, giving the appearance of a very fast navigation experience.
Immediate availability is a key benefit, as it allows users to access prefetched links instantly without waiting for resources to load. This seamless transition makes a significant improvement over traditional page loads.
Next.js handles prefetching intelligently, taking into account user bandwidth and resource priority. It avoids prefetching on slow networks to keep the app responsive.
Here are the benefits of intelligent resource management:
- Prefetching is adjusted based on user bandwidth.
- Resource priority is considered to optimize prefetching.
This approach ensures a seamless and responsive user experience, even on slower networks.
Custom Routing and Prefetching
You can add prefetching to custom routing components using the `useRouter` hook from Next.js. This is done by calling the `prefetch` method inside the `useEffect` hook.
The `prefetch` method is used to prefetch the route specified in the `href` property when the custom routing component is rendered. This can be seen in the example code for the `MyLink` component, where the `prefetch` property is set to `true` to prefetch the route when the link is rendered.
To implement prefetching in your custom routing code, use the `prefetch` method from `useRouter`. This method takes the route to prefetch as an argument.
Here are some examples of how to use the `prefetch` method:
In the example app, the `index.js` page has a `MyLink` to `margherita.js` and `pineapple-pizza.js`, where the `prefetch` property is set to `true` on `/margherita` and to `false` on `/pineapple-pizza`. This demonstrates how to use the `prefetch` method to prefetch routes in custom routing components.
Frequently Asked Questions
What does router prefetch do?
Router prefetch optimizes client-side transitions by preloading pages, but only in production mode and when using next/link is not present
Featured Images: pexels.com


