HTTP Vary Header Manipulator
`vary` is a focused Node.js utility library designed for manipulating the HTTP `Vary` response header. It provides a simple API to append fields to the `Vary` header of a Node.js `http.ServerResponse` object or directly to an existing `Vary` header string. Currently at version 1.1.2, the package maintains a stable and slow release cadence, primarily focusing on performance improvements and input validation rather than frequent feature additions. Its key differentiator is its singular purpose, offering a reliable and minimal solution for ensuring correct cache behavior when content varies based on request headers, without introducing additional HTTP framework overhead. It is a foundational utility used by many HTTP servers and middleware in the Node.js ecosystem.
Common errors
-
TypeError: vary is not a function
cause Attempting to use `import vary from 'vary'` in an ES Module context or when `esModuleInterop` is not enabled in TypeScript.fixChange the import statement to `const vary = require('vary')`. -
Error: Invalid field name
cause Passing a non-string, empty string, or syntactically invalid HTTP field name to `vary(res, field)` or `vary.append(header, field)`.fixEnsure the `field` argument is a non-empty string representing a valid HTTP header field name (e.g., 'User-Agent', 'Accept-Encoding'). Refer to RFC 7231 for valid field name syntax.
Warnings
- breaking Starting with v1.1.0, the `vary` function strictly enforces valid HTTP field names. If you pass an invalid or malformed field name, it will now be rejected, whereas previous versions might have silently ignored or attempted to normalize it, potentially leading to a valid but unintended header value.
- gotcha The `vary` package is a CommonJS module and does not natively support ES Modules (`import`/`export`) syntax. Attempting to import it using `import vary from 'vary'` will likely result in a runtime error.
- gotcha While `vary` can accept a string of multiple fields (e.g., `'Accept, User-Agent'`) or an array of fields, it's crucial to understand that it appends and deduplicates. If you provide a field already present, it maintains its position rather than moving it to the end.
Install
-
npm install vary -
yarn add vary -
pnpm add vary
Imports
- vary
import vary from 'vary'
const vary = require('vary') - vary.append
import { append } from 'vary'const vary = require('vary'); vary.append(header, field)
Quickstart
const http = require('http');
const vary = require('vary');
http.createServer(function onRequest (req, res) {
// About to user-agent sniff, so add User-Agent to Vary header
vary(res, 'User-Agent');
const ua = req.headers['user-agent'] || '';
const isMobile = /mobi|android|touch|mini/i.test(ua);
// Serve site, depending on isMobile
res.setHeader('Content-Type', 'text/html');
res.end('You are (probably) ' + (isMobile ? '' : 'not ') + 'a mobile user');
}).listen(3000, () => {
console.log('Server listening on http://localhost:3000');
});
// Example of using vary.append directly on a string
const initialVaryHeader = 'Accept, Accept-Encoding';
const newVaryHeader = vary.append(initialVaryHeader, 'Origin');
console.log('Appended Vary header:', newVaryHeader); // 'Accept, Accept-Encoding, Origin'