Prefetching Next Page Resources for Navigation
Use prefetch to preload resources for the next page the user is likely to visit, enabling near-instant page transitions and better user experience.
Detailed Explanation
Prefetching for Next Page Navigation
<link rel="prefetch"> downloads resources that will be needed on the next page the user is likely to visit. Unlike preload (which is for the current page), prefetch uses idle network time and does not compete with critical resources.
How Prefetch Works
- The browser downloads prefetched resources with the lowest priority
- Resources are stored in the HTTP cache (not the preload cache)
- When the user navigates to the next page, the cached resources load instantly
- If the user does not navigate, the resources are simply cached for later use
Common Patterns
Prefetch the Next Page HTML
<!-- On a blog listing page, prefetch the most popular article -->
<link rel="prefetch" href="/blog/popular-post" as="document" />
Prefetch JavaScript Bundles
<!-- On the product page, prefetch the checkout bundle -->
<link rel="prefetch" href="/js/checkout.js" as="script" />
<link rel="prefetch" href="/css/checkout.css" as="style" />
Prefetch on Hover or Focus
For more targeted prefetching, you can add prefetch hints dynamically:
document.querySelectorAll('a[data-prefetch]').forEach(link => {
link.addEventListener('mouseenter', () => {
const hint = document.createElement('link');
hint.rel = 'prefetch';
hint.href = link.href;
hint.as = 'document';
document.head.appendChild(hint);
}, { once: true });
});
This pattern prefetches the page only when the user hovers over a link, balancing performance with bandwidth efficiency.
Prefetch vs Prerender
prefetch downloads the resource but does not process it. For even faster navigation, some browsers support <link rel="prerender"> or the newer Speculation Rules API, which actually renders the page in the background:
<script type="speculationrules">
{
"prefetch": [
{ "source": "list", "urls": ["/next-page"] }
]
}
</script>
Best Practices
- Only prefetch likely destinations — analyze your analytics to determine the most common next pages
- Limit to 2-3 resources — prefetching too many wastes bandwidth, especially on mobile
- Respect Data Saver mode — check
navigator.connection.saveDatabefore prefetching - Use
asattribute — helps the browser cache the resource with the correct type - Do not prefetch on slow connections — check
navigator.connection.effectiveType
Use Case
An e-commerce product page prefetches the checkout CSS and JavaScript bundles because 40% of users who visit a product page proceed to checkout. When the user clicks 'Buy Now', the checkout page loads in under 200ms instead of the usual 1.5 seconds because the bundles are already cached.