2 min read
Edit on GitHub Intelligent Prefetching#
Keywords: page prefetch, fast transitions, prefetch link, hover prefetch
Asok's Reactive Engine includes a prefetching system that can reduce navigation latency by background-loading HTML fragments before the user clicks a link.
How it works#
- Detection: The engine identifies all elements with
data-blockordata-urlthat use the defaultclicktrigger (like links). - Trigger: When a user hovers (
mouseover) over such an element, Asok immediately starts fetching the associated fragment. - Caching: The fetched HTML is stored in a client-side memory cache.
- Instant Swap: When the user finally clicks the link, Asok checks the cache. If the fragment is already there, it performs the swap instantly without a network round-trip.
Example#
<!-- Hovering over this link triggers a pre-fetch of #main from /profile -->
<a href="/profile" data-block="main" data-push-url>View Profile</a>
You do not need to configure it separately — it is active by default for all data-block links.
Optimization Details#
- Safe consuming: Each cached fragment is consumed once. If you click again later, a new fetch will occur (unless you hover again).
- Reduced Latency: Prefetching typically happens during the "mouse dwell time" (the 200-400ms between hovering and clicking), which is often enough time to fetch the entire fragment.
- Bandwidth Aware: Only fragments (small HTML blocks) are prefetched, not full pages.
- Header identification: Prefetch requests send
X-Prefetch: 1header, allowing you to optimize server-side logic if needed.
Verification#
To see it in action:
- Open your browser's Network tab.
- Hover over a link with
data-block. - You will see a
fetchrequest occur immediately. - Click the link.
- Notice that no new request is made (or a very short one if it wasn't finished), and the page updates instantly.
Use prefetching combined with
data-indicatorto provide immediate feedback if a pre-fetch hasn't finished yet when the user clicks.
Was this page helpful?