Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thebigk's avatar
Level 13

NPM install vs pulling from a CDN

I was thinking if it's wiser to install the dependencies like jQuery, Vue, Boostrap locally via NPM or simply refer them in our blade templates via CDN? Ain't the CDN approach better in general?

0 likes
4 replies
bobbybouwmann's avatar

Although a CDN might be easier, it's still another request to a server. Your browser can currently do 5 requests at the time. This means that if you load multiple resources using urls you need to wait a bit until the whole page is loaded. When you minify and compile everything together you only need to include one file. This might sound better, but the file is bigger which might take a bit longer!

My recommendation: For development and trying out stuff a CDN is fine. For an actual application I would advise you to compile and minify everything into one file and include that ;)

2 likes
thebigk's avatar
Level 13

Thanks, @bobbybouwmann . How about serving the compiled css and js through CDN? It will still make an extra request for each; but if the file size is big, better upload it to CDN.

MikeHopley's avatar

This can easily become a bikeshedding topic, but here are some things to consider:

HTTP/2 reduces the benefit of combining files. In this case, you are probably better off not bundling libraries together with your own javascript, and you may even want to split your javascript into modules -- depending on the usage pattern across the site. This can help with caching.

CDNs cache files in locations around the world ("edge caching"), i.e. closer to the user than your own server. This reduces latency.

...however, each CDN incurs a DNS lookup, so you probably don't want lots of different files hosted by lots of different CDNs. Also consider that the more CDNs you use (or just domains in general), the more potential points of failure you have.

You probably have other static files anyway, so ideally these would be served from a CDN. In that case, why not move stuff like jQuery onto the same CDN that you use for your general files? This reduces domain dependencies and DNS lookups.

You also have to consider the cost/time/maintenance involved in these performance optimisations, versus how much benefit you're getting. With some techniques there's a good difficulty/benefit trade-off (e.g. gzip), with other not so much (e.g. critical CSS).

1 like

Please or to participate in this conversation.