The CDN should be used for all static files (.css/.js/images).
Other benefits from using a CDN would be that the CDN server would most likely be located closer to your end users then your origin which will benefit loading times. The CDN servers are also likely setup to serve static content much faster then your origin server by specifically catering the web server for static content.
Using a CDN vs traditional web hosting for delivering your static files such as CSS, JS, and images is commonly preferred. This is because once your files are cached on the CDN's edge servers, your site visitors will be delivered static content from the closest point of presence (PoP) instead of the origin server.
In the majority of cases, this shortens the distance between the client and the server and thus helps improve loading times without adding any additional HTTP requests. This also helps in other areas such as increasing redundancy, taking a load off the origin, etc.
Hosting on a CDN has many disadvantages:
- Privacy. Everytime you go to a site that hosts scripts/stylesheets/fonts on a CDN, the CDN knows about you visiting the site.
- Offline time. In the last weeks cloudflare etc had some hours, where their loading time was very slow or even offline for some minutes. In the best case your site looks ugly, when that happens, in the worst its completely unuseable in a time, where your competitors have problems, too, and you could get customers of them if your site was online and theirs not.
- Security. The CDN can be compromised (it happened before). In the best case your server spreads malware then, in the worst your data is now public or gone.
Compared to that the advantages are insignificant:
- Less data to load: we talk about some 100kb of scripts per site. 1 image in low quality does account for more than that. If you dont have a website that is on its limits with like 5.000.000 visits per minute, it will not make a difference. And if, you probably should set up a better server environment with load balancer and more webservers, because using CDNs will not solve your problems.
- Faster loading times, because CDN servers are closer to the client: in times, where the latency from EU to US is about 100ms for nearly all connections, the css gets loaded in 50ms or 100ms.
Theres like zero reason for using a CDN on a production environment.
Use a CDN if you need a CDN. If your user is global and spread over a large area, or you have a lot of such content that you don't want to store on your own server, that is when a CDN is useful. Globally, it can speed up access to your content if the server is closer to the user. If you have many GB or Terabytes of static data and a heavy load for access to that content, a CDN can help with that.
However, small, local sites or lightly loaded sites rarely need such things and a CDN can only add one more complication to your set up, operation and workflow, such as caching problems.
Too often I see people use a CDN cause they read they should be using one and no other reason.
Using a CDN can be both a burden and a benefit to a website, all depending on how it has been implemented.
- Static content stored closer to the end user (faster loading times)
- Additional sub-domains (
cdn2.example.com, etc), this assists with the inherent limit in browsers where they limit file downloads to two simultaneous files from the same fully qualified domain name at any one time. In other words using this example you would access HTML from
www.example.comand all the while be downloading 2 files from
cdn1.example.com, 2 files from
cdn2.example.com, and 2 files from
cdn3.example.comwith all three CDN domains accessing the one CDN service and source.