Understanding Prefetching in Next.js: prefetching is primarily applied to the <Link>
component in Next.js. When a <Link>
component enters the user’s viewport (initially or through scroll), Next.js automatically prefetches and loads the linked route and its data in the background to improve the performance of client-side navigations this will Enhance Navigation with the Link Component.
In Next.js, the Link
component is a built-in component that extends the HTML <a>
tag to provide prefetching and client-side navigation between routes. It is the primary and recommended way to navigate between routes in Next.js
The Link
component enables prefetching, which means that when a user hovers over a link, Next.js automatically prefetches the page’s JavaScript and CSS resources in the background. This prefetching improves the user experience by reducing the loading time when the user clicks on the link
Table of Contents
What is Prefetching?
Prefetching is an important technique in web development that aims to improve the performance and user experience of web applications. In Next.js, this functionality is primarily facilitated by the component, which is used for client-side navigation.
Prefetching in Next.js is a performance optimisation technique that loads resources for a page in the background before the user navigates to that page. This makes navigation instantaneous, providing a smooth and fast user experience.This predictive loading helps create a smoother and faster navigation experience for users by reducing the perceived latency of switching between pages.
How Prefetching Works in Nextjs ?
Prefetching only fetches the JavaScript code for the linked page, not the actual content. Next.js uses various prefetching strategies:
Viewport-Based Prefetching
In production, Next.js prefetches routes as they become visible in the viewport. This can occur during the initial page load or when the user scrolls through the website. The Intersection Observer API is used to detect when links appear in the viewport, triggering the prefetching process. This approach ensures that only the necessary content is downloaded, optimizing bandwidth usage.
Hover-Based Prefetching
During development, prefetching routes can be triggered by simply hovering over a <Link> component in the user interface. This means that as you navigate through your application during development, Next.js automatically fetches and caches the content of the next page based on your interactions.
Programmatic Prefetching
Next.js provides a method to programmatically prefetch routes using the prefetch method on the router object returned from the useRouter hook. This allows developers to dynamically control prefetching behavior.
Implementing Prefetching in Next.js
Next.js uses various prefetching strategies:
Using the Link Component
The <Link> component from Next.js is used to route to different pages within the app and includes an important prop: prefetch. By default, the prefetch prop is set to true, meaning the page will be pre-fetched when the link is visible in the viewport or when Next.js predicts that the user will navigate to it soon.
import Link from 'next/link';
function HomePage() {
return (
<div>
<Link href="/about">About</Link>
</div>
);
}
export default HomePage;
Disabling Prefetching
To avoid downloading unnecessary content, you can disable prefetching for rarely visited pages by setting the prefetch
property on <Link>
to false
.
<Link href="/about" prefetch={false}>
<a>About</a>
</Link>
Programmatic Prefetching
We can programmatically prefetch routes using the prefetch
method on the router object returned from the useRouter
hook.
import { useRouter } from 'next/router';
import { useEffect } from 'react';
function HomePage() {
const router = useRouter();
useEffect(() => {
router.prefetch('/about');
}, [router]);
return (
<div>
<Link href="/about">
<a>About</a>
</Link>
</div>
);
}
export default HomePage;
Note: prefetch
method: The prefetch
method on the router object is specifically designed for programmatic prefetching of routes. It was introduced in Next.js 9 and continues to function in Next.js 14.
Benefits of Nextjs Prefetching
- Improved Performance: Prefetching significantly reduces the time it takes to load new pages, resulting in a faster and more responsive application. By preloading resources, users experience smoother transitions between pages, leading to higher satisfaction and engagement.
- Enhanced User Experience: Prefetching can dramatically improve the user experience on a web application, especially on sites with high interaction and engagement rates. By loading future pages before they are needed, the perceived latency is greatly reduced, providing a more fluid browsing experience.
Conclusion:
Prefetching in Next.js is a powerful feature that enhances the performance and user experience of web applications by preloading resources in the background. By understanding and implementing prefetching, developers can create faster, more responsive, and user-friendly applications. The built-in support for prefetching in Next.js, along with the flexibility to control its behavior, makes it an essential tool for modern web development. By leveraging prefetching, developers can ensure that their applications are not only performant but also provide a seamless and enjoyable experience for users.