I am building a mobile website that uses the main jquery library and some js of our own. Our website is too big and has too much data to be a simple offline/online web app. We need web connectivity.
I’m trying to improve caching performance for caching a large amount of javascript for a mobile site. It is well known that that caching on the iPhone’s safari is limited to files 15-25kb in size, and our minified js is around 125kb.
I’ve thought about using the cache manifest, but this has the disadvantage that the browser requests the cache-manifest with each page load, and since we aren’t using a single page web app, this adds additional requests to the server.
Can we cache the javascript in localStorage (available in mobile safari and on the android browser) and then execute it from there?
Yes, you can. (sorry for answering my own question, I thought this was an interesting solution)
I found an outline of a code example here on slide #12.
http://www.slideshare.net/jedisct1/abusing-javascript-to-speedup-mobile-web-sites
I’ve implemented this on http://m.bbref.com/ (still in beta)
You have to use versioning of the script url to flush the cache when a new version is created, but this works for pages with localStorage and will also work when localStorage is not available. I’ve added some debugging code to the footer to show you where the js is loading from.
I’ve split this into a script for the header and one of the footer. These appear inline.
In the header (I’ve put it here as we use modernizr to add some classes to the html tag and I want those there as quickly as possible for rendering purposes. Could be moved to footer.
and in the footer (for performance reasons). I place the standard loading method. Note that the browsers that use this branch all cache properly so long as you have expires headers set.
I’ve confirmed in chrome and safari that the js is loaded from localStorage and the site functionality works as expected, and there is no request made to the server. And I’ve confirmed that when running on IE or firefox it loads the script as found in the footer.
Note: I added code to wrap the evals in try catch since I was having a problem in firefox.