NodeJS/express: Cache and 304 status code

前端 未结 5 1756
迷失自我
迷失自我 2020-11-30 18:41

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 solv

相关标签:
5条回答
  • 2020-11-30 19:19

    Old question, I know. Disabling the cache facility is not needed and not the best way to manage the problem. By disabling the cache facility the server needs to work harder and generates more traffic. Also the browser and device needs to work harder, especially on mobile devices this could be a problem.

    The empty page can be easily solved by using Shift key+reload button at the browser.

    The empty page can be a result of:

    • a bug in your code
    • while testing you served an empty page (you can't remember) that is cached by the browser
    • a bug in Safari (if so, please report it to Apple and don't try to fix it yourself)

    Try first the Shift keyboard key + reload button and see if the problem still exists and review your code.

    0 讨论(0)
  • 2020-11-30 19:22

    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/

    0 讨论(0)
  • 2020-11-30 19:24

    I had the same problem in Safari and Chrome (the only ones I've tested) but I just did something that seems to work, at least I haven't been able to reproduce the problem since I added the solution. What I did was add a metatag to the header with a generated timstamp. Doesn't seem right but it's simple :)

    <meta name="304workaround" content="2013-10-24 21:17:23">
    

    Update P.S As far as I can tell, the problem disappears when I remove my node proxy (by proxy i mean both express.vhost and http-proxy module), which is weird...

    0 讨论(0)
  • 2020-11-30 19:32

    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. But I'm not an expert on caching.

    The fix is to change (what is currently) line 37 of node-fresh/index.js from

    if (cc && cc.indexOf('no-cache') !== -1) return false;  
    

    to

    if (cc && (cc.indexOf('no-cache') !== -1 ||
      cc.indexOf('max-age=0') !== -1)) return false;
    

    I forked node-fresh and express to include this fix in my project's package.json via npm, 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.

    0 讨论(0)
  • 2020-11-30 19:33

    Try using private browsing in Safari or deleting your entire cache/cookies.

    I've had some similar issues using chrome when the browser thought it had the website in its cache but actually had not.

    The part of the http request that makes the server respond a 304 is the etag. Seems like Safari is sending the right etag without having the corresponding cache.

    0 讨论(0)
提交回复
热议问题