OpenReplay
Navigate back to the homepage
BLOG
Browse Repo
Back

The Ultimate Guide to Browser-Side Storage

Craig Buckler
July 14th, 2021 · 9 min read

It’s usually necessary to store user data server side in a database or similar repository. This guarantees persistence and ensures users can access their data from any web-connected browser (presuming your storage system is reliable, of course!)

Storing data in the browser becomes a more viable option when:

  1. You’re retaining an application’s state, such as the active panel, chosen theme, input options, etc.

  2. You’re storing data locally, perhaps for practicality, performance, privacy, or pre-upload reasons.

  3. You’re creating a Progressive Web App which works offline and has no server-side requirements other than the initial download and updates.

The data you store in the browser will either be persistent (retained until you delete it) or temporary (deleted when the user’s session ends). Browsers are more complex:

  • The OS, browser, plugins, or user can block or delete data depending on the storage space available, supported technologies, vendor policies, configuration settings, etc.

  • Temporary data is often retained beyond the session. For example, you can close then reopen a browser tab and data should remain available.

In all cases, data is domain-specific: values stored by one domain (or sub-domain) cannot be read, updated, or deleted by another. That said, a third-party page script loaded from another domain has the same level of access as any of your scripts.

To help you choose the best option, browser storage options below are split into three categories:

  1. Current options. APIs you can use today in all modern and most older browsers.

  2. Future options. Experimental APIs which should be stable within a few years.

  3. Past options. Deprecated APIs you should avoid at all costs!

But first, a couple of tips that will help you analyze your current situation and decide what to do with it.

DevTools analysis

The DevTools Application panel (Storage in Firefox) permits viewing, modification, and deletion of most storage containers. The Network panel is also useful when examining downloaded data or cookies sent by HTTP requests and responses.

Storage space analysis

You can verify storage API support by looking for specific properties or methods of the window object, e.g.

1// is localStorage available?
2if (typeof localStorage !== 'undefined') {
3
4 // yes!
5
6}

It’s more difficult to check when a data store has space available. You can use a try/catch block to detect failures:

1try {
2 localStorage.setItem('myObject', myObject);
3}
4catch (e) {
5 console.log('localStorage failed:', e);
6}

Alternatively, you can use the Promise-based Storage API to check the remaining space for Web Storage, IndexedDB, and the Cache API. The asynchronous .estimate() method returns:

  1. a quota property: the space available to the domain, and
  2. a usage property: the space already in use.
1(async () => {
2
3 // Storage API support
4 if (!navigator.storage) return;
5
6 const storage = await navigator.storage.estimate();
7
8 console.log(`permitted: ${ storage.quota / 1024 } Kb`);
9 console.log(`used : ${ storage.usage / 1024 } Kb`);
10 console.log(`% used : ${ Math.round((storage.usage / storage.quota) * 100) }%`);
11 console.log(`remaining: ${ Math.floor((storage.quota - storage.usage) / 1024) } Kb`);
12
13})();

This API is not supported in Safari or Internet Explorer. Neither will it help you decide what to do when storage runs out! You may need to consider server-side storage but retain a proportion of frequently-used data locally.

Current storage options

The following client-side storage APIs are available in all browsers going back a decade or longer. They should pose no problems in modern applications or IE11.

1. Variables

JavaScript variables are the fastest storage option but they’re highly volatile. The browser deletes data when you refresh or navigate away. Variables have no storage limit (other than your computer’s memory limit), but the browser will slow down as you fill the available memory.

1const
2 a = 1,
3 b = 'two',
4 c = {
5 one: 'Object',
6 two: 'three'
7 };

Use when:

  • you’re writing any client-side JavaScript
  • temporary data is adequate. In situations where you need permanent storage, you can write variables to a data store when the page unloads

Advantages:

  • if you’ve written any JavaScript, you’ve used variables already
  • the fastest and simplest option
  • no need for serialization or de-serialization (converting to a JSON string and back again)

Disadvantages:

  • data storage is temporary
  • third-party scripts can examine and overwrite global values.

2. DOM Storage

While not strictly a storage method, you can append values to any DOM node in named attributes or as a property of that object, e.g.

1// locate a DOM node
2const element = document.getElementById('mycomponent');
3
4// store values
5element.myValue1 = 1;
6element.setAttribute('myValue2', 2);
7element.dataset.myValue3 = 3;
8
9// retreive values
10console.log( element.myValue1 ); // 1
11console.log( element.getAttribute('myValue2') ); // "2"
12console.log( element.dataset.myValue3 ); // "3"

It’s fast and there are no strict limits, although it’s not ideal for large volumes of data. Third-party scripts or a refresh wipes the data, although the server can write attributes to the HTML again.

