# How To Force Cloudflare To Cache Resources

> By default Cloudflare caches all static resources - but here's how to "prime" it and force Cloudflare to cache what yo need on its EDGE servers.

- Author: [Bhagwad Park](https://www.namehero.com/blog/author/bhagwad-park/)
- Published: 2018-11-19
- Updated: 2018-12-10
- Category: [Website Performance](https://www.namehero.com/blog/category/website-performance/)
- Web page: https://www.namehero.com/blog/how-to-force-cloudflare-to-cache-resources/

I’ve spent the weekend speeding up my site with Cloudflare. At the end of the day, here are my pagespeed results from Google’s Lighthouse tool:

[![97 Percent Speed Optimization](https://www.namehero.com/blog/wp-content/uploads/2018/11/97-Percent-Speed-Optimization.png)](https://www.namehero.com/blog/wp-content/uploads/2018/11/97-Percent-Speed-Optimization.png)

As you can see, I’ve scored in the green for everything, and I already know what I have to do to reach 100%. And all of this was due to Cloudflare caching my resources. If you have Cloudflare installed, it’s probably already caching your CSS and JS files. But sometimes, you *think* it’s caching your resources, and it’s not. In this article, I’ll explain some of the things that can prevent Cloudflare from caching your files, and how to force it to do so.

## Check if Cloudflare is Caching your File or Not

The first step is to see if your static files are being delivered from Cloudflare’s EDGE servers. To do this, first make sure that Cloudflare is enabled on your site. You should see it when you’re logged into Cloudflare like this:

[![Cloudfare is Installed](https://www.namehero.com/blog/wp-content/uploads/2018/11/Cloudfare-is-Installed.png)](https://www.namehero.com/blog/wp-content/uploads/2018/11/Cloudfare-is-Installed.png)

Now load a page and take a CSS or JS URL. In this example, I’m choosing the following CSS file:

[![Example CSS File](https://www.namehero.com/blog/wp-content/uploads/2018/11/Example-CSS-File.png)](https://www.namehero.com/blog/wp-content/uploads/2018/11/Example-CSS-File.png)

Now open up a shell command line (either Windows or Linux), and type in the following command:

```
curl -svo /dev/null **[URL of File]**
```

Replace **[URL of File]** with the filename you just got. In the output, look for the line starting with:

```
CF-Cache-Status:
```

You will *most* likely see a MISS like this:

[![CF-Cache-Status MISS](https://www.namehero.com/blog/wp-content/uploads/2018/11/CF-Cache-Status-MISS.png)](https://www.namehero.com/blog/wp-content/uploads/2018/11/CF-Cache-Status-MISS.png)

Which means that Cloudflare has NOT cached your file for this location. Now run the command *two more times* at the most. If you see a MISS even on the third try, it means that something is wrong and that Cloudflare is not caching your resources.

If you are configured correctly, you will see a HIT on the consecutive tries:

[![Running it Again Will Hit the Cache](https://www.namehero.com/blog/wp-content/uploads/2018/11/Running-it-Again-Will-Hit-the-Cache.png)](https://www.namehero.com/blog/wp-content/uploads/2018/11/Running-it-Again-Will-Hit-the-Cache.png)

This is how you force Cloudflare to cache a specific file. But what’s happening?

## Why MISS and Then HIT?

Cloudflare has EDGE servers located around the globe. At last count, it was over 155. That means no matter where your visitors are, they have a Cloudflare server closeby. The caching process works like this:

1. Visitor requests a cacheable URL
2. That URL is not cached by default on that particular EDGE server
3. The EDGE server requests the URL from your origin server (your main site)
4. Your site delivers the URL to the EDGE server
5. The EDGE server delivers the URL to the visitor
6. For subsequent hits to that EDGE server LONE, the URL will be cached

Now this is a problem. It means that first time visitors from a particular location are *actually getting slower page speed times* with Cloudflare. That’s because there’s an additional step of the EDGE server requesting the URL from the origin – instead of the visitor requesting it directly.

True, subsequent visits will be faster. But with so many servers, that’s a LOT of visitors who are getting very low page speed times.

## Argo is a Possible Solution

In an earlier tutorial, Ryan had written about [Cloudflare’s “Argo” service](https://www.namehero.com/blog/cloudflares-argo-smart-routing-worth-cost/). While he focused mainly on the latency aspect, there is what I consider to be the most important part – tiered caching. With Argo, whenever an EDGE server doesn’t have a file, it *checks the other EDGE servers* to see if they have one. If they do, then the file is transferred to it from one of Cloudflare’s own servers.

This has two huge benefits:

1. Your site is hit just once for all cacheable resources
2. Cloudflare transfer times between servers is *far faster* than from your origin server

In fact, I’m going to experiment further with Argo and *cache even my HTML*. With Argo, HTML caching actually becomes useful since my origin server won’t have to do much work. The tricky part is modifying cache headers for HTML, creating accurate page rules, and figuring out how to bypass the cache when I need to do admin work, or write a new post.

But for now, use the technique above to force Cloudflare to prime a specific EDGE server if you’re expecting visitors from that location!

## Related posts

- [“Cache Everything” On Cloudflare The RIGHT Way](https://www.namehero.com/blog/cache-everything-on-cloudflare-the-right-way/)
- [Why Cloudflare “Cache Everything” Isn’t A Good Idea](https://www.namehero.com/blog/why-cloudflare-cache-everything-isnt-a-good-idea/)
- [Use Cloudflare Cache Rules Instead Of Page Rules](https://www.namehero.com/blog/use-cloudflare-cache-rules-instead-of-page-rules/)
- [How To Achieve ~90% Cache Hit Ratio On Cloudflare](https://www.namehero.com/blog/how-to-achieve-90-cache-hit-ratio-on-cloudflare/)
