How to Optimize Your Website Cloudflare Settings for Page Speed, Performance, & Security
Optimize your Site with Cloudflare in 60 minutes
Just Kidding: The configuration changes don’t take long, but validation may easily take a bit longer.
Optimizing your site with Cloudflare can have a significant impact on your business in several ways:
- Improved User Experience: Faster loading times lead to a better user experience, which can increase user engagement, reduce bounce rates, and improve conversion rates. In today’s competitive online environment, users expect fast and seamless interactions, and even slight delays can lead to lost customers.
- Enhanced SEO Performance: Page speed is a crucial factor in search engine rankings. Google and other search engines prioritize faster-loading sites, meaning that optimizing your site with Cloudflare can lead to higher visibility and more organic traffic.
- Increased Security: Cloudflare offers robust security features that protect your site from various threats, including DDoS attacks, SQL injections, and other malicious activities. Enhanced security can safeguard your business’s reputation and ensure the safety of user data.
- Scalability and Reliability: Cloudflare’s global CDN ensures that your site can handle large volumes of traffic without performance degradation. This is especially important during peak times or viral events when traffic can spike unexpectedly.
- Cost Savings: By offloading traffic to Cloudflare’s edge servers, you can reduce bandwidth costs and server load. This not only lowers operational costs but also extends the lifespan of your infrastructure.
- Actionable Insights: Cloudflare provides comprehensive analytics and insights into your site’s performance and security. These insights can help you make data-driven decisions to further optimize your site and improve your business strategies.
- For more extensive insights, we recommend setting up an application performance monitoring tool such as New Relic or DataDog or OpenTelemetry.
Pros and Cons of Cloudflare Optimization
Pros:
- Improved Performance: Cloudflare's CDN (Content Delivery Network) accelerates content delivery by caching static content at edge servers globally, reducing latency and improving load times.
- Security Enhancements: Provides additional security features such as DDoS protection, Web Application Firewall (WAF), and SSL/TLS encryption.
- Scalability: Can handle traffic spikes and provide consistent performance during high traffic periods.
- Cost Efficiency: Reduces server load and bandwidth costs by caching content and serving it from the nearest edge server.
- Comprehensive Analytics: Offers detailed insights into website traffic, security threats, and performance metrics.
Cons:
- Complex Configuration: Setting up and optimizing Cloudflare can be complex, especially for beginners.
- Dependency on Cloudflare: Heavy reliance on Cloudflare can be a risk if there are service outages or issues.
- Potential for Caching Issues: Incorrect caching settings can lead to outdated content being served to users.
- Cost for Premium Features: Some advanced features, such as Image Resizing and WAF, require a paid plan.
But all-in-all, it isn’t really that hard to configure—it just takes time to test the system for inadvertent caching and mitigating edge cases where you don’t want to optimize too much for performance.
Here’s how we do it.
Recommendations for Cloudflare Settings
For Highly-Cacheable Content:
- Caching Level: Cache Everything
- Explanation: This setting caches all content, including HTML, which is beneficial for highly-cacheable sites.
- Pro: Maximizes caching efficiency and minimizes server load.
- Con: Dynamic content might be served stale if not correctly configured.
- Edge Cache TTL: Longer Duration (e.g., 1 month)
- Explanation: Sets the duration for which content is cached at the edge servers.
- Pro: Reduces the frequency of requests to the origin server.
- Con: Updates to content might take longer to propagate.
- Always Online: Enabled
- Explanation: Serves a cached version of the site if the origin server is down.
- Pro: Provides high availability and uptime.
- Con: Users might see outdated content during server downtime.
- Auto Minify: Enabled (HTML, CSS, JS)
- Explanation: Automatically minifies code to reduce file sizes.
- Pro: Improves page load times by reducing the amount of data transferred.
- Con: Potential for code-breaking if minification is not handled correctly.
- Polish: Enabled (with WebP conversion)
- Explanation: Optimizes images by compressing them and converting them to WebP format.
- Pro: Significantly reduces image file sizes, improving load times.
- Con: Does not support all image formats (e.g., some PNGs).
For Partially Cacheable Content:
- Caching Level: Standard
- Explanation: Caches static assets but excludes HTML by default.
- Pro: Balances between performance and ensuring dynamic content is always fresh.
- Con: Less efficient than caching everything but safer for dynamic content.
- Edge Cache TTL: Shorter Duration (e.g., 1 hour)
- Explanation: Ensures that frequently changing content is updated more regularly.
- Pro: Keeps content more up-to-date while still benefiting from edge caching.
- Con: Increases the number of requests to the origin server.
- Page Rules: Specific Rules for Dynamic Pages
- Explanation: Custom rules to bypass cache or set different caching durations for dynamic pages.
- Pro: Fine-tunes caching strategy for different types of content.
- Con: Requires detailed configuration and ongoing management.
- Rocket Loader: Enabled
- Explanation: Improves loading times by asynchronously loading JavaScript.
- Pro: Enhances performance without compromising functionality.
- Con: Might cause compatibility issues with some JavaScript libraries.
- Polish: Enabled (without aggressive settings)
- Explanation: Optimizes images without over-compression.
- Pro: Improves performance without significant quality loss.
- Con: Less reduction in file size compared to aggressive settings.
Additional Recommendations:
- Synchronous JS File Loading: Prioritize critical JavaScript to load synchronously to improve perceived load times.
- Font Loading Optimization: Implement strategies like font-display: swap to prevent invisible text during font loading.
- Lazy Loading Offscreen Images: Defer the loading of images that are not immediately visible to the user.
- Server-Side Rendering (SSR): Improve performance and SEO by rendering pages on the server.
- Deferring Third-Party JS: Delay the loading of non-essential third-party scripts to after the initial page load.
Steps to Test Cloudflare Optimization Settings Using Page Rules
- Identify a Section of the Site to Test:
- Choose a specific section or page URL path of your site to test the new settings. This could be a directory (e.g., example.com/blog/*) or a specific page (e.g., example.com/about-us).
- Create a Page Rule in Cloudflare:
- Go to the Cloudflare dashboard and navigate to the "Page Rules" section.
- Click on "Create Page Rule."
- Enter the URL pattern for the section of the site you want to test. Use wildcard matching if needed (e.g., example.com/blog/*).
- Configure the Settings for the Page Rule:
- For Highly-Cacheable Content:
- Set Caching Level to "Cache Everything."
- Set Edge Cache TTL to "1 month."
- Enable Always Online.
- Enable Auto Minify for HTML, CSS, and JS.
- Enable Polish with WebP conversion.
- For Partially Cacheable Content:
- Set Caching Level to "Standard."
- Set Edge Cache TTL to "1 hour."
- Configure custom Page Rules to manage dynamic content.
- Enable Rocket Loader.
- Enable Polish without aggressive settings.
- For Highly-Cacheable Content:
- Monitor and Analyze Performance:
- Use tools like Google PageSpeed Insights and Lighthouse to measure performance improvements.
- Check Cloudflare analytics for insights on cache hit rates, bandwidth savings, and security threats.
- Iterate and Adjust:
- Based on the performance data, adjust the settings as needed to achieve the desired results.
- Test additional settings or features to further optimize performance.
- Expand Optimization Site-wide:
- Once satisfied with the results on the test section, gradually apply the optimized settings to other sections of the site.
- Continue monitoring and adjusting as necessary to maintain optimal performance.
Avoid Optimizations on Third-Party Hosted Platforms
Avoid applying these Cloudflare optimizations on third-party hosted platforms (e.g., Shopify, WordPress.com, etc.) unless you are certain of their compatibility. Third-party platforms often have their own optimization and caching mechanisms that could conflict with Cloudflare settings, leading to unintended consequences and lengthy troubleshooting.
Getting Started with Cloudflare
Have some general questions? Let us know in the comments below!
If you’d like some help getting started with Cloudflare please drop us a line and we’d be more than happy to review Cloudflare’s caching strategies and optimizations with you on your website.
References:
- Secure and Fast GitHub Pages with Cloudflare - https://blog.cloudflare.com/secure-and-fast-github-pages-with-cloudflare/
- Cloudflare Caching Explained - https://developers.cloudflare.com/cache/
- How to Optimize Your Site with Cloudflare - https://www.cloudflare.com/learning/performance/optimization/
- Image Optimization with Cloudflare Polish - https://developers.cloudflare.com/images/polish/
- Using Page Rules to Control Cloudflare Settings - https://support.cloudflare.com/hc/en-us/articles/218411427