Adding a property (element.myValue1) makes no change to the HTML itself and uses the prototypal nature of JavaScript objects.

Alternatively, you can set and get element attributes with:

  1. .setAttribute() and .getAttribute(). Be careful not to use an HTML attribute with associated functionality such as disabled or hidden.

  2. the dataset property. This appends an attribute prefixed with data- so it will not have default functionality.

  3. the classList API to add or remove classes from the class attribute. These cannot (easily) have a value assigned so are most practical for retaining Boolean variables.

All three options convert the value to a string so deserialization may become necessary.

Use when:

  • it’s practical to store a component’s state in its HTML or DOM representation
  • you want to pass data from the server to a component in HTML attributes

Advantages:

  • you can set values client side or server side
  • still fast if you can avoid DOM node searches and triggering layout changes

Disadvantages:

  • temporary data only
  • attribute strings may require serialization and deserialization
  • third-party scripts can examine and overwrite attribute values.

3. Web Storage (localStorage and sessionStorage)

Web Storage offers two APIs to get and set string-only name/value pairs:

  1. window.localStorage for persistent data, and
  2. window.sessionStorage for temporary session data.

The browser limits each domain to 5MB and, unusually, read and write operations are synchronous so it can delay other JavaScript processes.

The API is one of the easiest and well supported with .setItem(), .getItem(), and .removeItem():

1localStorage.setItem('value1', 1);
2localStorage.getItem('value1'); // "1"
3localStorage.removeItem('value1'); // gone

You can iterate though all items using .length (the number of items) and .key(N) (the name of the key N), or delete everything with .clear().

Changing a value raises a storage event in all other browser tabs/windows on the same domain so your application views can update as necessary:

1window.addEventListener('storage', e => {
2
3 console.log(`key changed: ${ e.key }`);
4 console.log(`from : ${ e.oldValue }`);
5 console.log(`to : ${ e.newValue }`);
6
7});

Use when:

  • you need to store small quantities of simple values and access them infrequently

Advantages:

  • simple API which is popular among developers
  • good browser support

Disadvantages:

  • strings only: serialization and de-serialization may be necessary
  • unstructured data, easy to overwrite a name used by another component
  • saving large datasets will affect page performance

4. IndexedDB

IndexedDB provides a NoSQL-like name/value database which can store at least 1GB and even reach 60% of the remaining disk space. You can apply indexes, use transactions, and search using a fast asynchronous API. Browser support is good (IE10+) and you can store JavaScript values natively without string serialization or de-serialization. It’s ideal for storing large quantities of state data

So why do few developers adopt IndexedDB?

The main reason is the API. It’s old, requires callback juggling, and feels inelegant when compared to modern async/await operations. You can Promisify it with wrapper functions, e.g. create a database connection when passed a name, version number, and upgrade function called when the version changes:

1// connect to IndexedDB database
2function dbConnect(dbName, version, upgrade) {
3
4 return new Promise((resolve, reject) => {
5
6 const request = indexedDB.open(dbName, version);
7
8 request.onsuccess = e => {
9 resolve(e.target.result);
10 };
11
12 request.onerror = e => {
13 console.error(`connect error: ${ e.target.errorCode }`);
14 };
15
16 request.onupgradeneeded = upgrade;
17
18 });
19
20}

Then connect and initialize a mystore object store:

1(async () => {
2
3 const db = await dbConnect('db', 1.0, e => {
4
5 db = e.target.result;
6 db.createObjectStore('mystore');
7
8 })
9
10})();

Use the db connection to .add new data items in a transaction:

1db.transaction(['mystore'], 'readwrite')
2 .objectStore('mystore')
3 .add('the value', 'key1')
4 .onsuccess = () => console.log( 'added' );

or retrieve values:

1db.transaction(['mystore'], 'readonly')
2 .objectStore('mystore')
3 .get('key1')
4 .onsuccess = data => console.log( data.target.result );

Use when:

  • you have lots of data to store
  • performance is a priority

Advantages:

  • a NoSQL database with transactions, indexing, and search options
  • no need to serialize or de-serialize data

Disadvantages:

  • the API is awful. You’ll want a wrapper library such as idb to make it usable.

5. Cache API

The Cache API stores HTTP request and response objects. It’s primarily used in Progressive Web Apps to cache network responses so an app can serve cached resources when it’s offline. It’s not practical for storing other types of data.

The cache storage size depends on the device. Chrome-based browsers typically permit 100MB per domain, but Safari limits it to 50Mb and expires data after 14 days.

You’re unlikely to encounter the Promise-based Cache API outside Service Workers, but you can store the content of a request in a named cache:

1// open a cache
2const cache = await caches.open( 'myCache' );
3
4// fetch and store response
5await cache.add( '/myfile.json' );

and retrieve it later:

