Cache Parser is a minimal JavaScript/TypeScript library designed specifically for parsing and building the HTTP Cache-Control header. It provides the `parse()` function to convert a raw Cache-Control string into a structured object representing its directives (e.g., `maxAge`, `public`, `noCache`) and the `tokenize()` function to convert a directives object back into an array of tokens suitable for HTTP headers. The current stable version is 1.2.6. It is part of the `tinylibs` monorepo, suggesting a release cadence tied to other utilities within that project, with frequent patch updates as seen in recent changelogs. Its key differentiator lies in its focused, minimal approach to handling only the Cache-Control header, providing clear TSDoc comments for usage and strong type safety for TypeScript users. It targets both Node.js and browser environments, with ESM, CJS, and UMD distributions available.
npm install cache-parserVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to parse a raw 'Cache-Control' header string into an object and how to build a header string from a directives object using `parse` and `tokenize` respectively.
When accessing parsed properties, use a nullish coalescing operator (`??`) or explicit `typeof` checks if a default value or different logic is required for absent directives.
For new projects or ESM-only environments, always use `import { namedExport } from 'cache-parser';`. If you encounter issues in a mixed environment, check your `tsconfig.json` or bundler configuration for module resolution settings.Regularly consult the release notes and changelog for `cache-parser` on npm or GitHub to stay informed about any specific changes, even when updating other `tinylibs` packages.
Ensure you are using `import { parse } from 'cache-parser';`. If the issue persists, verify your bundler configuration to correctly handle ES Modules.Replace `const { parse, tokenize } = require('cache-parser');` with `import { parse, tokenize } from 'cache-parser';`.Verify the spelling of `CacheControl` and ensure you are using `import type { CacheControl } from 'cache-parser';`. If the problem persists, check your `cache-parser` package version to ensure it is up-to-date.No dependency data recorded yet.