7

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?

  • > MISS: resource not in cache, served from origin server From: support.cloudflare.com/hc/en-us/articles/… – Steve Tauber Apr 13 '15 at 11:12
  • 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 2:03
  • Yes, but this doesn't explain why it doesn't cache it. – curiousdannii Sep 21 '16 at 0:54
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

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

0

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

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.