HTTP Cache-Control Header Parser and Formatter (ESM)

1.0.0 · active · verified Wed Apr 22

cache-control-esm is a JavaScript library specifically designed for parsing and formatting HTTP Cache-Control headers in strict adherence to RFC 7234. Currently at version 1.0.0, this package is a specialized fork of the `@tusbar/cache-control` library. Its primary differentiator is its explicit use of ECMAScript modules (`export default` and named exports), making it well-suited for modern bundlers and environments, especially within projects like `axios-cache-adapter`. Unlike other solutions that may bundle Express/Connect-like middleware or extensive interpretation logic, `cache-control-esm` maintains a narrow focus solely on the low-level parsing of header strings into a `CacheControl` object and formatting such objects back into valid HTTP header values. This focused approach ensures a lightweight and reliable tool for precise Cache-Control header manipulation without unnecessary overhead. Its release cadence is typically reactive to the needs of its consuming projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting a Cache-Control header using `format` and parsing one using `parse`.

import { format, parse } from '@rascarlito/cache-control';

// Simulate a response object
const res = {
  _headers: {},
  setHeader(name, value) {
    this._headers[name] = value;
  },
  getHeader(name) {
    return this._headers[name];
  }
};

// Set a Cache-Control header using the format function
res.setHeader('Cache-Control', format({
  public: true,
  immutable: true,
  maxAge: 31536000
}));

console.log('Set Cache-Control header:', res.getHeader('Cache-Control'));

// Parse a Cache-Control header from a string
const parsedHeader = parse('max-age=604800, public, no-transform');
console.log('Parsed Cache-Control object:', parsedHeader);

view raw JSON →