1

My Node.js app uses Express and my app has a route that sends a JSON file with Tweet data. I want it to cache for 20 seconds. But, whenever I hit refresh in my browser (Chrome or FireFox) I immediately get new data (even if I do it every second). Note that the data does change more than every 20 seconds, but I still want a 20 second cache.

Here is my route.

app.get('/tweet-stats.json', function(req, res) {
    res.set('Cache-Control', 'public, max-age=20');
    res.set('Expires', new Date(Date.now() + 20000));
    res.set('Last-Modified', new Date(Date.now()));
    res.set('Content-Type', 'application/json');
    res.send(publicTweetStatus());
});

Here are the request and response headers from FireFox (FireBug):

Response Headers  
HTTP/1.1 200 OK  
X-Powered-By: Express  
Cache-Control: public, max-age=20  
Expires: Fri May 10 2013 06:52:11 GMT+0000 (UTC)  
Last-Modified: Fri May 10 2013 06:51:51 GMT+0000 (UTC)  
Content-Type: application/json  
Content-Length: 209  
Connection: keep-alive  

Request Headers  
GET /tweet-stats.json HTTP/1.1  
Host: mydevelopmenturl.com  
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0  
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8  
Accept-Language: en-US,en;q=0.5  
Accept-Encoding: gzip, deflate  
Connection: keep-alive  

How can I make browsers cache this for 20 seconds before checking with the server again?

UPDATE

So, I tried robertklep's suggestion and it worked in some browsers/OSs and not others:

Ubuntu Chrome - No Cache!!!!!!!!!!!!
Ubuntu FireFox - No Cache!!!!!!!!!!!!!!!
Windows 7 - Chrome - Cache
Windows 7 - FireFox - Cache
Windows 7 - IE 9 - Cache
Windows 7 - Opera - No Cache!!!!!!!!!!!!!!!!!
iOS Safari - Cache
Mac OSX - Safari - Cache
Mac OSX - Chrome - Cache
Mac OSX - Firefox - Cache

Why the differences?

4

0 回答 0