Skip to main content

filter · HTML filters

Insert DNS prefetch (insert_dns_prefetch)

Category:
HTML
CoreFilters:
No
OptimizeForBandwidth:
No
Risk:
Generally safe

insert_dns_prefetch adds connection warm-up hints for the origins of resources referenced in the body that the head does not already reference. A <link rel="dns-prefetch"> hint starts the DNS lookup early, while the browser is still busy with the HTML; a <link rel="preconnect"> hint goes further and opens the connection, TCP and for HTTPS also TLS, before the resource tag is even seen. Live demo: insert_dns_prefetch.

How it works

When it helps and when it does not

It helps when a page pulls from a few stable third-party origins, such as font CDNs or analytics hosts: the lookup and handshake then overlap with the HTML download instead of starting when the resource is discovered. It does nothing for same-origin resources, since the connection to the page’s own origin is already open. It also does little when the set of third-party domains churns between page views, because the hints are learned from earlier rewrites of the page and may name domains it no longer uses.

How it decides

The filter records which origins a page’s body resources come from, in order of first appearance. Hints are emitted once the number of hinted domains changes by at most two between rewrites; the check counts domains, so a page whose domains change but whose count holds still gets hints for the stored list. A page gets at most eight hints in total; the first two domains get preconnect, the rest dns-prefetch. Early views contribute data and get nothing; later views get the hints. Domains the author already hinted or referenced in the head are not duplicated.

When to use it

  • Not in CoreFilters: it runs only when you enable it by name.
  • Risk rating on these docs: Generally safe.

Risks

  • Every hint costs the browser work, and a preconnect costs an open connection held for an origin the visitor might not need; the built-in caps of eight hints, at most two of them preconnects, bound that overhead.

  • Verify with the X-Mod-Pagespeed response header and a ?PageSpeedFilters=-insert_dns_prefetch comparison; Is it working? has the steps.

Configuration

Enable it in the module configuration, at server, virtual-host or location scope:

Apache

ModPagespeedEnableFilters insert_dns_prefetch

nginx

pagespeed EnableFilters insert_dns_prefetch;

IIS (pagespeed.config)

pagespeed EnableFilters insert_dns_prefetch
<!-- inserted into <head> -->
<link rel="preconnect" href="https://fonts.examplecdn.com" />
<link rel="dns-prefetch" href="//analytics.example.com" />

On the worker

The worker runs its own pipeline, configured by flags. Its equivalent of this filter is the preconnect injection transform. Toggleable via --no-preconnect-injection.

Scoping, ForbidFilters and the thresholds filters read: Choosing filters .

Live example

Injects <link rel="dns-prefetch"> hints so the browser resolves DNS early.

This optimization works at the response-header or document-head hint level, so it leaves no change in the page body to diff; the example shows it live.

See the before and after, with the source diff →

Frequently asked questions

What does the insert_dns_prefetch filter do?
insert_dns_prefetch adds connection warm-up hints for the origins of resources referenced in the body that the head does not already reference. A <link rel="dns-prefetch"> hint starts the DNS lookup early, while the browser is still busy with the HTML; a <link rel="preconnect"> hint goes further and opens the connection, TCP and for HTTPS also TLS, before the resource tag is even seen. Live demo: insert_dns_prefetch.
Is insert_dns_prefetch enabled by default?
No. insert_dns_prefetch is not in CoreFilters, the default RewriteLevel; it runs only when you enable it by name with EnableFilters.
How do I enable insert_dns_prefetch on Apache and nginx?
Add ModPagespeedEnableFilters insert_dns_prefetch on Apache or pagespeed EnableFilters insert_dns_prefetch; on nginx. On IIS, add pagespeed EnableFilters insert_dns_prefetch to pagespeed.config.

This page is drawn from the insert_dns_prefetch entry on HTML filters. Every filter in one table: PageSpeed filters.

Search