0

This is a direct duplicate of this question, but the solution provided doesn't work.

As part of a WordPress plugin that I maintain, I am currently spitting out a dynamic CSS file using the following code:

public static function buildCustomCss() {
  if (1 == intval(get_query_var(self::$query_var))) {
     ob_start();
        global $css;
        $expires = 60 * 60 * 24 * 365; // cache for a year

        header('Pragma: public');
        header('Cache-Control: maxage=' . $expires);
        header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
        header('Content-type: text/css');

        echo str_replace('>', '>', esc_html($css));
     ob_end_flush();
     exit;
  }
}

The header values used match what is used in the cited question above, but both Chrome AND Firefox refuse to honor the cache request. I've tried multiple servers and each one spits back a 200 response every time. I'd hoped this would prove to be a better solution than just throwing the CSS into the main page, but if I can't get caching working then it's going to end up significantly worse.

The full list of request headers (Chrome):

Accept:text/css,*/*;q=0.1
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:<cookie values>
DNT:1
Host:example.org
Referer:http://example.org/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36

The full list of response headers (Edited to include latest headers tested, including public in both pragma and in cache-control):

Cache-Control:no-transform,public,maxage=31536000
Connection:keep-alive
Content-Type:text/css; charset=UTF-8
Date:Sun, 30 Mar 2014 22:55:57 GMT
Expires:Mon, 30 Mar 2015 22:55:56 GMT
Pragma:public
Server:nginx
Transfer-Encoding:chunked
Community
  • 1
  • 1
Dan
  • 3,246
  • 1
  • 32
  • 52

1 Answers1

0

If you want a file to be cached by browsers, you should set the Cache-control header to public:

header('Cache-control: public');
Companjo
  • 1,789
  • 18
  • 24
  • It [shouldn't make a difference](http://stackoverflow.com/questions/10314174/difference-between-pragma-and-cache-control-headers), but I tried. No change. – Dan Mar 30 '14 at 23:09