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.
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.
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.
'deny' Send a 403 for any request for a dotfile.
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 ...
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') })
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.
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.
@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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With