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 🙏

© 2024 – Pkg Stats / Ryan Hefner

connect-cachify-static

v4.0.0

Published

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

Downloads

224

Readme

NPM version Build Status Dependency Status

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).

If you reference cachifieable resources from CSS files you probably also want to use postcss-cachify.

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
  • format - function that creates cachified version of the URL - allowed values are 'path', 'name', or the function that takes path and hash and creates cachified version of the file URL

API

cachify function is injected in res.locals and thus can be accessed from the template code. cachify and filter can be also retrieved by calling await helpers() on the initialized middleware instance

cachify(path, integrity)

  • path - URL of the resource to be cachified
  • integrity - optional - if truthy cachify will generate a tuple { path, integrity }, which can be used to format <script> and <link> elements with subresource integrity support

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 cachified version incorporating reasonably unique {prefix} generated based on the file content.

The specific format of the cachified version depends on the format parameter:

  • path default

    /path/to/the/file -> /{prefix}/path/to/the/file

  • name

    /path/to/the/file -> /path/to/the/{prefix}- file

You can also pass a format function:

// this is how default format is implemented
function format(path, prefix) {
  return '/' + prefix + path;
}

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)

  //- scripts with SRI support
  - var c = cachify('/script/main.js', true)
  script(src=c.path, integrity=c.integrity, crossorigin='anonymous', defer)

filter(patter)

returns an array of cachified paths matching pattern

Usage

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

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

License

MIT