As some js files are very common and widely used on the web, why browsers don't reutilise them?

wouldn't it improve efficiency, as js framework files are usually heavy?


They are. If you link to the javascript as hosted by an external source such as ajax.googleapis.com and you have previously got that file while browsing another webpage, your browser will use the cached version (assuming all other cache settings are OK).

Obviously though you are now using google' bandwidth to download that file. Although the overall bandwidth used amongst all websites is less that if each one hosted the same file. Google is paying for more of it.

So you can see that if you are the person hosting the javascript file, you need to be getting some benefit out of it yourself. So for paid products, or advertising stuff where there is value in the metadata of the request for the JS file you do see this happening.

But generally, if you have published a JS framework of some kind, it would cost you money to host the file in a hot linkable way.


One reason is that there is no web of trust in place that would facilitate using code from one source in the rendering of a website from another source. Right now all the code linked in a HTML document is (supposed to) running in a sandbox where it pretty much only access data and functions related to that document and some things about the browser.

What you don't want is that module leftpad from superawesomecompany.com is used widely on the net by various other sites due to its utility, but then it turns out that they secretly phone home all your credentials. If you include the actual code to run in each website, you can at least be sure no sudden changes are snuck in. If you just import leftpad in your code and the code is dynamically loaded at runtime, you can only trust in superawesomecompany and hope they're not evil.

  • 1
    There are techniques like Subresource Integrity that prevent an external resource from changing without detection, but that can be tricky to use. – amon Apr 2 at 11:23

If your site not only depends on your server, but N other servers, you have N additional bottle necks, possible break points, especially if some library becomes popular. Their servers are not necessarily very commercial. They might even blacklist such an abuse of their resources.

Also you do not want to give them statistics and other info on your site.

And hackers then have fruitfull access points for attacks. In the past some PHP frameworks (as PHP was/is popular) had such backdoors.

  • whell I was thinking more about catching the data on the browser. It would be more a browser feature than a server one. – Adrian Godoy Apr 2 at 11:49
  • But the browser would from time to time read from a different site/server. So the browser would need to load from several servers. That already works for foreign URLs in ones web page. And they are cached with a refresh/life time as the delivering server said. – Joop Eggen Apr 2 at 11:57
  • 2
    @AdrianGodoy Browsers do cache a lot of stuff. The biggest killer of this, however, is files that are hosted and unrecognizable as an already-cached file. E.g. We use webpack and plugins therein, which takes all our fancy react+typescript and libraries, and bundles them up into one file. Thing is, it's now one file to download. Chrome can't see that there's a piece of that that's React and avoid downloading that part. Our servers can't ask if there's already a React in the cache to re-bundle without React. It's one of the cons of bundling, that browser cache doesn't help your first load – Delioth Apr 2 at 14:39
  • @Delioth It seems to me that as an industry using known CDN for things like Jquery, react, angular... could improve first loads of thousands of web pages. hence my question. – Adrian Godoy Apr 3 at 15:07
  • @AdrianGodoy While using CDN can help with first pageload for caching, there's also drawbacks on it. It means there's another point of possible failure - security breach (MitM), loss of service, or whatever else. If we use a CDN I have no real control over the js the client gets - does it support their browser? There's nothing telling me about new versions or getting new versions (a la NPM). Or managing the dependencies. And what happens when client doesn't have my version of Angular, but my code (which doesn't make sense without Angular) loads before Angular's done loading? Easier to bundle. – Delioth Apr 3 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.