The background-image I'm serving is fairly large and I'd like to have it cached permanently. I figure I should send a cache header with a max age of infinity. How can I do this in a clean and proper way. I'm using express (out of the box) and don't have any caching modules.
You can setup a middleware handler that can match the path of your static background image and then set a cache-control header. This will set the cache-control header for your image but not for other static resources.
app.configure(function(){
app.use(function(req, res, next) {
var matchUrl = '/background.jpg';
if(req.url.substring(0, matchUrl.length) === matchUrl) {
res.setHeader("Cache-Control", "max-age=31556926");
}
return next();
});
app.use(express.static(path.join(application_root, "StaticPages")));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
The order is important. Your middleware function needs to fall in line before the express.static handler.
I think a much better approach would be to shrink the image down and then use a program like Diet JPEG (OS X) to minimize the file size.