Boost WordPress Loading Speed with Preload, Prefetch, and Preconnect Hints

Resource hints in WordPress: preconnect, preload and prefetch

The browser finds most resources late. It reads the HTML, then the CSS, and only inside the CSS it finds the @font-face rule that points to the font file. A third-party script on another domain first needs a DNS lookup, a TCP connection and a TLS handshake. Resource hints tell the browser about these things earlier, so the waiting happens in parallel instead of one step after another.

Problem: The waterfall of a WordPress page shows a chain: HTML, then the stylesheet, then the font file, and the text appears 400 ms later than it could. A map script on another domain starts with a connection setup that costs another 150 ms on mobile.

Solution: Add preconnect for the few third-party origins the page really uses with the wp_resource_hints filter, preload the one font that the first screen needs with the wp_preload_resources filter (WordPress 6.1+), and use prefetch only for files that the next page will most likely need.

The four hints

  • dns-prefetch: only the DNS lookup for another origin. Cheap, useful for origins that are used later on the page.
  • preconnect: DNS, TCP and TLS. Worth it for 2–3 origins that are needed early; every open connection costs the browser something, so a list of ten is counterproductive.
  • preload: download this file now with high priority, it is needed for the current page. Typical cases: the main font, the LCP image when it is a CSS background.
  • prefetch: download this file with low priority for a future navigation.

preconnect and dns-prefetch with wp_resource_hints

WordPress already prints resource hints in wp_head (for example dns-prefetch for the emoji CDN) through wp_resource_hints(). Adding our origins to the same filter keeps them in one place and avoids duplicates.

add_filter( 'wp_resource_hints', 'ha_resource_hints', 10, 2 );
function ha_resource_hints( array $urls, string $relation_type ): array {
    if ( 'preconnect' === $relation_type ) {
        // Google Fonts serves the files from a second origin, the files need crossorigin.
        $urls[] = array(
            'href'        => 'https://fonts.gstatic.com',
            'crossorigin' => 'anonymous',
        );
        if ( is_page( 'contact' ) ) {
            $urls[] = 'https://maps.googleapis.com';
        }
    }

    if ( 'dns-prefetch' === $relation_type ) {
        $urls[] = 'https://www.googletagmanager.com';
    }

    return $urls;
}

The map origin gets a preconnect only on the page that shows the map. A connection that is opened and not used within about ten seconds is closed again by the browser, and the work is wasted.

preload the font with wp_preload_resources

Since WordPress 6.1 the wp_preload_resources filter prints <link rel="preload"> tags. Fonts are always loaded in CORS mode, so a font preload needs crossorigin even when the file is on the same domain; without it the browser downloads the font twice.

add_filter( 'wp_preload_resources', 'ha_preload_resources' );
function ha_preload_resources( array $resources ): array {
    $resources[] = array(
        'href'        => get_theme_file_uri( 'fonts/inter-var.woff2' ),
        'as'          => 'font',
        'type'        => 'font/woff2',
        'crossorigin' => 'anonymous',
    );
    return $resources;
}

Preload only one or two fonts: the regular weight of the body text, maybe the heading font. Every preloaded file competes with the CSS and the LCP image for bandwidth at the most important moment. For an <img> in the content, preload is not needed: fetchpriority="high" on the image does the job, see Optimize Core Web Vitals in WordPress.

prefetch for the next page

add_filter( 'wp_resource_hints', 'ha_prefetch_single_script', 10, 2 );
function ha_prefetch_single_script( array $urls, string $relation_type ): array {
    if ( 'prefetch' === $relation_type && ( is_home() || is_archive() ) ) {
        $urls[] = get_theme_file_uri( 'js/single-post.min.js' );
    }
    return $urls;
}

On the blog index, the script of the single post template is fetched in idle time and comes from the cache when the visitor opens a post. Use the same URL with the same ?ver= as the enqueued script, otherwise the cached file does not match.

Check the result

Open DevTools, tab Network, reload with the cache disabled and look at the Initiator and Priority columns: the font should start right after the HTML, with priority High, and appear only once. The Console warns when a preloaded file was not used within a few seconds of the load event; that warning means the preload is wrong and should be removed.

NOTE: Older articles recommend HTTP/2 Server Push with a Link header in .htaccess. Chrome removed support for it in 2022 (version 106), so those headers do nothing for most visitors. The replacement for pushing early is the 103 Early Hints response, which some CDNs (for example Cloudflare) can send from Link: rel=preload headers. How to load the scripts and styles themselves without blocking: defer JavaScript and CSS loading in WordPress.

Update (2025): WordPress 6.8 adds speculative loading to core: links to other pages of the site are prefetched when the visitor is about to click them, using the Speculation Rules API. Manual prefetch hints for whole pages are not needed there anymore.