BT
x Your opinion matters! Please fill in the InfoQ Survey about your reading habits!

Going Offline with LocalForage

by João Paulo Marques on Feb 26, 2014 |

The Mozilla Foundation has released localForage, a new JavaScript library that promises to simplify the process of storing offline data in web applications.

Although it's not a recent browser technology, offline support has always been too fragmented because of the many choices available. What makes this library unique is the fact that it tries to combine the best of both worlds: the features of some more recent technologies (asynchronism and blob support) with a simple API. This offers a powerful offline capability to web applications, making them closer to their mobile native counterparts and more intuitive for developers to work.

One of the first choices available was localStorage which provided a simple data access for offline storage. However benchmark tests have shown it to be slow, it's synchronous and it cannot handle binary blobs (for example, no mp3 files caching is possible). Since then, two other popular choices have emerged - IndexedDB and Web SQL - which are asynchronous, fast, and support large data sets. The drawback of these two technologies is the fact their APIs aren't very simple to use and neither are supported in all major browsers.

The recently release of localForage tries to overcome these problems by unifying technologies – asynchronism and blob support from IndexedDB and Web SQL with the very simple localStorage syntax:

var settings = {color: 'black', font: 'Helvetica'};
localForage.setItem('settings', settings, function(result) {
  console.log(result);
});

The inclusion of IndexedDB and Web SQL support allows one to store more data for the web applications than localStorage alone would allow. The non-blocking nature of their APIs makes the web applications faster by not hanging the main thread on get/set calls. Additionally, localForage supports callbacks and ES6 Promises leaving the choice of the best implementation to the developer.

The library automatically loads and manages the drivers for IndexedDB, Web SQL and localStorage so one don’t need to handle this manually (the best driver is chosen independently the browser where the application is running). When neither IndexedDB nor Web SQL are available, localForage falls back to localStorage so at least basic data can be stored offline (though with no blob support and much slower).

All modern browsers are supported by localForage. Asynchronous storage is available in all browsers, with their version that supports localStorage in parentheses:

  • Android Browser 2.1
  • BlackBerry 7
  • Chrome 23 (Chrome 4.0 with localStorage)
  • Chrome for Android 32
  • Firefox 10 (Firefox 3.5 with localStorage)
  • Firefox for Android 25
  • IE 10 (IE 8 with localStorage)
  • IE Mobile 10
  • Opera 15 (Opera 10.5 with localStorage)
  • Opera Mobile 11
  • PhoneGap/Apache Cordova 1.2.0
  • Safari 3.1

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT