bhauman / lein-figwheel

Figwheel builds your ClojureScript code and hot loads it into the browser as you are coding!
Eclipse Public License 1.0
2.88k stars 209 forks source link

Set Cache-Control: no-cache on responses #586

Closed danielcompton closed 7 years ago

danielcompton commented 7 years ago

Cache-Control: no-cache tells the browser that it may cache the response, but it must always revalidate the cached response with the server, before using it. This is equivalent to setting

Cache-Control: max-age=0, must-revalidate

max-age=0 means that the response is always stale, and must-revalidate means that the client must always revalidate stale resources.

Expires: -1 is not needed, all modern browsers support Cache-Control. IE8 (and below?) treat 'Cache-Control: no-cache' as 'no-store', meaning that they will never cache files. That seems a small price to pay for the simpler reasoning about caching by only using Cache-Control.

Chrome dev site has good information on the current state of the art for HTTP caching. Jake Archibald has a good explanation of exactly the caching pattern that we are looking to employ.

See also https://stackoverflow.com/a/19938619/826486 for more discussion on Cache-Control: no-cache.

Based on @tonsky's work on #464, but fixes merge conflicts, and removes Expires: -1.

Fixes #546.

pesterhazy commented 7 years ago

@danielcompton I think this is the right thing to do. Note that as I understand it setting no-cache doesn't completely disable caching, it only disables blindly avoiding the network roundtrip. The cached resource will still be used if caching headers are returned.

Does figwheel honor the If-None-Match and If-Modified-Since headers so browsers get a more efficient 304 Not Modified resource?

danielcompton commented 7 years ago

Note that as I understand it setting no-cache doesn't completely disable caching

Yep.

Does figwheel honor the If-None-Match and If-Modified-Since headers so browsers get a more efficient 304 Not Modified resource?

Figwheel doesn't send an Etag for files so it won't get an If-None-Match (though that might be a good future improvement). AFAICT it didn't return a 304 for If-Modified-Since, I think static files are served from https://github.com/danielcompton/lein-figwheel/blob/1c5fb829b59230bf03bc1f565be103543ecd41c0/sidecar/src/figwheel_sidecar/components/figwheel_server.clj#L163-L174.

I've just added another commit which checks the If-Modified-Since header. I checked this now, and verified that before the wrap-not-modified addition, Chrome requested the files every reload, and got a 200 every time. After adding wrap-not-modified, we get pleasing 304's for everything after initial load.

bhauman commented 7 years ago

perfect!

Sophia-Gold commented 7 years ago

This is great! Thanks @danielcompton.