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

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
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. 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.

Not the answer you're looking for? Browse other questions tagged .