dnsprefetch

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package dnsprefetch provides middleware that sets the X-DNS-Prefetch-Control response header, controlling whether browsers speculatively resolve the DNS names of links and resources on a page. It is the Go analogue of Helmet's dnsPrefetchControl middleware from the Node ecosystem, packaged as a drop-in express.Handler that writes the same header with the same "on"/"off" semantics.

Use this middleware to make a deliberate choice about DNS prefetching rather than leaving it to browser defaults. Prefetching improves perceived performance by resolving hostnames before the user clicks, but it also leaks a signal to DNS resolvers about which links a page contains, which can be a minor privacy concern for pages that reference sensitive third-party hosts. Turning it off (the default here) prioritizes privacy; turning it on prioritizes latency. Mount it with app.Use for a site-wide policy, or attach it to a specific router when only some pages warrant a different setting.

Operationally the middleware belongs anywhere before the response body is written. On each request it sets a single response header, X-DNS-Prefetch-Control, to a value computed once when New builds the handler, and then always calls next() so the request proceeds untouched. It reads no request headers and no request state; it is purely additive to the response and never short-circuits the chain.

The behavior is governed by Options.Allow. When Allow is true the header value is "on", enabling prefetching; when false — including the zero-value Options, which is usable — the value is "off", disabling it. New is variadic and accepts zero or one Options; calling New() with no arguments is equivalent to New(Options{}) and yields "off". There is no failure mode and no per-request branching beyond writing the precomputed value.

Compared with Helmet's dnsPrefetchControl, this port keeps the identical contract — one header, "on" or "off", defaulting to "off" — but is intentionally minimal: it exposes a single boolean rather than an options object, and it does not attempt to emit any related headers. It pairs well with the sibling security-header middlewares (such as csp and dnsprefetch's Helmet cousins) when composing a broader hardening layer.

Example

Example mounts the dnsprefetch middleware with prefetching enabled and shows the resulting response header. Passing Options{Allow: true} makes the middleware emit X-DNS-Prefetch-Control: on, opting into browser DNS prefetching for lower perceived latency; the default New() would emit off. The header is written on every response before the route handler runs, and the middleware always calls next, so the handler still produces its normal body. The example drives the app with httptest and prints the header value, which is fully deterministic. Switch Allow to false, or drop the option entirely, to prioritize privacy instead.

package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/malcolmston/express"
	"github.com/malcolmston/express/middleware/dnsprefetch"
)

func main() {
	app := express.New()
	app.Use(dnsprefetch.New(dnsprefetch.Options{Allow: true}))
	app.Get("/", func(req *express.Request, res *express.Response, next express.Next) {
		res.Send("ok")
	})

	rec := httptest.NewRecorder()
	app.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))

	fmt.Println(rec.Header().Get("X-DNS-Prefetch-Control"))
}
Output:
on

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Options) express.Handler

New returns middleware that sets the X-DNS-Prefetch-Control header.

Types

type Options

type Options struct {
	// Allow enables DNS prefetching ("on"). When false, prefetching is turned
	// "off".
	Allow bool
}

Options configures the dnsprefetch middleware. The zero value is usable and yields X-DNS-Prefetch-Control: off.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL