connect-cachify-static

static (simpler and faster) variant of connect-cachify middleware

npm install connect-cachify-static
10 downloads in the last month

Build Status Dependency Status NPM version

connect-cachify-static

static (simpler and faster) variant of connect-cachify middleware

Adds Cache-Control: max-age=31536000 header to all requests with 'cachified' prefix. Prefix is based on the file content and calculated for all files during application startup (which means that it won't handle dynamically generated files).

Installation

$ npm install connect-cachify-static

Options

match - regular expression that determines which files in root will be cachified, if omitted the usual suspects are included .js, .css, .png, .jpg, and .gif control_headers - if truthy, the middleware will strip ETag and Last-Modified headers from the response

API

cachify(path)

path URL of the resource to be cachified

It should be called when generating HTML, CSS etc., in order to create a 'cachified' URL for the resource. cachify will replace /path/to/the/file with /{prefix}/path/to/the/file where {prefix} is a reasonably unique identifier generated based on the file content.

Since using cachify will make the browsers to cache the resource for approximately 1 year we need to bust the cache whenever the resource content changes.

head
  //- styles
  link(rel="stylesheet", href=cachify('/css/style.css'), media="screen")
  link(rel="stylesheet", href=cachify('/css/print.css'), media="print")
body
  // can be used to pass cachified URLs to client scripts
  #info(data-icon=cachify('/img/icon.png'))
  //- scripts
  script(src=cachify('/script/main.js'), defer)

If you use cachify with express the cachify method is added to res.locals and thus available directly in the views.

Usage

var connect = require('connect');
var cachifyStatic = require('connect-cachify-static');

connect()
  .use(cachifyStatic(__dirname + '/public'), {
    match: /\.js$/, // only javascript files
    control_headers // strip E
  })
// need static to actually server the file
connect()
  .use(connect.static(__dirname + '/public'))

License

MIT

npm loves you