1const
2
3 // open cache
4 cache = await caches.open( 'myCache' ),
5
6 // fetch stored response
7 resp = await cache.match( '/myfile.json' ),
8
9 // get content as text
10 response = await resp.text();

Use when:

  • caching files for offline usage
  • you want to improve web performance

Advantages:

  • good Promise-based API

Disadvantages:

  • impractical for storing anything other than HTTP responses
  • some Apple-imposed restrictions.

6. Cookies

Cookies have a bad reputation, but they’re essential for any web system which needs to maintain server/browser state such as logging on. Strictly speaking, cookies are not a client-side storage option since the server and browser can modify data. It’s also the only option which can automatically expire data after a period of time.

A domain can store no more than 20 named cookies with a maximum string of 4Kb in each. This is a restrictive 80Kb limit, but every HTTP request and response sends the cookie data. If you set a total of 50Kb then request ten 1-byte files, it would incur more than 1MB of cookie-specific bandwidth.

The JavaScript document.cookie API is simplistic with name/value pairs separated by an equals symbol (=):

1document.cookie = 'cookie1=1';
2document.cookie = 'cookie2=two';

Values cannot contain commas, semicolons, or whitespace so the encodeURIComponent() may be necessary:

1document.cookie = `cookie3=${ encodeURIComponent('Hello world!') }`;

You can add further options with semi-colon separators:

optiondescription
;domain=cookies are only accessible to the current domain, but ;path=site.com permits use on any subdomain
;path=cookies only available to the current and child paths, ;path=/ permits use on any path
;max-age=the cookie expiry time in seconds
;expires=a cookie expiry date, e.g. ;expires=Mon, 12 July 2021 11:22:33 UTC (format with date.toUTCString())
;securesend the cookie over HTTPS only
;HTTPOnlycookies are server-only and not available to client-side JavaScript

For example, set a state object accessible on any path which expires in 10 minutes:

1const state = { id:123 };
2
3document.cookie = `state=${ encodeURIComponent(JSON.stringify(state)) }; path=/; max=age=600`;

To examine cookies, you must parse the long document.cookie string to extract name and value pairs using a function such as:

1const state = cookieValue('state');
2
3// cache of parsed cookie values
4let cookieCache;
5
6// return a cookie value
7function cookieValue(name) {
8
9 if (!cookieCache) {
10
11 cookieCache = cookieCache || {};
12
13 // parse
14 document.cookie
15 .split('; ')
16 .map(nv => {
17
18 nv = nv.split('=');
19 if (nv[0]) {
20
21 let v = decodeURIComponent( nv[1] || '' );
22
23 try { v = JSON.parse(v); }
24 catch(e){}
25
26 cookieCache[ nv[0] ] = v;
27
28 }
29
30 });
31
32
33 }
34
35 // return cookie value
36 return cookieCache[ name ];
37
38}

Use when:

  • you need to maintain small quantities of state data between the client and server - such as a session ID

Advantages:

  • essential for logins
  • current session by default, but expiry control with max-age and Expires

Disadvantages:

  • limited space
  • poor JavaScript API
  • requires string serialization and de-serialization
  • browsers and plugins can block cookies
  • legislation may require a warning or opt-in for non-essential cookies, although this usually applies to whatever storage option you choose.

Future storage options

The following client-side storage APIs are new. They have limited browser support, may be behind experimental flags, and are subject to change. There’s no guarantee they’ll ever become a Web Standard so avoid using them for mission-critical applications.

7. File System Access API

Browsers run in an isolated sandboxed environment which prevents malicious sites accessing personal data stored on your hard drive.

The downside is that you cannot ‘edit’ a local file. Consider a web-based text editor. You must upload a file to that application, edit it, then download the updated version. And what if you’re working on several files? You could store the file in the cloud or use synchronization services such as Dropbox and OneDrive, but those can be impractical or overkill for simple apps.

The new File System Access API allows a browser to read, write, update, and delete from your local file system when you grant permission to a specific file or directory. A returned FileSystemHandle allows your web application to behave like a desktop app.

The following example function saves a Blob to a local file:

1async function save( blob ) {
2
3 // create handle to a local file
4 const handle = await window.showSaveFilePicker();
5
6 // create writable stream
7 const stream = await handle.createWritable();
8
9 // write data
10 await stream.write(blob);
11
12 // close the file
13 await stream.close();
14}

Use when:

  • you want a web app to read and write local files
  • you’re happy to wait a couple of years before browser support is viable

Advantages:

  • some exciting possibilities
  • web apps can compete with native apps
  • less need to upload files or process data on a server

Disadvantages:

  • little browser support - partial implementation in Blink but behind a flag
  • the API is subject to change.

8. File and Directory Entries API

