Learn the common methods to customize Cloudflare’s cache settings to extend caching to additional resources.
- your origin web server sends certain Cache-Control headers for your content, or
- Cloudflare is instructed to cache additional content via a Cache Everything Page Rule.
Cache additional content at Cloudflare
Caching additional content at Cloudflare requires a Cache Everything Page Rule. Without creating a Cache Everything Page Rule, dynamic assets are never cached even if a public Cache-Control header is returned. When combined with an Edge Cache TTL > 0, Cache Everything removes cookies from the origin web server response.
To create a Cache Everything Page Rule, follow these steps:
1. Log in to your Cloudflare account.
2. Choose the appropriate domain.
3. Click the Page Rules app.
4. In the textbox under If the URL matches, create a URL pattern to differentiate your website’s static versus dynamic content.
5. Choose the Cache Level setting and then the Cache Everything submenu setting.
6. Click Save and Deploy.
7. Verify your resources are cached by checking the cache response returned by Cloudflare.
Cloudflare caches XML responses when using Cache Everything. By default, jquery's getScript explicitly bypasses cache by appending a timestamp query string unless the behavior is disabled. For further details on how to adjust Cloudflare’s behavior in regards to query strings, refer to our article on Understanding Cloudflare Caching Level.
Example configurations to customize caching
Some possible combinations of origin web server settings and Cloudflare Page Rules include:
- Create a directory for static content at your origin web server. For example, create a /static/ subdirectory at your origin web server and a Cache Everything Page Rule matching the *example.com/static/* URL pattern.
- Append a unique file extension to static pages. For example, create a .shtml file extension for resources at your origin web server and a Cache Everything Page Rule matching the *example.com/*.shtml URL pattern.
- Add a query string to a resource’s URL to mark the content as static. For example, add a static=true query string for resources at your origin web server and a Cache Everything Page Rule matching the *example.com/*?*static=true* URL pattern.