Are libraries downloaded multiple times?

12

I have an application that uses 3 libraries (Bootstrap, jQuery, jQueryUI) on 10 different pages. Do browsers cache libraries?

    
asked by anonymous 06.01.2015 / 17:57

2 answers

15

If they are coming from the same URL and there is no indication / configuration otherwise it is to cache certain policies set by browsers / users.

To use what is cached a read attempt is made and should return 304 Not modified .

This is why it is recommended to use a default location where these libraries already exist. So your website does not need to download what's already on the user's browser. Even if it was originally downloaded by another website .

It usually "cheaper" to load a full library of the Google CDN or own library or some other site than to make a customized version of it only with what you use.

Caching tutorial .

    
06.01.2015 / 18:03
12

In general, browsers do cache. Eventually they check for changes and download the file again, but most of the time you do not have to worry so much about performance.

If you want to change the default behavior, you can force this on your system or your browser by changing the HTTP headers Expires and Cache-Control . This is more than enough for an intranet.

On the internet you can use a link to a CDN repository, which contains the most common libraries. That way you save bandwidth and leverage the cache that may have been made in accessing other sites.

In the case of jQuery, for example, you can use the CDNs available on the library website .

It's important to note that, unlike known libraries, caching specific scripts can be a problem if you make small changes over time. It is common for users to complain about bugs that appear soon after updating the system version, and the solution usually clears the cache.

To avoid this, one of the techniques used is to append a version number as a URL parameter by including the specific script. Example:

<script type="text/javascript" src="app-script.js?ver=1.1">
Updating the version along with the new releases system will force the browser to download the new versions of the scripts.

    
06.01.2015 / 18:13