connect-dyncache
v0.2.0
Published
Connect middleware to enable intelligent cache headers for dynamic content
Downloads
10
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.