Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express static server cache control with max-age=0,must-revalidate

I have setup a simple static server using express.

var location = path.join(__dirname, 'public');
app.use(express.static(location, { cacheControl: true, setHeaders: function(res, path) { 
    res.setHeader("Cache-Control","max-age=0,must-revalidate");  
} }));

The request header is sent with If-None-Match and If-Modififed-Since and I can also see 304 Not Modified in the response in Chrome if I reload the page without modifying the files. And I get a 200 OK if I modify one of the files.

But why is my Chrome network tab showing the size of the file downloaded instead of saying (from memory cache) when the status code is 304 Not Modified ?

I was expecting the files to be loaded from cache if its not modified and served up from the server if modified.

Appreciate any help and guidance.

like image 703
anivas Avatar asked Jun 17 '17 18:06

anivas


People also ask

What's the difference between cache-control max age 0 and no-cache?

When max-age=0 is used, the browser will use the last version when viewing a resource on a back/forward press. If no-cache is used, the resource will be refetched.

What is serveStatic?

serveStatic(root, options) Create a new middleware function to serve files from within a given root directory. The file to serve will be determined by combining req. url with the provided root directory.

When working with Dotfiles in Express js the option deny responds with which error code?

'deny' Send a 403 for any request for a dotfile.

What does must-revalidate mean in Cache-Control?

Cache-Control "max-age=3600, must-revalidate". it is telling both client caches and proxy caches that once the content is stale (older than 3600 seconds) they must revalidate at the origin server before they can serve the content. This should be the default behavior of caching systems, but the must-revalidate directive makes this requirement ...

How to set the max age of cache-control?

The simplest middleware to set cache-controlwould be to set the cache header for the entire application as follows: app.use(function(req, res, next) { res.set('Cache-control', 'public, max-age=300') })

What is the difference between no-cache and Max-age=0 and must-revalidate?

no-cache and max-age=0, must-revalidate indicates same meaning. Clients can cache a resource but must revalidate each time before using it. This means HTTP request occurs each time though, it can skip downloading HTTP body if the content is valid.

Does express consider the cache stale when reloading Safari?

As you said, Safari sends Cache-Control: max-age=0 on reload. Express (or more specifically, Express's dependency, node-fresh) considers the cache stale when Cache-Control: no-cache headers are received, but it doesn't do the same for Cache-Control: max-age=0. From what I can tell, it probably should.


2 Answers

@sBanda described the situation very well. Receiving a 304 is expected since the specified cache-control policy states the file as stale, yet the ETag check shows it hasn't change. You get the 304 because you could have not requested the specific resource, yet you did, wasting bandwidth and cpu load.

What you should do to avoid it is something like this:

const express       = require('express');


const server        = express();
const oneHour       = 3600000;    // 3600000msec == 1hour

server.use(express.static('www', { maxAge: oneHour })); // Client-side file caching

server.get('/', function(req, res) {
  res.sendFile(__dirname + '/index.html');
});

server.listen(8080);

Source

like image 61
maninak Avatar answered Nov 07 '22 08:11

maninak


When the browser puts something in its cache, it also stores the Last-Modified or ETag header from the server. This tag is then used to send a request with the If-Modified-Since or If-None-Match header witch in effect tells server to send 304 if the content still has that ETag.

In your case Chrome is doing a request to ask the server should it use its cache there is an old post explaining this here.

You might want to check out this website about increasing application performance via HTTP Cache Headers.

like image 31
sBanda Avatar answered Nov 07 '22 07:11

sBanda