Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cache-headers

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cache-headers

Generate browser and cdn cache header values

  • 1.0.0-hella.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
18
increased by350%
Maintainers
1
Weekly downloads
 
Created
Source

Cache Headers

Create cache headers as application-level or route-level middleware. This has only been tested as middleware for an express app. The primary cache header set is the Cache-Control header value. All time values are set as seconds per the w3 spec.

This package is developed using ES6 and transpiled with babel. It is also using the 1stdibs eslint rules.

Installation

$ npm install --save cache-headers

Tests

$ npm install
$ npm test

Usage

App-level middleware

const express = require('express');
const app = express();
const cache = require('cache-headers');
const cacheOptions = {
    paths: {
        '/**/generic': {
            maxAge: 'TEN_MINUTES',
            staleRevalidate: 'ONE_HOUR',
            staleError: 'ONE_HOUR'
        },
        '/short-cached/route': {
            maxAge: 60
        },
        '/user/route': false,
        '/**': {
            maxAge: 600
        }
    }
};

// some other middleware

app.use(cache.middleware(cacheOptions));

// rest of app setup

With the example above, the Cache-Control header is set as follows when a user hits these different site routes:

  • /**/generic (any route ending in generic): Cache-Control: max-age=600, stale-while-revalidate=3600, stale-if-error=3600
  • /cached/route: Cache-Control: max-age=60
  • /user/route: Cache-Control: no-cache, max-age=0
  • /** (any other route not listed): Cache-Control: max-age=600

Alternatively for no-cache, the following could be used:

'/user/route': {
    setNoCache: true
}

Router-level middleware

Taking the app-level setup above, you can additionally override the default paths initially set in the cacheOptions.

const express = require('express');
const router = express.Router();
const cache = require('cache-headers');
const cacheOptions = {
    cacheSettings: {
        "maxAge": 2000
    }
};

// app.use(cache.middleware(cacheOptions)) is loaded prior to this route, therefore running by default
// and any subsequent call to set the header is then overwritten

router.get('/endswith/generic', cache.middleware(cacheOptions), (req, res, next) => {
    // do route-y stuff
    next();
});

Rather than set the original headers defined in the paths config in the app-level setup (for the /**/generic path), this will output the following: Cache-Control: max-age=2000

API

cache.middleware (all properties optional)

{
    cacheSettings: {
        maxAge: number|string,
        staleRevalidate: number|string,
        staleError: number|string
    },
    paths: {
        '/glob/**/path': object|boolean=false
    }
}

The following are acceptable values to use if a string is passed in for cache values:

  • 'ONE_MINUTE'
  • 'TEN_MINUTES'
  • 'ONE_HOUR'
  • 'ONE_DAY'
  • 'ONE_WEEK'
  • 'ONE_MONTH'
  • 'ONE_YEAR'

If no options are passed in, the default value set is Cache-Control: max-age=600

Contributing

All code additions and bugfixes must be accompanied by unit tests. Tests are run with jest and written with the node assert module.

Acknowledgement

A portion of this code was taken from this cache-control package/repo.

Keywords

FAQs

Package last updated on 02 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc