DEV Community

Prahlad Yeri
Prahlad Yeri

Posted on

Is using a CDN or hosting your own Fonts a better idea?

Lately, I've come across many articles which suggest that you host your own static assets (especially fonts and css) instead of using a worldwide CDN like CDNJS. The argument they cite is that your own site will be much faster than CDN, so the assets will load faster.

But my argument is that if I use a popular library like jquery or bootstrap, chances are that they've already been downloaded to the user's local machine through a CDN (because dozens of other websites the user may have visited use CDNs). So, the network cost is zero! But if I host a jquery.min.js or bootstrap.min.css specifically on my server, it'll be an additional toll on network resources, isn't it?

So, I think that hosting your own asset is an anti-pattern, what do you think?

Top comments (4)

Collapse
 
ahferroin7 profile image
Austin S. Hemmelgarn

Arguments in favor of hosting your own assets:

  • You can actually do custom versions of the assets. Bootstrap is probably the best example of this, as Bootstrap 4 pretty much requires you to have your own custom version of the CSS if you want a custom theme (and it looks like this probably won't change in Bootstrap 5, but might in Bootstrap 6 if they finally start using custom properties).
  • You aren't dependent on external infrastructure. Remember what happened recently when CloudFlare had issues?
  • You don't have to worry about leaking customer information to external sources. This sounds stupid, but may matter in some cases.
  • You can easily develop your site offline. You might be able to do this still with a CDN, but you have to be lucky for it to work, and also have to remember not to use a hard refresh.

Arguments in favor of using CDN-hosted assets:

  • If it's a common asset, the client system may avoid making a network request.
  • Even if it's not a common asset, it's less disk space and network bandwidth needed for your server.

Also, note that both arguments favoring CDN hosting become a bit weaker if you're doing a PWA and your Service Worker is sanely handling the caching.

Collapse
 
rhymes profile image
rhymes
Collapse
 
neverendingqs profile image
Mark Tse

Possible trade-off: increased risk that your site goes down if the CDN goes down.

Collapse
 
andyli profile image
Andy Li

Kind of related: You cannot develop your CDN-using site when you're offline.

I found that out when I was on a 10-hour flight. Wanted to improve the layout but found that everything was off since I could not load bootstrap...