Prefetching
A proactive optimization that fetches likely next-page code or data during browser idle time so subsequent navigations feel instantaneous
Table of Contents
Structure Diagram
What is Prefetching?
A proactive optimization that fetches likely next-page code or data during browser idle time so subsequent navigations feel instantaneous. It is a core concept in Performance & Optimization within Web Dev.
Core Principles
Prefetching uses idle network time—triggered by link visibility, hover intent, or focus—to fetch likely next-page chunks, HTML, or API data at low priority into cache before the user clicks.
Implemented via <link rel="prefetch">, the browser Speculation Rules API, or framework routers like Next.js <Link> and TanStack Query prefetchQuery, it eliminates perceived navigation latency without competing with current-page critical resources.
Practical Takeaways
- Balance eagerness against bandwidth: Prefetching every link on a 100-item catalog wastes mobile data and spikes server load. Prefetch primary CTAs on viewport entry and list items on hover/pointer-down intent.
- Respect data-saver preferences: Disable speculative prefetching when
navigator.connection?.saveDatais true or on slow cellular connections. - Don't confuse
preloadwithprefetch: Usepreloadfor high-priority assets needed on the current page (LCP image, primary font) andprefetchfor low-priority assets for the next navigation.
Notable Examples
- Next.js
<Link>: Automatically prefetches linked route bundles and RSC payloads as links enter the viewport. - TanStack Query
prefetchQuery: Warms detail-page API caches on row hover so opening an item skips loading spinners entirely.