Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node/Express serving image if exist; Caching issue

Node/Express serving image if exist; Caching issue

Problem

been using SO for a while but it's my first question here, as I couldn't find a solution for this particular problem. I'll try to be as explicit as possible, is anything is unclear just let me know so I update the question accordingly.

I'm using a route to serve a user's Photo according to the userId passed in the route as parameter (). It works, but each time the tag is encountered it request the image from the server and never uses a cached version.

I played with Cache-Control a bit, but it's either all or nothing: If caching is enabled and the user change his photo, it will still use the old photo.

My question is: is it possible to use a cached version if the photo hasn't changed, but if it did use the one from the server? I tried using "must-revalidate" along with "max-age" or "no-cache" to no avail.

Here's the route code if it helps:

Module.exports.getPhoto = function(req, res) {
    var mime = require('mime-magic'),
        memberId = req.params.memberId,
        imgUrl;

    path.exists('public/images/memberPhotos/' + memberId, function(exists) {
        if(exists) {
            imgUrl = 'public/images/memberPhotos/' + memberId;
        }else{
            imgUrl = 'public/images/memberPhotos/noPhoto.jpg';
        }

        fs.readFile(imgUrl, function(err, img) {
            mime.fileWrapper(imgUrl, function(err, mimeType) {
                if(!err) {
                    console.log(mimeType);
                    res.writeHead(200, {
                        'Content-Type': mimeType,
                        'Cache-Control': "max-age=" + 43800*60 + ", must-revalidate"
                    });
                    res.end(img, 'binary'); 
                }
            });

        });
    });
};

Thanks

Problem courtesy of: Olivier Clément

Solution

You want to use an ETag and If-None-Match headers. You will need to track your images ETags on the server and if the incoming If-None-Match matches the current ETag you respond to the request with a 304 - Not Modified. Otherwise you serve the image and include a new ETag in the headers.

See http://blog.phyber.com/supporting-cache-controls-in-nodejs for more details.

Solution courtesy of: Ryan Olds

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node/Express serving image if exist; Caching issue

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×