Does it make sense to have max-age and s-maxage in the Cache-Control HTTP header?

北城余情 提交于 2020-01-01 23:11:14

问题


Considering that max-age applies to all the caches, and s-maxage only applies to shared caches (proxy and gateway cache)....

Does it make sense to use both directives in a non-expirable and public page?

Controller pseudo-code:

w = Response();
w.setPublic();
w.setMaxAge("1 year");
w.setShareMaxAge("1 year");

return w;

回答1:


From HTTP Header Field Definitions:

14.9.3 Modifications of the Basic Expiration Mechanism

...

s-maxage

If a response includes an s-maxage directive, then for a shared cache (but not for a private cache), the maximum age specified by this directive overrides the maximum age specified by either the max-age directive or the Expires header.

...

Note, "overrides". So, it would only make sense if you intend to specify a different maximum age for shared caches as compared to max-age, which would be used by end users.

In your particular example, they're the same, so specifying s-maxage is just unnecessary.




回答2:


Key point to note in the definition is share cache

From HTTP Header Field Definitions

14.9.3 Modifications of the Basic Expiration Mechanism

...

s-maxage

If a response includes an s-maxage directive, then for a shared cache (but not for a private cache), the maximum age specified by this directive overrides the maximum age specified by either the max-age directive or the Expires header.

...

It means that reverse proxy/caches like varnish, cloudfront, cloudflare can have a cache age different to browser cache. I would personally prefer value of s-maxage to be higher than maxage

In HTTP 1.1 once response is cached, you can't contact browser to invalidate cache but you can tell it to CDNs.



来源:https://stackoverflow.com/questions/15971747/does-it-make-sense-to-have-max-age-and-s-maxage-in-the-cache-control-http-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!