Links can be preloaded before the user clicks them. When a preloaded link is clicked, the response is already cached and the interaction feels instant.
Eager preloading can also populate the cache to keep links accessible while offline.
To preload a link when the user hovers
over it, set an [up-preload] attribute:
<a href="/path" up-preload>Hover over me to preload my content</a> <!-- mark: up-preload -->
By default Unpoly waits for 90 milliseconds of hovering before making the preload request. This prevents accidental preloading when the mouse moves over a link that the user doesn't intend to click.
To change the delay, set an [up-preload-delay] attribute
or configure up.link.config.preloadDelay. Increasing the delay will lower the load on your server,
but will also make the interaction feel less instant.
On touch devices preloading begins when the user places their finger on the link.
To preload all links on hover, configure up.link.config.preloadSelectors.
To preload a link as soon as it appears in the DOM, set an [up-preload="insert"] attribute.
This is useful for links with a high probability of being clicked, like a navigation menu:
<a href="/menu" up-layer="new drawer" up-preload="insert">≡ Menu</a> <!-- mark: up-preload="insert" -->
When an eagerly preloading link is inserted multiple times, only a single request is made. All subsequent render passes will render from the cache.
To "lazy preload" a link when it is scrolled into the viewport,
set an [up-preload="reveal"] attribute.
This is useful when a link is below the fold and is unlikely to be clicked until the user scrolls:
<a href="/stories/106" up-preload="reveal">Full story</a> <!-- mark: up-preload="reveal" -->
When a lazy preloading link enters and exits its viewport repeatedly, only a single request is made.
To programmatically preload any link element at a time of your choosing, use the up.link.preload() function.
The following compiler would preload a link with a [rel=next] attribute when
the user hovers over a <main> element:
up.compiler('link[rel=next]', (link) => {
return up.on('mouseover', 'main', () => up.link.preload(link))
})
A link that is preloaded in that fashion does not require an [up-preload] attribute.
To preload a given URL without a link element, make a background request that caches:
up.request('/menu', { cache: true, background: true })
The cached response will be used by any followed link to the same URL:
<a href="/menu" up-follow>Menu</a>
Requests sent when preloading differ in some ways from requests sent when following a link directly:
When a link is preloaded via the [up-preload] attribute or up.link.preload() function, an up:link:preload event
is emitted.
The event can be prevented to stop the preload from taking place:
function isSlowConnection() {
// https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation
return navigator.connection && navigator.connection.effectiveType.includes('2g')
}
up.on('up:link:preload', function(event) {
if (isSlowConnection()) {
event.preventDefault()
}
})
Tip Your coding agent can search these docs locally with the Unpoly agent skill