The File and Directory Entries API provides a domain-specific virtual local file system where a web app can read and write files without having to request user permission.

While some support is available in most browsers, it’s not on the Web Standards track, and could easily slip into the “storage options to avoid” category. The API is unlikely to be practical for a number of years.

Use when:

  • you like living dangerously!

Advantages:

  • a virtual file system is safer than a real one

Disadvantages:

  • non-standard, implementation incompatibilities, subject to change, and may never receive good browser support.

Open Source Session Replay

Debugging a web application in production may be challenging and time-consuming. OpenReplay is an Open-source alternative to FullStory, LogRocket and Hotjar. It allows you to monitor and replay everything your users do and shows how your app behaves for every issue. It’s like having your browser’s inspector open while looking over your user’s shoulder. OpenReplay is the only open-source alternative currently available.

OpenReplay

Happy debugging, for modern frontend teams - Start monitoring your web app for free

Past storage options

The following client-side storage APIs are old, deprecated, or hacks. They may continue to work, but are best avoided for new projects.

9. window.name

The window.name property sets and gets the name of the current window. This was typically used to keep track of several windows/tabs, but the browser retains the string value between refreshes or linking elsewhere.

Bizarrely, window.name will accept a few megabytes of string data:

1let value = { a:1, b:2, c:3 };
2window.name = JSON.stringify( value );

which you can read back at any point:

1let newValue = JSON.parse( window.name );

window.name was never designed for data storage but was often used as a hack or to polyfill localStorage APIs in IE7 and below.

Use when:

  • you’re supporting very old browsers and there’s no other option
  • you’re not storing sensitive information

Advantages:

  • easy, session-only data storage

Disadvantages:

  • requires string serialization and de-serialization
  • any other domain can read or change window.name when you link elsewhere.

10. WebSQL

WebSQL brought SQL-like database storage to the browser. It had a limit of 5MB and a reputation for poor performance, but the API was familiar to anyone using SQL databases on the server:

1// create database (name, version, description, size in bytes)
2const db = openDatabase('todo', '1.0', 'to-do list', 1024 * 1024);
3
4db.transaction( t => {
5
6 // create table
7 t.executeSql('CREATE TABLE task (id unique, name)');
8
9 // add record
10 t.executeSql('INSERT INTO task (id,name) VALUES (1, "write article")');
11
12});
13
14// output all items
15db.transaction( t => {
16
17 t.executeSql(
18 "SELECT * FROM task",
19 [],
20 (t, res) => { console.log(res.rows); }
21 );
22
23});

Chrome and Safari offered varying inconsistent implementations of WebSQL, but Mozilla and Microsoft opposed it in favor of IndexedDB. The API was deprecated in 2010 and will not receive updates.

Use when:

  • you have an old web app and it’s impractical to update to another storage mechanism

Advantages:

  • familiar SQL syntax

Disadvantages:

  • limited storage
  • missing or buggy browser support with inconsistent SQL
  • old callback-based API
  • poor performance

11. AppCache

AppCache is the predecessor to the Cache API. It attempted to specify caching behavior in a plain text manifest file:

1CACHE MANIFEST
2# cache files
3index.html
4css/style.css
5js/main.js
6images/image.png
7
8# use from network when available
9NETWORK:
10network.html
11
12# fallback when not available
13FALLBACK:
14. fallback.html
15images/ images/offline.png

This looks simple when compared with Service Workers, but there were many AppCache issues and gotchas which would break your site. For example, the cache would only update when you changed the manifest and non-cached resources would refuse to load on a cached page. AppCache was a failure - avoid it!

Use when:

  • you’re a masochist with no fear of breaking your app!

Advantages:

  • conceptually easy at first

Disadvantages:

  • devilishly difficult once you progressed beyond the basics
  • removed from the Web platform.

Conclusion

Browsers have evolved over the past three decades so it’s not surprising they offer a multitude of storage APIs which essentially do the same thing. It’s not always easy to find the ‘best’ option and you may have to combine several depending on the functionality you’re implementing, e.g.

  • a cookie or two for session state
  • DOM storage for Web Component state
  • Web Storage for simple UI options (dark mode, sidebar size, etc.)
  • IndexedDB for large binary objects and data dumps
  • the Cache API for offline and quick file access
  • JavaScript variables for everything else.

More articles from OpenReplay Blog

5 Useful VueJS Tips to Improve Your Building Experience

Improve your build experience using these VueJS tips

July 13th, 2021 · 4 min read

Easy-Peasy State Management Using React Easy Peasy

Learn how easy it is to manage your app's state with this React library

July 11th, 2021 · 11 min read
© 2021 OpenReplay Blog
Link to $https://twitter.com/OpenReplayHQLink to $https://github.com/openreplay/openreplayLink to $https://www.linkedin.com/company/18257552