2

In reviewing the answer to this post, I don't understand why 0 seconds is used for best practice here:

ExpiresByType text/html "access plus 0 seconds"

I think this means that whenever a user visits the website, the html page is always downloaded instead of pulled from cache. Is that right?

Is this what people typically set for this variable? If so why? If not, which scenarios benefit from setting it to 0 seconds?

Community
  • 1
  • 1
ggkmath
  • 4,048
  • 22
  • 67
  • 126

1 Answers1

7

If text/html only meant "content from static HTML files", you'd be right, and you'd want to set a longer cache life. But server-side scripts typically also return a web page; that is, their responses' Content-Type will often be text/html as well. And if all HTML were cached, dynamically generated content might not appear to update properly.

If you wanted to expire only non-static HTML, you might be able to do something like

<FilesMatch "\.php$">
    ExpiresByType text/html "access plus 0 seconds"
</FilesMatch>

(using PHP as an example).

cHao
  • 78,897
  • 19
  • 136
  • 168
  • Thanks @cHao,I think you're implying some knowledge about static vs dynamic HTML that's not mentioned... to be clear, are you saying that this setting of "0" benefits dynamic-generated HTML, and for static HTML, it's fine to be cached long term and so something greater than 0 is typical? Also, is it true that a setting of "0" forces the HTML file to be downloaded from the server each time the page is loaded? – ggkmath Oct 30 '13 at 21:00
  • 1
    The first part is about right. Setting the expire time to access+0 is better when the content might be out of date even a second later. For static HTML files, since they don't change nearly that often, it'd be better to cache them for a while. But as for the second part, "force" is a really strong word when you're talking about other people's servers. The header is just stating how you'd *like* a cache to treat your file. There's no law saying it has to honor that. Any decent cache will at least try, though. – cHao Oct 30 '13 at 21:17
  • This is where I get confused. If I put "access plus 0 seconds" for my `text/html` file, I observe my html file gets downloaded once, and always pulls from cache thereafter. Until, I load a new version of that html file on the server and change the link's url string cache-busting version number... as soon as I do that, the html file downloads again from server. This is exactly the behavior I want. However, I can't seem to square it with the "0 seconds" setting (isn't this telling the browser to download that html file off the server each time)? – ggkmath Oct 30 '13 at 21:22
  • Absent other stuff, it *should* download the file each time. There might be other mechanisms at work, though, including your browser's settings. The browser isn't as strict as most proxy servers; it can be set to ignore that stuff. IE, for example, can be set to "check for newer versions of stored pages" -- ie: update the cache -- every visit, every session, automatically (probably based on caching headers), or *never*. – cHao Oct 30 '13 at 22:45
  • @ggkmath: that's also what i observed. Using [this](http://stackoverflow.com/questions/11532636/prevent-http-file-caching-in-apache-httpd-mamp#answer-11724596) works for forcing it though. – br4nnigan Jul 22 '15 at 15:45