GithubHelp home page GithubHelp logo

net-a-porter / connect-cache-control-middleware Goto Github PK

View Code? Open in Web Editor NEW
0.0 5.0 1.0 12 KB

A Connect/Express middleware for consistently setting Cache-Control headers

JavaScript 96.62% Shell 3.38%

connect-cache-control-middleware's Introduction

Connect Cache Control Middleware

Build status npm

A Connect/Express middleware for consistently setting Cache-Control headers

Installing

npm i --save connect-cache-control-middleware

Usage

var cacheControlMiddleware = require('connect-cache-control-middleware');

// creates a middleware instance that prevents caching
var preventCaching = cacheControlMiddleware.create();

// creates a middleware instance that caches routes
var allowCaching = cacheControlMiddleware.create({
	maxAge: 600,
	staleWhileRevalidate: 86400
});

app.get('/dont-cache-me', [preventCaching], someHandlerFunction);
app.use(allowCaching);

API

cacheControlMiddleware.create([cacheControlConfig])

Returns a new instance of the middleware handler which can be passed to Connect.

If cacheControlConfig is omitted, then the middleware sets the following headers on each request:

  • Cache-Control: 'no-cache, no-store, must-revalidate'
  • User-Cache-Control: 'no-cache, no-store, must-revalidate'
  • Pragma: 'no-cache'
  • Expires: '0'

Otherwise, it will set Cache-Control and User-Cache-Control to:

  • max-age=<cacheControlConfig.maxAge>
  • stale-while-revalidate=<cacheControlConfig.staleWhileRevalidate>

cacheControlMiddleware.buildCacheControlHeader(cacheControlConfig)

A helper function, used internally, to consistently build Cache-Control headers. This is exposed publicly if one requires to set headers outside of the middleware chain e.g. express-remote-handlebars.

As with the create method, cacheControlConfig should be an object exposing respective maxAge and staleWhileRevalidate properties.

This method returns a String.

Unit tests

  • npm i
  • npm test

Releasing

  • Bump version number in package.json (semver please!)
  • git tag release-<version number>
  • git push origin --tags

connect-cache-control-middleware's People

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

jamesseanwright

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.