{"id":603,"date":"2012-05-05T12:42:53","date_gmt":"2012-05-05T12:42:53","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=603"},"modified":"2012-05-05T12:46:51","modified_gmt":"2012-05-05T12:46:51","slug":"sessionstorage-conserve-mobile-bandwidth","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/sessionstorage-conserve-mobile-bandwidth\/","title":{"rendered":"Using sessionStorage to Conserve Mobile Bandwidth"},"content":{"rendered":"<p>If you&#39;re website also offers a mobile version, it&#39;s likely that you are pulling much of your data for the mobile site from your &quot;desktop&quot; site. And, even more likely that you&#39;re using AJAX to do it.<\/p>\n<p>However, if you are loading static data repeatedly, you can imagine the unnecessary use of mobile bandwidth can add up to a slower user experience. The HTML 5 spec provides a means to improve this, by storing data in the web browser. This new method provides enhancements over cookies, in that it is safer and easier to access via JavaScript.<\/p>\n<p>Of course, when requesting data via AJAX, there is the option to cache that request in the browser. In the case of jQuery, the <a href=\"http:\/\/api.jquery.com\/jQuery.ajax\/\"><em>cache <\/em><\/a>parameter to the <em>$.ajax<\/em> method serves this purpose.<\/p>\n<p>However, depending on your server settings, the cache expiration might be a little &quot;strong&quot;.<\/p>\n<p>A session type storage is more suitable for a shorter expiration &#8211; the life of the user session. HTML 5 supporting browsers offer the sessionStorage and localStorage options along with their respective JavaScript methods. You can leverage <a href=\"http:\/\/www.w3schools.com\/html5\/html5_webstorage.asp\">sessionStorage<\/a> by first checking for the existence of a sessionStorage variable, and if it does not exist, perform the appropriate AJAX call to get the data. After which, you would store that data in the sessionStorage scope.<\/p>\n<h3>sessionStorage Example<\/h3>\n<p><code>function performAction() {<br \/>\n\t&nbsp;&nbsp;&nbsp; if (sessionStorage.myData == null) {<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $.ajax({<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; url: &#39;http:\/\/www.mysite.com\/getData.cfm&#39;,<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dataType: &#39;jsonp&#39;,<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; success: function(data) { <br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sessionStorage.myData = JSON.stringify(data);<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; performActionHelper();<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; });<br \/>\n\t&nbsp;&nbsp;&nbsp; }<br \/>\n\t&nbsp;&nbsp;&nbsp; else <br \/>\n\t&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; performActionHelper();<br \/>\n\t}<\/p>\n<p>\tfunction performActionHelper() {<br \/>\n\t&nbsp;&nbsp;&nbsp; \/\/ processing code here<br \/>\n\t&nbsp;&nbsp;&nbsp; console.log(myData);<br \/>\n\t}<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>There&#39;s a couple things to take note of here.<\/p>\n<ol>\n<li>Notice how we separate the processing into another function. This allows us to reuse the code as well as provide a means for the processing code to be executed asynchronously.\n<p>\t\tNote: You can&#39;t reliably create a function that makes an asynchronous AJAX call, then returns a value, as that value probably won&#39;t be available by the time the function returns.<br \/>\n\t\t&nbsp;<\/li>\n<li>sessionStorage currently can only store string data, so we must &quot;flatten&quot; the data, then parse it when needed (see use of <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/cc836459%28v=vs.85%29.aspx\">JSON.stringify<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/cc836466%28v=vs.85%29.aspx\">JSON.parse<\/a>)<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>If you&#39;re website also offers a mobile version, it&#39;s likely that you are pulling much of your data for the mobile site from your &quot;desktop&quot;&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[14,5,132,41],"tags":[53],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/603"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=603"}],"version-history":[{"count":2,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/603\/revisions"}],"predecessor-version":[{"id":605,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/603\/revisions\/605"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}