Why does the browser not load the cdn file from the cache?

Here is a very simple example to illustrate my question using jQuery from the CDN to change the page:

<html> <body> <p>Hello Dean!</p> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script>$("p").html("Hello, Gabe!")</script> </body> </html> 

When this page loads with an Internet connection, the page displays "Hello Gabe." When I disconnect the Internet connection, the message "Hello Dean" will appear on the page with an error - JQuery is not available.

I understand that CDNs have a long Cache-Control and Expire header response, which, as I understand it, means that the browser caches the file locally.

 $ curl -s -D - https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js | head HTTP/1.1 200 OK Server: cloudflare-nginx Date: Fri, 17 Apr 2015 16:30:33 GMT Content-Type: application/javascript Transfer-Encoding: chunked Connection: keep-alive Last-Modified: Thu, 18 Dec 2014 17:00:38 GMT Expires: Wed, 06 Apr 2016 16:30:33 GMT Cache-Control: public, max-age=30672000 

But this does not seem to be happening. Can someone explain what is happening? Also - how can I get the browser to use a copy of jQuery in the cache somewhere?

This question arose because we want to use CDN to serve external libraries, but also want to be able to develop the page offline - for example, on an airplane.

I get this behavior using Chrome and Firefox.

+7
html5 caching browser-cache cdn offline-caching
source share
2 answers

There is nothing to do with CDN. When the browser then encounters the script tag, it will request it on the server, whether it is hosted on a CDN or on your server. If the browser previously downloaded it at the same address, the server tells whether to restart it or not (sending 304 HTTP status status ).

What you're probably looking for is a cache application for offline use. This is possible with the HTML5 cache manifest file . You need to create a file that lists all the files that you need to cache for explicit offline use.

+2
source share

Since the previous answer recommended using the cache manifest file, I just wanted people to know that this feature is being removed from web standards.

Information from Mozilla:

https://developer.mozilla.org/en-US/docs/Web/HTML/Using_the_application_cache

It is recommended that service workers be used in place of the cache manifest.

0
source share

All Articles