Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

so here is a little mystery. google analytics javascript somehow always loads from browser cache on F5 (in Firefox). i cannot get the same thing to happen with my own scripts.

here are the headers that come back on initial request:

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

http://my-site.com/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's 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'm 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
// ...

thanks!
leon

share|improve this question

3 Answers 3

Without seeing the headers for when you reloaded the page it is a little hard to double check some things.

One thing I do notice is that the dates/times don't seem correct (though this may just be an artefact of how you copied and pasted your question). The response from Google's server says the Date is Thu, 01 Mar 2012 20:58:10 GMT while your js says it is Thu, 01 Mar 2012 21:20:16 GMT. If my reading of this page is correct, if your server sets Date headers that are in the future as far as your personal machine is concerned, then the calculated current_age of your cached copy will always be 0 (rounded up from a negative number). This would cause your browser to attempt to validate it by doing a conditional GET which shows up as 304 Not Modified. Google also may be slightly out of sync with your personal machine but with them the Age header is being set and this is used when available if the calculated age value is less that it.

If this is the case, make sure that your web server (and your personal machine) are syncing their time to an internet time server. Look for a regional time server pool and get your machines to sync to them.

This may not be correct, and I don't have enough rep here to comment on your original question so I have had to add it as a straight up answer.

share|improve this answer

Google's code is being called from:

<script src="http://www.google-analytics.com/ga.js" type="text/javascript"></script>

If you'll put your JS code in a file and call it the same way:

<script src="myscript.js" type="text/javascript"></script>

Your script will be cached as well. Check the last paragraph here

share|improve this answer
    
i am actually loading the tracking script (as well as all others) asynchronously using LABjs, so there is no difference in the way they are called. i'm not using google's stock snippet. i will update the OP with my exact code. –  leeoniya Mar 5 '12 at 18:17

On some browsers and in some circumstances (I haven't actually examined the code in sufficient detail to be sure which), LABjs uses different methods for preloading same-domain and cross-domain scripts. It's possible that the preloading method chosen by LABjs for same-domain scripts is suboptimal on your browser.

If so, you might want to report it to the LABjs devs. (Preferably after searching to check that it's not already a known issue, of course.)

share|improve this answer
    
a valid point. however, the jquery loaded from the cross-origin CDN gets 304-poked also. i don't see any domain favoritism coded in there, so would probably rule out LABjs as the culprit. Chromium does seem to load a lot more from cache, even on F5. original test was in FF. maybe i'll check bugzilla. –  leeoniya Mar 5 '12 at 23:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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