Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS/express: Cache and 304 status code

When I reload a website made with express, I get a blank page with Safari (not with Chrome) because the NodeJS server sends me a 304 status code.

How to solve this?

Of course, this could also be just a problem of Safari, but actually it works on all other websites fine, so it has to be a problem on my NodeJS server, too.

To generate the pages, I'm using Jade with res.render.

Update: It seems like this problem occurs because Safari sends 'cache-control': 'max-age=0' on reload.

Update 2: I now have a workaround, but is there a better solution? Workaround:

app.get('/:language(' + content.languageSelector + ')/:page', function (req, res) {     // Disable caching for content files     res.header("Cache-Control", "no-cache, no-store, must-revalidate");     res.header("Pragma", "no-cache");     res.header("Expires", 0);      // rendering stuff here… } 

Update 3: So the complete code part is currently:

app.get('/:language(' + content.languageSelector + ')/:page', pageHandle);  function pageHandle (req, res) {     var language = req.params.language;     var thisPage = content.getPage(req.params.page, language);      if (thisPage)     {         // Disable caching for content files         res.header("Cache-Control", "no-cache, no-store, must-revalidate");         res.header("Pragma", "no-cache");         res.header("Expires", 0);          res.render(thisPage.file + '_' + language, {             thisPage : thisPage,             language: language,             languages: content.languages,             navigation: content.navigation,             footerNavigation: content.footerNavigation,             currentYear: new Date().getFullYear()         });     }     else     {         error404Handling(req, res);     } } 
like image 950
h345k34cr Avatar asked Sep 15 '13 10:09

h345k34cr


People also ask

How do I get a 304 status code?

An HTTP 304 not modified status code means that the website you're requesting hasn't been updated since the last time you accessed it. Typically, your browser will save (or cache) web pages so it doesn't have to repeatedly download the same information. This is an attempt to speed up page delivery.

What is 304 HTTP status code?

The HTTP 304 Not Modified client redirection response code indicates that there is no need to retransmit the requested resources. It is an implicit redirection to a cached resource.

Which HTTP status code is usually returned when the resource is cached?

The items with code "200 (cache)" were fulfilled directly from your browser cache, meaning that the original requests for the items were returned with headers indicating that the browser could cache them (e.g. future-dated Expires or Cache-Control: max-age headers), and that at the time you triggered the new request, ...

What is Express () in node JS?

Express is a node js web application framework that provides broad features for building web and mobile applications. It is used to build a single page, multipage, and hybrid web application. It's a layer built on the top of the Node js that helps manage servers and routes.


1 Answers

Easiest solution:

app.disable('etag'); 

Alternate solution here if you want more control:

http://vlasenko.org/2011/10/12/expressconnect-static-set-last-modified-to-now-to-avoid-304-not-modified/

like image 85
blented Avatar answered Sep 19 '22 17:09

blented