DEV Community

Jess Chandler
Jess Chandler

Posted on

Should I install the package or use the CDN?

I'm thinking particularly about chartjs right now, but it could apply to many things

Top comments (4)

elmuerte profile image
Michiel Hendriks

As in most cases the answer is: It depends.

Even though CDNs are made to be reliable. It is a resource you have no control over. Maybe they will clean up the content after a while and remove (little used) resources. Maybe the CDN is blocked for the users. etc.

So, it depends:

  • how quickly can you adept your program to resolve CDN related issues
  • are there network accessibility constraints (think: corporate networks)

The company I work for makes so called enterprise software. So I have made it a rule to not use CDNs for anything we can host ourselves.

But for personal projects or PoCs I often do use CDNs. But once I start approaching shippable products I try to in-source the resources as much as possible.

ben profile image
Ben Halpern

I agree with all of this. I'll add that if you can add the npm package and include the library as part of your build with the rest of your code, your code should be cleaner. You can rely on imports instead of global variables.

All-in-all I'd call the CDN a quick and dirty version or a good solution depending on your needs, but when in doubt, make it part of your package.json.

Disclaimer: I'm always uncertain about anything in JS land these days 😄

rishibaldawa profile image
Rishi Baldawa

I'd recommend using CDN when you're just playing around. It's quick & cheap way to experiment but rely on packaging when you're deploying to something of importance (i.e. in production).

Packaging gives you dependency management, source control, static checks / analysis. If the cost of hosting the CDN is too high, then roll your own (or use of them CDN as service tools).

If you're in the corporate world, in either case you'll have to worry a lot more about licensing, source code correctness, security and accessibility of code / CDN.

eduardort profile image
Eduardo Reyes

It depends, there are many solutions to this.

You could create your own CDN (makes sense if you're not using HTTP/2)
You could use the provided CDN (please use a subresource integrity check)
You could combine the assets using a build system and serve that file (I recommend using some kind of cache busting and cache that big file locally)

Or just install the dependencies and serve them directly, it all depends how much effort you want to put into this and what you think it's gonna be valuable for you.