Jump to content

Best way to load js libraries from CDN or local


Juergen
 Share

Recommended Posts

Hello @ all,

I have included all my external JS files via npm which makes updating of every script very easy. A lot of these JS-libraries are also available on a CDN. My thought was to load the scripts from a CDN if they are present, otherwise load it from my local folder.

There are some scripts such as steal.js (https://stealjs.com/docs/StealJS.loading-from-cdn.html) which can be used, but I want to know how others deal with this issue.

The best would if the version of the local npm-folder (fe. jquery 2.1.0) would be checked and then the same version would be loaded from the CDN if present, so the local and the CDN version are always the same.

Most of the solutions need a hardcoded CDN version that should be fetched. I dont know if this scenario is possible, but maybe someone come up with a good solution.

Best regards

Link to comment
Share on other sites

Same here. Also, I like to minify + concat everything and deliver just one css and one js file. (AIOM is handy for that, or you can use any Grunt/Gulp task for that)

But when I'm in the early stages of setting up a site / app, or just want to play around with different libraries, I usually load everything from CDNs. 

Also, when it comes to upgrading frontend assets, I don't monitor every new release and run for the latest version. Priority number one is stability. Only when there's a good reason (security bugfixes or major new features that I know I will use), I upgrade JS stuff.

  • Like 2
Link to comment
Share on other sites

19 minutes ago, pwired said:

I guess loading from a url makes it a bit slower and gives more overhead.

The browser is already loading stuff from a URL, yours. :huh:

CDNs do have advantages, that's why they exist. :rolleyes:

Link to comment
Share on other sites

Quote

The browser is already loading stuff from a URL, yours.

The js wouldn't be loaded from that url, but from another url, so more dns resolvement and traffic from another server.

Same like dragan I go for stability and not for every latest and newest.

  • Like 1
Link to comment
Share on other sites

There are as always Pros and Cons about using a CDN. One of the thing I like with CDN is the multiple-request capability.

But on other hand, in a security point-of-view, using a CDN if you are paranoïd will make you crazy knowing that someone could modify the delivered JS code to collect data from your users...

  • Like 2
Link to comment
Share on other sites

I am no expert, but I though the two key advantages to CDNs were:

1) Browser caching - if someone already has http:://wwwcdn.com/jquery.min.js cached in their browser they won't need to load it again - this is probably the biggest reason isn't it?

2) CDN servers might be faster than your cheap shared hosting?

  • Like 1
Link to comment
Share on other sites

1 hour ago, adrian said:

I am no expert, but I though the two key advantages to CDNs were:

1) Browser caching - if someone already has http:://wwwcdn.com/jquery.min.js cached in their browser they won't need to load it again - this is probably the biggest reason isn't it?

2) CDN servers might be faster than your cheap shared hosting?

That's 100% correct!

Link to comment
Share on other sites

12 hours ago, pwired said:

Do you mean during the time of developing a website ?

I mean on production sites. I always use local stuff on development. In the past I have loaded files from different CDNs (Google, Jsdelivr,...). So I could use parallel downloads instead of serial, which should make the site faster.  My strategy was always to load a JS from CDN if possible. If not than fallback to the local version on the server. This needs to look manually if a specific or newer version is available on the CDN.

So I am interested in how others deals with this issue and I have read a lot of interesting thoughts here. :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...