James Parsons
James Parsons

Reputation: 6047

Do browser cache downloaded javascripts

A lot of the time, instead of downloading scripts, people will set the source as a fully qualified URL. As shown here:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.7/angular.min.js"></script>

But will the browser cache this file, or will it have to re-download it if the session is reset or if you load a new tab. Would using something like Web Jars or Bower for dependency management keep it from doing so if it did?

Upvotes: 0

Views: 80

Answers (1)

DanSingerman
DanSingerman

Reputation: 36502

The browser should cache javascript files according to their HTTP headers, just like any other resource.

Typically a CDN hosted file like you quoted will have a long cache time:

$ curl -I https://ajax.googleapis.com/ajax/libs/angularjs/1.3.7/angular.min.js
HTTP/1.1 200 OK
Vary: Accept-Encoding
Content-Type: text/javascript; charset=UTF-8
Last-Modified: Mon, 15 Dec 2014 23:03:29 GMT
Date: Tue, 16 Dec 2014 20:07:10 GMT
Expires: Wed, 16 Dec 2015 20:07:10 GMT
Access-Control-Allow-Origin: *
Timing-Allow-Origin: *
X-Content-Type-Options: nosniff
Server: sffe
X-XSS-Protection: 1; mode=block
Cache-Control: public, max-age=31536000
Age: 61071
Alternate-Protocol: 443:quic,p=0.02
Transfer-Encoding: chunked

Upvotes: 1

Related Questions