DNS Prefetch Control Middleware for Express

0.3.0 · abandoned · verified Wed Apr 22

The `dns-prefetch-control` package provides an Express.js middleware to manage the `X-DNS-Prefetch-Control` HTTP response header. This header influences whether browsers perform DNS prefetching, an optimization where browsers proactively resolve domain names for links and resources that a user might access, potentially reducing perceived latency. The current stable version of this standalone package is 0.3.0, last published seven years ago. While simple and focused, its primary differentiator was its integration into the broader Helmet.js security suite. Modern usage of this functionality is typically through the `helmet` package itself (e.g., `helmet({ xDnsPrefetchControl: { allow: true } })`), as the standalone `dns-prefetch-control` repository has been archived, indicating it's no longer actively maintained as a separate entity. Disabling DNS prefetching, which is the default for security-focused middleware like Helmet.js, enhances user privacy by preventing speculative DNS lookups that could reveal browsing patterns, albeit at a potential slight performance cost.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting the X-DNS-Prefetch-Control header using the middleware, defaulting to 'off', and notes the modern Helmet.js integration.

import express from 'express';
import dnsPrefetchControl from 'dns-prefetch-control';
// For modern usage, consider importing helmet directly:
// import helmet from 'helmet';

const app = express();
const PORT = process.env.PORT || 3000;

// Use dns-prefetch-control to disable DNS prefetching (default and recommended for privacy)
app.use(dnsPrefetchControl());

// Example of explicitly allowing DNS prefetching (potential privacy trade-off, slight performance gain)
// app.use(dnsPrefetchControl({ allow: true }));

// If using Helmet.js, you would configure it like this (and omit the standalone middleware):
// app.use(helmet({
//   xDnsPrefetchControl: { allow: false } // or true
// }));

app.get('/', (req, res) => {
  res.send('<h1>DNS Prefetch Control Example</h1><p>Check your network headers for X-DNS-Prefetch-Control.</p>');
});

app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
  console.log('X-DNS-Prefetch-Control header should be set.');
});

view raw JSON →