So here is a little mystery. Google Analytics javascript somehow always loads from browser cache on F5 (in Firefox). I can not get the same thing to happen with my own scripts.
Here are the headers that come back on initial request:
For http://google-analytics.com/ga.js
:
(Status-Line) HTTP/1.1 200 OK
Last-Modified Thu, 16 Feb 2012 00:48:45 GMT
X-Content-Type-Options nosniff, nosniff
Date Thu, 01 Mar 2012 20:58:10 GMT
Expires Thu, 01 Mar 2012 22:58:10 GMT
Content-Type text/javascript
Vary Accept-Encoding
Age 1326
Cache-Control max-age=7200, public
Server GFE/2.0
Transfer-Encoding chunked
Connection close
For http://my-site.example/myscript.js
:
(Status-Line) HTTP/1.1 200 OK
Server nginx
Date Thu, 01 Mar 2012 21:20:16 GMT
Content-Type application/javascript
Last-Modified Fri, 10 Feb 2012 17:50:27 GMT
Transfer-Encoding chunked
Connection keep-alive
Vary Accept-Encoding
Expires Thu, 01 Mar 2012 23:20:16 GMT
Cache-Control max-age=7200, public
Content-Encoding gzip
When I press F5 on my page, ga.js
just loads from cache, but myscript.js
gets requested and comes back with 304 Not Modified
.
What is the magic combo here? And how do I make it load from cache without a server trip until it expires?
EDIT
I am NOT using Google's stock tracking snippet. I am using the following loading code:
<script src="/assets/js/vendor/LAB.min.js"></script>
<script>
$LAB
.script("//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js").wait()
.script("/assets/js/common.js?abc123").wait()
.script("/assets/js/home.js?abc123")
.script("/assets/js/stats.js?abc123");
</script>
stats.js
// Google Analytics
var _gaq = [["_setAccount", "UA-XXXXXXXX-1"], ["_trackPageview"]];
$LAB.script("//google-analytics.com/ga.js");
// WebSTAT
// ...