Link prefetching

What is prefetching?

Link prefetching is a strategy to improve the perceived performance of page loading.
We cannot improve on the explanation of prefetching provided by MDN Web Docs. They describe it thus:
Link prefetching is a browser mechanism, which utilizes browser idle time to download or prefetch documents that the user might visit in the near future. A web page provides a set of prefetching hints to the browser, and after the browser is finished loading the page, it begins silently prefetching specified documents and stores them in its cache. When the user visits one of the prefetched documents, it can be served up quickly out of the browser's cache.
Commonly documents and assets are prefetched using a <link> element containing a rel="prefetch" attribute. For example:
1
<link rel="prefetch" href="/images/my-pic.jpg">
Copied!
The rel="prefetch" attribute is a 'hint' to the browser that the specified URL should be prefetched. It may or may not be prefetched depending on whether the browser has sufficient idle time to do so before the user actually clicks the link.
This works well for traditional sites where each new URL involves an HTTP request and a page refresh. Performance is improved because prefetched documents and assets are already in the browser cache and so don't need to be fetched at the time that the link is clicked.
In addition to prefetch the rel attribute of the <link> element can take different values depending on the type of prefetching required:
    dns-prefetch
    preconnect
    prefetch
    subresource
    prerender
​This CSS-tricks article provides a comprehensive overview of these different prefetching options.
Care should be taken when implementing prefetching and consideration should be given to the likelihood that the document or asset will be needed in the near future, i.e. how likely it is that a particular link will be the next one to be clicked.

Implementing link prefetching strategies with Frontity

For JavaScript based SPA applications such as Frontity the situation is slightly different. No additional elements are needed in the markup, and the prefetching is performed with JavaScript. In such cases data is prefetched and stored in the application's "state".
We will describe two methods for implementing link prefetching in Frontity. The first will use the <Link /> component provided by Frontity. The second will demonstrate how to create your own component implementing link prefetching.
Frontity's <Link /> component supports prefetching. The prefetching strategy that it adopts is determined by the value of the property state.theme.autoPrefetch.
The possible values for state.theme.autoPrefetch are:
Value
Description
no
No auto prefetch.
hover
Prefetches links on hover.
in-view
Prefetch links currently visible in the viewport.
all
Prefetches all internal links on the page.
The value of state.theme.autoPrefetch could then be set in the frontity.settings.js file, for example:
1
const settings = {
2
// Other settings...
3
packages: [
4
{
5
name: "my-theme",
6
state: {
7
theme: {
8
autoPrefetch: "hover", // values: no | hover | in-view | all
9
},
10
},
11
},
12
// Other packages...
13
],
14
};
Copied!
A component that uses Frontity's <Link /> component should import it from @frontity/components:
1
import Link from "@frontity/components/link";
Copied!
Here is a short video demonstrating prefetching with Frontity's <Link> component:
Note that for links present in the content to benefit from the prefetching strategy of the <Link> component the content should be processed by the html2react package. See this page in our tutorial for a detailed example illustrating the steps that need to be taken.
If you wish to create your own link component that also implements auto-prefetching based on the value of a state.theme.autoPrefetch property then a possible implementation could look like this:
1
import React, { useEffect } from "react";
2
import { connect } from "frontity";
3
​
4
const Link = ({
5
state,
6
actions,
7
link,
8
className,
9
children,
10
rel,
11
"aria-current": ariaCurrent,
12
onClick: onClickProp,
13
}) => {
14
// Check if the link is an external or internal link
15
const isExternal = link.startsWith("http");
16
​
17
// Prefetch the link's content when it mounts and autoPreFetch is set to `true`
18
useEffect(() => {
19
if (!isExternal) {
20
if (state.theme.autoPrefetch === "all") actions.source.fetch(link);
21
}
22
}, []);
23
​
24
const onClick = (event) => {
25
// Do nothing if it's an external link
26
if (isExternal) return;
27
​
28
event.preventDefault();
29
// Set the router to the new url.
30
actions.router.set(link);
31
​
32
// Scroll the page to the top
33
window.scrollTo(0, 0);
34
​
35
// if the menu modal is open, close it so it doesn't block rendering
36
if (state.theme.isMobileMenuOpen) {
37
actions.theme.closeMobileMenu();
38
}
39
​
40
if (onClickProp) {
41
onClickProp(event);
42
}
43
};
44
​
45
return (
46
<a
47
// ref={ref}
48
href={link}
49
onClick={onClick}
50
className={className}
51
aria-current={ariaCurrent}
52
rel={isExternal ? "noopener noreferrer" : rel}
53
onMouseEnter={() => {
54
// Prefetch the link's content when the user hovers on the link
55
if (state.theme.autoPrefetch === "hover" && !isExternal)
56
actions.source.fetch(link);
57
}}
58
>
59
{children}
60
</a>
61
);
62
};
63
​
64
export default connect(Link);
Copied!
This uses useEffect to call actions.source.fetch(link) to fetch the data if the value of state.theme.autoPrefetch is "all", and if the value is "hover" then it calls actions.source.fetch(link) to fetch the data when the onMouseEnter event occurs.
Last modified 5mo ago