8

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?

Jesse Nickles
  • 250
  • 1
  • 12
Wudong
  • 191
  • 1
  • 1
  • 5
  • it seems now you have found the solution. What have you done ? – Trif Apr 29 '15 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 }));` – Rudy Lee Apr 13 '16 at 02:03
  • > MISS: resource not in cache, served from origin server From: https://support.cloudflare.com/hc/en-us/articles/200168266-What-do-the-various-CloudFlare-cache-responses-HIT-Expired-etc-mean- – Steve Tauber Apr 13 '15 at 11:12
  • Yes, but this doesn't explain why it doesn't cache it. – curiousdannii Sep 21 '16 at 00:54
  • Related: https://serverfault.com/questions/295790/cloudflare-and-cache-control – Jesse Nickles Aug 31 '22 at 08:59

4 Answers4

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

Michelangelo
  • 250
  • 2
  • 12
  • 4
    I believe this is incorrect. CloudFlare caches static files like js/css by default, but not HTML pages. See information here https://support.cloudflare.com/hc/en-us/articles/200168216--Does-CloudFlare-cache-HTML-content- – Tim Feb 28 '16 at 00:34
  • A useful tool for confirming everything is working ok is https://cf-cache-status.net – Jay Aug 20 '18 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

AJ H
  • 131
  • 1
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

Jay
  • 149
  • 1
  • 7
  • 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. – Jesse Nickles Aug 31 '22 at 08: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.

Jesse Nickles
  • 250
  • 1
  • 12