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);
}
}
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 whenCache-Control: no-cache
headers are received, but it doesn't do the same forCache-Control: max-age=0
. From what I can tell, it probably should. But I'm not an expert on caching.The fix is to change (what is currently) line 37 of
node-fresh/index.js
fromto
I forked node-fresh and express to include this fix in my project's
package.json
vianpm
, you could do the same. Here are my forks, for example:https://github.com/stratusdata/node-fresh https://github.com/stratusdata/express#safari-reload-fix
The safari-reload-fix branch is based on the 3.4.7 tag.