akka / akka-http

The Streaming-first HTTP server/module of Akka
https://doc.akka.io/docs/akka-http
Other
1.34k stars 594 forks source link

Adding a default cache-control header for routes served from FileAndResourceDirectives #2273

Open Prithvirajbilla opened 6 years ago

Prithvirajbilla commented 6 years ago

FileAndResourceDirectives provides a lot of helpful functions to serve static assets from file system. Currently it adds Etag and Last-Modified response header to help with caching on browsers. I think we should also add a default Cache-Control response header. Because without a cache-control header, browsers are free to choose their own caching heuristic and that may result in serving stale assets from cache and some weird behaviours.

And, also I think the default cache-control header should be Cache-Control: must-revalidate, because this will trigger a cache revalidation on every request (If the requested asset is not changed, browser will get 304 - Not modified and served from cache)

Current workaround is to wrap getFromFile calls under respondWithHeader functions. It would be nice, if the library provides this to us by default.

jrudolph commented 5 years ago

Thanks for the suggestion, @Prithvirajbilla and sorry for the extremely long delay in answering.

Your suggestion seems quite reasonable. Do you have any resources about how serve static files in the best way?

Prithvirajbilla commented 5 years ago

Thanks for the response.

The default behavior I am proposing is the following. And, Let's see if agree on the default behavior.

Proposed Solution Adding a default Cache-Control response header with value no-cache.

Please note that no-cache: doesn't mean "don't cache", but indicates that cache MUST NOT use a stored response to satisfy the request without successful validation on the origin server

Please note that the proposed solution in my earlier comment is not actually right, which I realized later on.

For more details, please look at the following resource. https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#no-cache_and_no-store