Chapter 3. Local Storage, Sync Storage, and IndexedDB

This chapter covers three more storage types first introduced in Table 2-1 in Chapter 2: Local Storage, Sync Storage, and the IndexedDB database. All three store key-value pairs (KVPs), not files.

We’ll be extending the Simple Editor app from Chapter 2, using Local and Sync Storage. Then, I’ll introduce a mailing-list app to demonstrate what you can do with IndexedDB.

Local and Sync Storage

Many browsers, including Chrome, support localStorage, which is similar to persistent cookies in that it stores KVPs. The main difference is that localStorage has greater capacity and isn’t passed to the server along with HTTP requests. Similarly, sessionStorage is similar to session cookies in that it lasts only as long as the current session.

However, Chrome Apps can’t use localStorage. Instead, there’s a Chrome API, chrome.storage.local, that’s better: it can store JavaScript objects (localStorage is limited to strings) and, like most Chrome APIs, it operates asynchronously, allowing the app to be more responsive. What’s more, there’s a variant, chrome.storage.sync, with the same API, which is automatically synchronized between computers running the same app, similar to the way that Sync Files are synchronized.

Note

As is specified in Chapter 2, you need storage permission in your manifest to use the chrome.storage APIs.

Chrome Apps can use sessionStorage, but there’s no reason to do so. Because a Chrome App HTML page can’t be refreshed and can’t ...

Get Programming Chrome Apps now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.