0

I was using the HTML meta tags to disable cache until I learned that this only works if the file is served locally, so I disabled the cache through the recommended use of headers by putting this at the top of my pages:

header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");

What I can't find an answer to and it may be silly to ask, but this doesn't disable resources from caching either, does it? I was in my developer tools and looking at the network tab and noticed that the page was loading fresh every time, but resources such as CSS and Javascript were still loading from cache, which lead me to believe that preventing page cache doesn't also mean prevent page resources from being cached, but I don't know if that's actually true. So what I did was add some versioning to the resources like so:

<link rel="stylesheet" href="css/style.css?version=1">

That stopped the resource from being cached, which is what I wanted, but then after reading some more it appears that additional headers can be used to target certain file formats and stop them from being cached as well, which would yield the same results as the versioning, albeit a different way.

Is my understanding of this correct and/or is mixing methods bad practice?

Waxi
  • 1,641
  • 2
  • 14
  • 20
  • Maybe you're caching at the Web Server level? In IIS you have this option, almost sure others too – Oscar Feb 22 '17 at 20:55
  • 2
    for appache: http://httpd.apache.org/docs/current/mod/mod_expires.html –  Feb 22 '17 at 20:56

1 Answers1

1

Your observations and understanding are correct. When you set caching headers in your php, for example, they are valid for that page only. Look at it from a browser's point of view:

  • Request page - receive response with instructions not to cache it
  • Parse received page, get URIs of resources (css, js, img, etc.)
  • Send request for each of these resources - receive response

If in this last response there are no headers telling the browser to not cache them, then they will be cached per standard rules.

There are various ways to prevent caching. One is exactly what you describe, i.e. add an ever changing parameter to the requested URI - this way, every time it's requested, it's requested with a different parameter. It would still be cached, but the next request would not match this one and the cache would not be used.

Another involves changing the configuration of your web server to indicate that specific types of resources (file names, extensions, content types, etc) should not be cached. For example, the following in Apache configuration indicates that no caching should occur at all for any files:

<Files *>
    Header set Cache-Control: "private, pre-check=0, post-check=0, max-age=0"
    Header set Expires: 0
    Header set Pragma: no-cache
</Files>

Do note that the idea of caching is two-fold:

  • allow clients to get responses quicker by using previously downloaded local copy of a resource
  • lower the load on your server by having to serve fewer responses

Therefore do be careful with turning caching off. You may (in the extreme case) end up killing your server if you seriously underestimate the number of requests you need to handle.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • Thank you for the browser's POV, that made it clear to me. I'm not sure why I thought the initial instructions not to cache would trickle down to everything else in the page, but now I understand! – Waxi Feb 22 '17 at 21:08
  • 1
    just to add even if you do this the files could still be cached at various locations such as your ISP. –  Feb 22 '17 at 23:29
  • @nogad correct. Some proxies may ignore or even strip caching headers. – Aleks G Feb 22 '17 at 23:51