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-dyncache

v0.2.0

Published

Connect middleware to enable intelligent cache headers for dynamic content

Downloads

12

Readme

connect-dyncache

NodeJS is connect middleware to make it easy to add ETag or Last-Modified headers to your dyanmic resources to allow browsers to cache responses.

Example

var app = require('express').createServer();    

app.use(require('connect-dyncache')());

app.get('/page', function(req, res) {
  res.autoEtag();
  res.send("Some content that is generated based on state");
});

app.get('/page2', function(req, res) {
    res.setEtag("Some well-known value");
    res.send("Some large content that we have a pre-defined etag we can use for (such as a revision id or something)");
});

app.get('/page3', function(req, res) {
    res.setLastModifiedDate(new Date("Thu Feb 21 2013 21:46:00"));
    res.send("Some large content generated by something that we can track with a modified date, such as a db record");
});

... and on the other side of the TCP connection ...

HTTP/1.0 304 Not Modified
Content-Length: 0

Why

ETag headers basically give the client something to identify this version of the document with. Using it with dynamic data like this all the processing still happens on the server, but you can save a lot of bandwidth (and speed up the client experience) by supporting ETag or Last-Modified date caching.

This middeware adds three methods for caching; you can use res.autoEtag() anywhere before the first data is sent to the client to have it generate an etag using an md5 hash of your page. If you have some identifier that can be used to uniquely identify this version of the page you can set that using res.setEtag(etag), and if you have a modified date that you can use you can set that with res.setLastModifiedDate().

res.autoEtag()

Call this function to have the response object automatically calculate the md5 sum of your response and use that as the ETag. This is probably the easiest method to use.

res.setEtag()

If you already have something that is a valid etag (something that will absolutely change if the document ever changes) then you can use it here in this call to setEtag. If the ETag provided by the page matches this one then this method will return false. If it does, you can stop processing and just call res.end().

res.setLastModifiedDate(date)

If you know when the last time the current page was modified, such as if the core database table the page is generated from has a modified date on it, you can use that here either in place of or in addition to the ETag. If the client already has a cached copy of the page based on the date they provided, this method will return false. If it does, you can stop processing and just call res.end().

see also...

...cachify - middleware to help with caching static resources. ...etagify - a different approach to etag caching for pages that don't change during the lifetime of the process.

Help out

I created this because I needed it; feel free to extend it with examples, fix bugs, and add functionality through pull requests!

License

Copyright (c) 2013, Richard Bateman <[email protected]>

You can use this however you want. I disavow responsibility for
*anything* that occurs as a result of using this, whether good
or bad, including but not limited to: Correct caching behavior,
incorrect caching behavior, promotion and/or positive change of
employment, downsizing and/or negative change of employment,
terrorism, philanthropism, sleep deprivation, sleep apnia,
excessive spam, marital problems, weddings, funerals, unexpected
lottery winnings, uncomfortable social situations, new facebook
friends, increase in website speed, decrease in website speed,
groupies, guppies, puppies, or loss of sales.

Use at your own risk.