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-Pagespeedresponse header and a?PageSpeedFilters=-insert_dns_prefetchcomparison; Is it working? has the steps.
Configuration
Enable it in the module configuration, at server, virtual-host or location scope:
Apache
ModPagespeedEnableFilters insert_dns_prefetchnginx
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.
Frequently asked questions
- What does the insert_dns_prefetch filter do?
insert_dns_prefetchadds 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_prefetchis not in CoreFilters, the default RewriteLevel; it runs only when you enable it by name withEnableFilters. - How do I enable insert_dns_prefetch on Apache and nginx?
- Add
ModPagespeedEnableFilters insert_dns_prefetchon Apache orpagespeed EnableFilters insert_dns_prefetch;on nginx. On IIS, addpagespeed EnableFilters insert_dns_prefetchto pagespeed.config.
Related filters
- add_base_tag — Add base tag
- add_head — Add head
- add_ids — Add IDs
- add_instrumentation — Add instrumentation
- collapse_whitespace — Collapse whitespace
- combine_heads — Combine heads
- compute_statistics — Compute HTML statistics
- convert_meta_tags — Convert meta tags
This page is drawn from the insert_dns_prefetch entry on HTML filters. Every filter in one table: PageSpeed filters.