9

I'm wondering why Cloulflare would not cache a certain static js file.

Given the reuqest:

curl -I http://www.testlifeinuk.com/dist/lifeinuk.js 

the headers returned like:

HTTP/1.1 200 OK Date: Mon, 12 Jan 2015 10:59:21 GMT Content-Type: application/javascript Connection: keep-alive Set-Cookie: __cfduid=d11a9f01292153436a211a9d807a3399b1421060361; expires=Tue, 12-Jan-16 10:59:21 GMT; path=/; domain=.testlifeinuk.com; HttpOnly X-Powered-By: Express Etag: W/"507660-1420797981000" Cache-Control: public, max-age=1382400 Last-Modified: Fri, 09 Jan 2015 10:06:21 GMT Vary: Accept-Encoding Via: 1.1 vegur CF-Cache-Status: MISS Expires: Wed, 28 Jan 2015 10:59:21 GMT Server: cloudflare-nginx CF-RAY: 1a78d818b4af0b81-LHR 

What puzzles me is that no matter how many times I do the request, it always return

CF-Cache-Status: MISS 

Any idea why?

5
  • > MISS: resource not in cache, served from origin server From: support.cloudflare.com/hc/en-us/articles/… Commented Apr 13, 2015 at 11:12
  • it seems now you have found the solution. What have you done ? Commented Apr 29, 2015 at 10:24
  • I had the same problem. In my case, I forgot to set the cache maxAge in express. Something like this server.use(express.static(path.join(__dirname, 'public'), { maxAge: 3600 })); Commented Apr 13, 2016 at 2:03
  • Yes, but this doesn't explain why it doesn't cache it. Commented Sep 21, 2016 at 0:54
  • Related: serverfault.com/questions/295790/cloudflare-and-cache-control Commented Aug 31, 2022 at 8:59

4 Answers 4

6

You have to set the Page Rule in the CloudFlare Admin panel. The first step to using Page Rules is to define a pattern that defines when the rule is triggered. These patterns can be simple, such as a single URL, or complicated including multiple wildcards. Imagine you have a content management system with a single URL:

http://www.testlifeinuk.com 

Now if you try to create a rule with URL Pattern:

http://www.testlifeinuk.com 

Only the homepage will be cached!

If you would like to cache the whole website, you have to use the Wildcard in this way:

 *testlifeinuk.com/* 

In this way all the pages and resources will be cached by Cloudflare like this:

http://www.testlifeinuk.com/dist/lifeinuk.js http://www.testlifeinuk.com/mypage.html https://www.testlifeinuk.com/mypage.html https://testlifeinuk.com/mypage.html 

and the result of the header will be:

HTTP/1.1 200 OK Date: Mon, 25 May 2015 10:21:05 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive X-Powered-By: PHP/5.4.39-0+deb7u2 Expires: Mon, 25 May 2015 10:51:05 GMT Cache-Control: public, max-age=1800 Pragma: no-cache Vary: Accept-Encoding CF-Cache-Status: HIT <<<<< Look this parameter Server: cloudflare-nginx CF-RAY: 1ec082ee43150e60-MXP Content-Encoding: gzip 

regards

2
  • 4
    I believe this is incorrect. CloudFlare caches static files like js/css by default, but not HTML pages. See information here support.cloudflare.com/hc/en-us/articles/… Commented Feb 28, 2016 at 0:34
  • A useful tool for confirming everything is working ok is cf-cache-status.net Commented Aug 20, 2018 at 16:25
3

Aside what @Michelangelo mentioned about you need to setup CloudFlare Page Rules at first, you also need to make sure you're set response headers correctly, so that you won't override the default caching behavior:

If the Cache-Control header is set to "private", "no-store", "no-cache", or "max-age=0", or if there is a cookie in the response, then CloudFlare will not cache the resource. Otherwise, if the Cache-Control header is set to "public" and the "max-age" is greater than 0, or if the Expires headers are set any time in the future, we will cache the resource.

See: https://support.cloudflare.com/hc/en-us/articles/202775670-How-Do-I-Tell-Cloudflare-What-to-Cache-

And that's why @Rudy Lee said don't forget to set maxAge option because default maxAge is 0 in Express.

See: https://expressjs.com/en/api.html#express.static

1

Another possibility is that something defined in your .htaccess may be conflicting with Cloudflare's caching. If expires headers are set there they will override Cloudflare's caching settings. I found this to be the case with my own website and I didn't even realize initially until I did a bunch of testing with https://cf-cache-status.net

1
  • Origin server cache TTL will only override Cloudflare edge TTL if it's greater than what you tell Cloudflare to cache your assets for. For example, if your origin server TTL for JPEG files is 30 days, but you Cloudflare cache tab is set to 15 days, then Cloudflare will replicate the origin server TTL of 30 days for JPEG files in their proxy headers.... but if it's smaller than your TTL in Cloudflare settings, they will use the Cloudflare TTL instead. Commented Aug 31, 2022 at 8:55
0

Missing from the Cloudflare documentation is also this zinger:

If your origin server does not set one of either Cache-Control or Expires headers at all (i.e. both of those headers are missing) then apparently Cloudflare will default to MISS regardless of your static file expiration settings on their cache tab...

In such a case, you need to use Page Rules to set Edge Cache TTL on those assets, and to strip cookies you can also set Cache Everything inside the same rule.

I've been using Cloudflare and playing with LEMP stack configurations for many years, but I only recently noticed this happening... I could be wrong, but I think maybe they changed this behavior in the past 1-2 years, around the same time they tweaked some of their cache tab features perhaps.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.