# Insert DNS prefetch (insert_dns_prefetch)

Adds link rel=dns-prefetch for third-party domains. The insert_dns_prefetch filter: what it does, its risks and the Apache, nginx and IIS directives.

Canonical URL: https://modpagespeed.com/docs/filters/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](https://modpagespeed.com/examples/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?](https://modpagespeed.com/docs/is-it-working/) has the steps.

## Configuration

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

**Apache**

```apache
ModPagespeedEnableFilters insert_dns_prefetch
```

**nginx**

```nginx
pagespeed EnableFilters insert_dns_prefetch;
```

**IIS (pagespeed.config)**

```text
pagespeed EnableFilters insert_dns_prefetch
```

```html
<!-- 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](https://modpagespeed.com/docs/worker-configuration/#preconnect-injection) transform. Toggleable via [`--no-preconnect-injection`](https://modpagespeed.com/docs/worker-configuration/#--no-preconnect-injection).

Scoping, `ForbidFilters` and the thresholds filters read: [Choosing filters](https://modpagespeed.com/docs/filter-selection/).

## Live example

See the before and after, with the source diff: [insert_dns_prefetch example](https://modpagespeed.com/examples/insert_dns_prefetch/).

## 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.

## Related filters

- [`add_base_tag`](https://modpagespeed.com/docs/filters/add_base_tag/): Add base tag
- [`add_head`](https://modpagespeed.com/docs/filters/add_head/): Add head
- [`add_ids`](https://modpagespeed.com/docs/filters/add_ids/): Add IDs
- [`add_instrumentation`](https://modpagespeed.com/docs/filters/add_instrumentation/): Add instrumentation
- [`collapse_whitespace`](https://modpagespeed.com/docs/filters/collapse_whitespace/): Collapse whitespace
- [`combine_heads`](https://modpagespeed.com/docs/filters/combine_heads/): Combine heads
- [`compute_statistics`](https://modpagespeed.com/docs/filters/compute_statistics/): Compute HTML statistics
- [`convert_meta_tags`](https://modpagespeed.com/docs/filters/convert_meta_tags/): Convert meta tags

This page is drawn from the [insert_dns_prefetch entry on HTML filters](https://modpagespeed.com/docs/html-filters/#insert_dns_prefetch). Every filter in one table: [PageSpeed filters](https://modpagespeed.com/docs/filters/).
