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

cache-immutable

v1.0.2

Published

set HTTP response headers to cache for a long time

Downloads

3,166

Readme

var assert = require('assert')
var http = require('http')

var cacheImmutable = require('cache-immutable')

var receivedResponse = false

http.createServer()
.on('request', function (request, response) {
  // Call the function with a Node.js http.ServerResponse argument.
  cacheImmutable(response)
  response.end()
})
.listen(0 /* random high port */, function () {
  var server = this
  var port = server.address().port
  http.request({port: port})
  .on('response', function (response) {
    var headers = response.headers
    // RFC 2616 (HTTP 1.1), section 14.21
    assert.equal(headers['cache-control'], 'max-age=31536000')
    // 31536000
    // = 365 * 24 * 60 * 60
    // ~= a (non-leap) year's worth of seconds
    // HTTP 1.0 headers.  See note below.
    assert.equal('expires' in headers, false)
    assert.equal('pragma' in headers, false)
    receivedResponse = true
    server.close()
  })
  .end()
})

process.on('exit', function () {
  assert.equal(receivedResponse, true)
  console.log('Tests passed')
})

HTTP 1.0 defines the Expires header, and HTTP 1.1 (RFC 2616 section 14.21) mentions:

To mark a response as "never expires," an origin server sends an Expires date approximately one year from the time the response is sent. HTTP/1.1 servers SHOULD NOT send Expires dates more than one year in the future.

Cache-Control is much simpler; there are all kinds of quirks to deal with generating and parsing the "HTTP-date" format for Expires, while max-age is just a number of seconds.

Fortunately, section 14.9.3 clarifies:

Furthermore If a response includes both an Expires header and a max-age directive, the max-age directive overrides the Expires header, even if the Expires header is more restrictive. This rule allows an origin server to provide, for a given response, a longer expiration time to an HTTP/1.1 (or later) cache than to an HTTP/1.0 cache.

Fortunately, many "HTTP 1.0" clients actually support Cache-Control, like nearly all recent clients. Mark Nottingham notes that's actually legit under RFC 2145. Section 2.2 says:

For example, an HTTP/1.1 server may send a "Cache-control" header to an HTTP/1.0 client; this may be useful if the immediate recipient is an HTTP/1.0 proxy, but the ultimate recipient is an HTTP/1.1 client.