We often test our WordPress site for speed and other performance related issues. Tools such as Pingdom and GTmetrix test the website and show detailed analysis highlighting any warnings and errors. “Serve static content from a cookieless domain” is one such warning which at times appear inside the performance insights table.
Serving static content such as JavaScript, CSS and image files do not need an HTTP cookie with it. These unnecessary cookies put extra load on network traffic and slow down the web page loading. Since it is no good with these cookies, speed testing tools shoot this warning so this type of content is served via cookieless domain to save precious server resources. So we will explore further what a cookieless domain is and what are other ways to avoid this warning.
However, there is one thing worth noting. It is safe to ignore this warning with Cloudways which uses new protocol HTTP/2 for data transferring. HTTP/2, unlike HTTP, is more efficient in handling concurrent requests hence is not affected by these small cookies.
Cloudways’ users can enable this feature via a 1-click button from their Server Dashboard → Settings and Packages.
Let’s see how can we easily fix “Serve Static Content From a Cookieless Domain” warning in WordPress.
Fixing “Serve Static Content From a Cookieless Domain” Warning
In order to fix “Serve Static Content From a Cookieless Domain” warning, we will use two methods. Our first method is fairly easy and if you are lucky to be using Cloudways, then it’s only a matter of few clicks. Our second method, however, requires some knowledge and work but I can assure you whichever the method you are going to choose, I will make it as easy as possible. Let’s begin!
Method 1: Using a Content Delivery Network
Content Delivery Network or CDN for short is an effective way of serving static content to your end user. This network is spread across the globe and serve the cached content of your site from its nearest server to the user.
Introducing CDN to handle static content is an easier way. CDNs such as StackPath is capable of ignoring the cookies and can serve this content cookieless to the end user. Since no cookies are set by serving domain the warning goes away in the web testing tools.
Cloudways users can activate CloudwaysCDN powered by StackPath from their Application Dashboard simply by adding website URL.
Method 2: Re-configuring WordPress Site
Another way of serving static content cookieless is by reconfiguring the WordPress site. This method involves the creation of either a subdomain in case you use a domain like www.mydomain.com or getting a new domain in case you use top-level domain such as mydomain.com.
Creating a Subdomain
Create a subdomain and name it whatever you like for example static.mydomain.com. If you are using cPanel then head over to DOMAINS section and click on subdomain option.
Enter the name of your new subdomain and click on CREATE button to save the changes.
Cloudways users can create a subdomain using the application dashboard. Navigate to Domain Management tab and add additional domains.
Now point your subdomain to main domain via CNAME using DNS settings of your domain provider.
Next step will be to point this subdomain to the WordPress directory which is located inside the WordPress installation file in the public_html folder.
Add the code below in wp-config.php file which is also located inside the WordPress installation directory.
define(“WP_CONTENT_URL”, “http://static.mydomain.com”); define(“COOKIE_DOMAIN”, “mydomain.com”);
Update Existing Post Content
We have now configured our WordPress site to server static content from cookieless domain. But this will only be applicable for our future posts. In order to to update the existing posts simple open your database manager and navigate to tables. Open the query window and run the following command.
UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://www.mydomain.com/wp-content/uploads/','http://static.mydomain.com/uploads/')
Note: Replace ‘mydomain’ with your domain name.
Above query will update the existing posts so it does not generate the warning on the browsers. Now log in to your WordPress admin dashboard and check if the existing posts are loading their images from static.mydomain.com.
Google Analytics Settings
If your WordPress site used Google Analytics for tracking then you also have to make a few changes in the Analytics code and also make sure that the same settings are being used inside the Analytics account.
Inside the code change the the ‘auto’ property to your www.address of your website.
<script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-XXXXXXX-1', 'www.mydomain.com'); ga('send', 'pageview'); ga('set', 'displayFeaturesTask', null); </script>
Now go to Google Analytics account and navigate to ‘Admin’ menu.
Under ‘Property Settings’ tab, make sure that both the Property Name and Default Property URL has your www address of your website.
How to Solve CloudFlare Cookies Problem?
CloudFlare CDN is a great service and is used by many WordPress sites. Due to its structure CloudFlare CDN adds a cookie _cfduid with every HTTP request. These cookies can not be avoided using any official method available except for not using CDN service and stick to its fast DNS service.
Let’s dig deep and explore alternate method to get rid of these cookies.
- 1 – Avoid using it as CDN service for domain/subdomain from which you intend to serve static content and use it just as a DNS manager.
- 2 – If you still wish to use CloudFlare and avoid its cookies then use another domain based hostname such as StackPath to server static content. Use another subdomain for it such as abc-stackpath.com
Using CloudFlare as DNS Manager Only
In order to use CloudFlare as a DNS manager only, log into its dashboard and switch the status to ‘Paused’. Now follow the image below to configure DNS record.
Wait for DNS to get propagated and to confirm it check its status at whatsmydns.net
Final Thoughts
Today we explored the reason behind “Serve Static Content From a Cookieless Domain” warning and learned two methods to resolve this issue. CDN is a powerful utility which has tons of benefits in terms of website performance. Share your experience of implementing these two methods in the comments below.
Ibad Ur Rehman
Ibad Ur Rehman is a WordPress Community Manager at Cloudways. He likes to explore the latest open-source technologies and to interact with different communities. In his free time, he likes to read, watch a series or fly his favorite Cessna 172SP in X Plane 11 flight simulator.