ConfigGenerator

Cloudflare Caching Rule Generator

Generate Cloudflare caching headers and rule suggestions for static assets, HTML, APIs, images, fonts, immutable files, and no-cache routes.

Output:A ready-to-use configuration file for Cloudflare Caching Rule with best practices applied.

Caching Presets

File Location

Where to place your _headers

Source Repository
public/_headers
After Build
out/_headers
  • Next.js server-side redirects in next.config.ts do not work for static export on Cloudflare Pages.
  • Use output: 'export' in your Next.js config.
  • Place _headers in the public directory before building.
  • Cloudflare Pages should deploy the 'out' directory.
/*.html
  Cache-Control: public, max-age=0, must-revalidate

/_next/static/*
  Cache-Control: public, max-age=31536000, immutable

/images/*
  Cache-Control: public, max-age=86400, stale-while-revalidate=604800

/fonts/*
  Cache-Control: public, max-age=31536000, immutable

/api/*
  Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate

Quick Summary

Use the Cloudflare Caching Rule Generator to safely define Cache-Control headers in your _headers file for HTML, hashed assets (JS/CSS), APIs, and images on Cloudflare Pages.

What is this tool?

Cloudflare Pages has default caching behaviors, but they are often not perfectly optimized for your specific framework. The Caching Rule Generator helps you build explicit Cache-Control headers that tell both the visitor's browser and Cloudflare's edge servers exactly how long to keep files.

How to Use This Tool

  1. Select your framework (Next.js, React, Astro, etc.) to get the right file location.
  2. Enable caching rules for the file types in your project (HTML, JS/CSS, Fonts, APIs).
  3. Review the generated _headers rules.
  4. Copy the content into your _headers file in the correct directory.

What This Tool Generates

  • _headers rules targeting specific URL paths with optimized Cache-Control directives.

Best Practices

  • Cache hashed files (JS/CSS with random strings in the name) forever using 'immutable'.
  • Never cache HTML documents for long, or users will be stuck on old versions of your app.
  • Use 'no-store' for any route that returns user-specific or sensitive data (like /api/profile).

Common Mistakes

  • Applying 'max-age=31536000, immutable' to '/*' which caches your index.html forever, breaking your site updates.
  • Forgetting that setting a header in _headers affects BOTH the browser cache and Cloudflare's edge cache for static Pages.
  • Caching Next.js Server Actions or dynamic API routes on static exports.

Security Notes

  • Ensure API routes have strict no-cache policies to prevent accidental data leaks where user A sees user B's cached response.

Frequently Asked Questions

How do I cache static assets on Cloudflare Pages?
You can cache static assets by creating a _headers file and setting a Cache-Control header for a specific path, e.g., '/assets/*' with 'Cache-Control: public, max-age=31536000, immutable'.
What Cache-Control should I use for JS and CSS?
If your framework hashes file names (like app-1a2b3c.js), you should use 'immutable' caching with a max-age of 1 year. This prevents the browser from ever re-requesting the file until the HTML references a new hash.
Should HTML be cached?
Generally, HTML should have a very short max-age or 'no-cache'/'must-revalidate' to ensure visitors always get the latest version of your site, which in turn references the new hashed CSS/JS files.
What is immutable caching?
Immutable means the file will never change at that specific URL. If the file needs to change, it will be published at a new URL (usually via a content hash in the filename). This allows browsers to skip checking if the file is fresh.
How do I prevent API caching?
Apply 'Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate' to your API routes to ensure Cloudflare and the browser never cache sensitive data.

How We Keep Your Configs Safe & Valid

Built-in Error Checking

Every file is checked against official rules. We catch missing fields and bad syntax. YAML indentation errors are flagged right away. Kubernetes, Terraform, and Docker specs are all covered. API versions and labels are verified too. You get valid output every time you generate.

100% Private & Local

All tools run in your browser only. Your API keys never leave your machine. We do not use any tracking scripts. No data is sent to any server. Passwords and secrets stay on your device. Crypto operations use the Web Crypto API. Your privacy is fully protected at all times.

Secure Settings by Default

Configs use safe defaults out of the box. Containers run as non-root users. Root filesystems are set to read-only. Dangerous Linux capabilities are dropped. Network policies limit pod-to-pod traffic. TLS 1.3 is enabled for web servers. Security headers are added where needed.

Ready for CI/CD & Git

Output files are ready for your Git repo. Use them with ArgoCD, Flux, or GitHub Actions. Files use clear formatting and comments. Code review is easy for your team. Indentation and key order are consistent. Test in staging before going to production. Every file is clean and well-structured.

Infrastructure as Code

Store configs in Git alongside your code. Terraform modules include typed variables. Backend configs support remote state locking. Outputs work across multiple modules. Ansible playbooks use clear task steps. Chef and Puppet configs are also supported. Every file works with version control tools.

Monitoring & Tracing

Set up Prometheus with auto-discovery rules. Create Grafana dashboards with template variables. Add alerting rules with severity labels. Use OpenTelemetry for trace collection. Forward logs to Loki or Elasticsearch. Connect to Jaeger or Tempo for tracing. Monitor metrics, logs, and traces together.

Container & Docker Safety

Dockerfiles use multi-stage builds for small images. Base images are pinned to exact versions. Dev files are excluded from final images. Health checks are added for orchestrator use. Containers switch to non-root users. Docker Compose uses named volumes and networks. Resource limits are set in deploy configs.

Multiple Output Formats

Export as YAML, JSON, HCL, or TOML. Kubernetes uses YAML with proper separators. Terraform uses HCL with correct escaping. JSON output has consistent indentation. Copy to clipboard with one click. Preview output with syntax highlighting. Line numbers help you review quickly.

Related Tools