Hacker News new | ask | show | jobs
by guessmyname 2987 days ago
Years ago, I used to link the library from Google [1] or CloudFlare [2].

Nowadays, with all the Node.js stuff that goes around modern front-end, I don't see the point of embedding a JavaScript library from a CDN, unless that library is dependent on a remote service, e.g. Google Analytics, Google Maps, etc… That being said, if you are still maintaining a legacy website that depends on jQuery, you should consider to embed the library like this instead:

<script>window.jQuery || document.write('<script src="/js/jquery.min.js"><\/script>')</script>

[1] https://developers.google.com/speed/libraries/

[2] https://cdnjs.com/libraries/jquery

1 comments

> Nowadays, with all the Node.js stuff that goes around modern front-end, I don't see the point of embedding a JavaScript library from a CDN, unless that library is dependent on a remote service, e.g. Google Analytics, Google Maps, etc… That being said, if you are still maintaining a legacy website that depends on jQuery, you should consider to embed the library like this instead:

What does Node.js have to do with deciding whether to get your static assets from a public CDN or not? I hope your not serving your static assets with Node.js.

> What does Node.js have to do with…

There are tools like Grunt and WebPack (which depend on Node.js) that can bundle all your dependencies.

I cannot provide details about how they work because I don't do front-end development, but I can tell you about years ago when I had to copy & paste both code and links to jQuery and other libraries like BackBone or Ember.js (relevant at the time) into my projects. Nowadays, web developers seem to prefer the use of tools that came from the Node.js ecosystem to handle these dependencies in a more "engineer-ish" way using NPM packages.

Yea you can still not bundle the actual library and grab it from a CDN, using webpack externals[1]. Using webpack doesn't really change anything.

[1]: https://webpack.js.org/configuration/externals/

I assume he's talking about NPM. Now that everybody's hot new SPA has a few hundred thousand NPM dependencies, you roll it all up using Webpack and can just as easily `npm install jquery` as `<script src="cdn.jquery.com"></script>`.
> Now that everybody's hot new SPA has a few hundred thousand NPM dependencies, you roll it all up using Webpack and can just as easily `npm install jquery` as `<script src="cdn.jquery.com"></script>`.

Just because you can doesn't mean you should. You usually don't want some huge javascript bundle to load it's bad for page load performance. Also Webpack allows you to chose whether you want to bundle a dependency locally or grab it at runtime from a CDN or another sever.

Most of the dependencies are for the Node dev tooling and never gets deployed. You can easily build a lightweight Vue/React site with Webpack.