npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

fastify-cache-control

v1.0.0

Published

Declarative HTTP Cache-Control headers management for Fastify with CDN support, presets, and pattern matching

Downloads

92

Readme

fastify-cache-control

Declarative HTTP Cache-Control headers management for Fastify with CDN support, presets, and pattern matching.

npm version License: MIT

Features

  • Declarative Cache-Control header management
  • Built-in presets for common scenarios (static, api, realtime, etc.)
  • Pattern matching rules for route-based caching
  • CDN-Cache-Control support (RFC 9213)
  • Automatic Vary header generation
  • Full TypeScript support
  • Zero dependencies (only fastify-plugin)

Requirements

  • Node.js >= 20.0.0
  • Fastify >= 5.0.0

Install

npm install fastify-cache-control

Quick Start

import Fastify from 'fastify'
import cacheControl from 'fastify-cache-control'

const fastify = Fastify()

await fastify.register(cacheControl, {
  defaults: { public: true, maxAge: 3600 }
})

fastify.get('/', async () => {
  return { hello: 'world' }
})
// Response: Cache-Control: public, max-age=3600

Options

interface FastifyCacheControlOptions {
  // Default directives for all responses
  defaults?: CacheControlDirectives | CachePreset

  // Status codes to apply defaults (default: [200, 201, 204, 206, 301-308])
  statusCodes?: number[]

  // HTTP methods to apply defaults (default: ['GET', 'HEAD'])
  methods?: string[]

  // Pattern-based caching rules
  rules?: CacheRule[]

  // CDN-specific cache control (RFC 9213)
  cdn?: {
    directives?: CacheControlDirectives
    header?: string  // default: 'CDN-Cache-Control'
  }

  // Auto-set Vary header for private/no-cache fields (default: true)
  autoVary?: boolean

  // Disable in development (default: false)
  disableInDevelopment?: boolean

  // Enable debug logging (default: false)
  debug?: boolean
}

Route Configuration

// Using directives object
fastify.get('/static', {
  config: {
    cache: {
      public: true,
      maxAge: 86400,
      immutable: true
    }
  }
}, handler)

// Using preset
fastify.get('/assets', {
  config: { cache: 'static' }
}, handler)

// Disable caching
fastify.get('/private', {
  config: { cache: false }  // Sets no-store
}, handler)

Programmatic API

fastify.get('/dynamic', async (request, reply) => {
  // Full control
  reply.cacheControl({
    private: true,
    maxAge: 60,
    mustRevalidate: true
  })
  return data
})

// Shortcuts
fastify.get('/secret', async (request, reply) => {
  reply.noCache()  // no-store
  return sensitiveData
})

fastify.get('/versioned', async (request, reply) => {
  reply.immutable(31536000)  // public, max-age=31536000, immutable
  return asset
})

fastify.get('/api', async (request, reply) => {
  reply.cachePreset('api')  // private, no-cache
  return apiData
})

Built-in Presets

| Preset | Directives | Use Case | |--------|------------|----------| | static | public, max-age=31536000, immutable | Fingerprinted static assets | | api | private, no-cache | API responses requiring revalidation | | realtime | no-store | Real-time data, never cache | | page | public, max-age=3600, must-revalidate | Public pages | | private | private, max-age=60, must-revalidate | User-specific data |

Pattern Matching Rules

Apply caching rules based on URL patterns:

await fastify.register(cacheControl, {
  rules: [
    // String pattern with wildcard
    { match: '/static/*', cache: 'static' },

    // Regex pattern
    { match: /^\/api\/public\//, cache: { public: true, maxAge: 300 } },

    // Function matcher
    { match: (req) => req.url.includes('admin'), cache: false },

    // Catch-all for API
    { match: '/api/*', cache: 'api' }
  ]
})

Rules are evaluated in order; first match wins.

CDN-Cache-Control (RFC 9213)

Set different cache policies for CDNs vs browsers:

await fastify.register(cacheControl, {
  defaults: { public: true, maxAge: 60 },  // Browser: 60s
  cdn: {
    directives: { maxAge: 600 },            // CDN: 600s
    header: 'CDN-Cache-Control'             // or 'Cloudflare-CDN-Cache-Control'
  }
})

Response headers:

Cache-Control: public, max-age=60
CDN-Cache-Control: max-age=600

Automatic Vary Header

When using private or no-cache with field names, Vary is automatically set:

reply.cacheControl({ private: ['cookie', 'authorization'] })
// Cache-Control: private="cookie, authorization"
// Vary: Cookie, Authorization

Disable with autoVary: false.

Priority

Headers are applied in this order (highest to lowest):

  1. reply.cacheControl() / reply.noCache() / etc.
  2. Route config.cache
  3. Pattern matching rules
  4. Global defaults

Manually set Cache-Control headers are never overwritten.

Directives Reference

| Directive | Type | Header Output | |-----------|------|---------------| | public | boolean | public | | private | boolean \| string[] | private or private="field1, field2" | | noCache | boolean \| string[] | no-cache or no-cache="field" | | noStore | boolean | no-store | | noTransform | boolean | no-transform | | mustRevalidate | boolean | must-revalidate | | proxyRevalidate | boolean | proxy-revalidate | | mustUnderstand | boolean | must-understand | | immutable | boolean | immutable | | maxAge | number | max-age=N | | sMaxage | number | s-maxage=N | | staleWhileRevalidate | number | stale-while-revalidate=N | | staleIfError | number | stale-if-error=N |

Error Handling

The plugin throws FastifyCacheControlError for:

  • CACHE_CONTROL_CONFLICT: Using public and private together
  • CACHE_CONTROL_INVALID_VALUE: Negative time values
  • CACHE_CONTROL_INVALID_PRESET: Unknown preset name

TypeScript

Full TypeScript support with module augmentation:

import type { CacheControlDirectives, CachePreset } from 'fastify-cache-control'

// reply.cacheControl() and reply.noCache() are typed
// config.cache is typed in route options

License

MIT