8

Craft seems to be setting headers to tell the browser not to cache anything it serves, which seems bad.

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0

Why it do dat?

9

Craft doesn't explicitly set any caching headers in the response, but you can control exactly how you want your caching headers to behave from your templates with the {% header %} tag.

For example:

{# Tell the browser to cache this page for 30 days #}
{% set expiry = now|date_modify('+30 days') %}

{% header "Cache-Control: max-age=" ~ expiry.timestamp %}
{% header "Pragma: cache" %}
{% header "Expires: " ~ expiry|date('D, d M Y H:i:s', 'GMT') ~ " GMT" %}

By default PHP has the session_cache_limiter() method set to nocache, which is probably what you're seeing.

| improve this answer | |
  • Hmmm...It seems like something is (Yii or Craft) unless I'm confused. Request headers for a static (non-craft request): HTTP/1.1 200 OK Date: Thu, 18 Sep 2014 01:44:49 GMT Server: Apache Last-Modified: Thu, 18 Sep 2014 01:44:48 GMT Accept-Ranges: bytes Cache-Control: max-age=0 Expires: Thu, 18 Sep 2014 01:44:49 GMT Vary: Accept-Encoding Content-Encoding: gzip X-UA-Compatible: IE=edge Content-Length: 26 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: text/html; charset=utf-8 Here's one from Craft: ``` – Tim Kelty Sep 18 '14 at 1:45
  • Maybe Apache sets them by default if nothing has already been set? – Brad Bell Sep 18 '14 at 1:49
  • Response header examples below (non-pertinent headers removed) Response headers for a static (non-Craft request): HTTP/1.1 200 OK Date: Thu, 18 Sep 2014 01:44:49 GMT Last-Modified: Thu, 18 Sep 2014 01:44:48 GMT Cache-Control: max-age=0 Expires: Thu, 18 Sep 2014 01:44:49 GMT Here's one from Craft: HTTP/1.1 200 OK Max-Age=300; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache – Tim Kelty Sep 18 '14 at 1:51
  • 1
    @TimKelty I would definitely recommend not changing this from php.ini as it will have unintended consequences. If you don’t like PHP’s default behavior you should just start explicitly setting what you want using the {% header %} tag. – Brandon Kelly Sep 18 '14 at 13:50
  • 1
    Pfft... LIVE ON THE EDGE! – Brad Bell Sep 18 '14 at 18:05
1

As highlighted in Brad Bell's answer, those headers are likely set by PHP. By default PHP has the session_cache_limiter() method set to nocache. You can verify this in your Craft admin panel under Utilities > PHP Info > session.cache_limiter.

If you change this value to public, it will set a cache expiry according to the value of session.cache_expire (set to 180 by default). This is explained here in the PHP docs.

I did not heed Brandon Kelly's advice, and I set session.cache_limiter=public in my php.ini file. I do not know which unintended consequences he speaks of, but I like to live dangerously.

Here are the new headers I get:

Cache-Control: public, max-age=10800
Expires: Wed, 27 Nov 2019 11:54:38 GMT (180 minutes from now)

If you do this, it will break a few things in the admin. Admin pages won't always load properly because the wrong things will be cached. It works perfectly on the website though.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.