Home

Awesome

<p align="center"> <br> <img width="200" height="200" src="./awesome-storage-logo.png" alt="logo of awesome-web-storage repository"> <br> <br> </p>

awesome-web-storage Awesome

Everything you need to know about Client-side Storage.

Table of Contents

Introduction

What is Web Storage

Web storage, sometimes known as DOM storage (Document Object Model storage), provides web application software methods and protocols used for storing data in a web browser.

Web storage is being standardized by the World Wide Web Consortium (W3C). It was originally part of the HTML5 specification, but is now in a separate specification.

Modern websites and web applications these days are becoming smarter than before and to achieve such smartness, the data storage system plays a very crucial role. Be it storing visitors' data & settings or any kind of information, web-storage is a breakthrough in the history of web applications. It offers a very basic need of storing persistent data in the browser itself which has literally spurred up its application from game-changing novelty to must-have features.

Usually, this kind of data doesn't need a well-organized backend database. The purpose of the data varies as per the website/webapp requirements. Different pages of the same website need to share data among themselves to behave alike and perfectly. This sharing of data among windows/tabs is very common these days and most of the sites or web apps use it for various jobs like User Authentication, Personalization (showing different views to a new visitor and on subsequent visits), Tracking user behavior, etc.

Before HTML5, storing application level data was possible only using cookies. Cookies can store up to 4KB of data, including the bytes consumed by the cookie name. This domain-specific data is sent along with every browser request. With the advent of HTML5, it's much easier now to store structured data on the client-side securely and faster than ever. HTML5 introduced the concept of Web Storage. Web storage can be viewed simplistically as an improvement on cookies, providing much greater storage capacity. HTML5 introduces two such web storage mechanisms: LocalStorage and SessionStorage. There are so many limitations in using cookies which can now be overcome using web storage.

There's no term as "Perfectly Secured Persistent Storage" available in the browsers as any type of storage system( persistent or non-persistent) can be manually tweaked by the end-user(assuming she knows a bit of geeky stuff :P).

Web Storage concepts and usage

The two mechanisms within Web Storage are as follows:

  1. sessionStorage maintains a separate storage area for each given origin that's available for the duration of the page session (as long as the browser is open, including page reloads and restores).
  2. localStorage does the same thing, but persists even when the browser is closed and reopened.

Web Storage interfaces

Storage

Allows you to set, retrieve and remove data for a specific domain and storage type (session or local.)

Window

The Web Storage API extends the Window object with two new properties — Window.sessionStorage and Window.localStorage — which provide access to the current domain's session and local Storage objects respectively, and a Window.onstorage event handler that fires when a storage area changes (e.g. a new item is stored.)

StorageEvent

The storage event is fired on a document's Window object when a storage area changes.

Browser Support

<img src="media/edge.png" alt="IE / Edge" width="16px" height="16px" /></br> IE / Edge<img src="media/firefox.png" alt="Firefox" width="16px" height="16px" /></br> Firefox<img src="media/chrome.png" alt="Chrome" width="16px" height="16px" /></br> Chrome<img src="media/safari.png" alt="Safari" width="16px" height="16px" /></br> Safari<img src="media/opera.png" alt="Opera" width="16px" height="16px" /></br> Opera
IE8+, Edge12+3.5+4+4+11.5+

Different Storage APIs

Following are various storage techniques which HTML5 storage provides. Each technique has its own pros and cons.

Cookies

An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to the user's web browser. The browser may store it and send it back with the next request to the same server. Typically, it's used to tell if two requests came from the same browser — keeping a user logged-in, for example. It remembers stateful information for the stateless HTTP protocol.

Pros:
Cons:
API
  1. Read

    Reading all cookies accessible from the domain

    var allCookies = document.cookie;
    
  2. Write

    Writing a new cookie on a domain

    document.cookie = newCookie;
    

    As mentioned in the mdn docs, any of the following cookie attribute values can optionally follow the key-value pair, specifying the cookie to set/update, and preceded by a semi-colon separator:

    • ;path=path - (e.g., '/', '/mydir') If not specified, defaults to the current path of the current document location.

    • ;domain=domain - (e.g., 'example.com' or 'subdomain.example.com').

    • ;max-age=max-age-in-seconds - (e.g., 60*60*24*365 or 31536000 for a year)

    • ;expires=date-in-GMTString-format - If not specified it will expire at the end of a session.

    • ;secure - Cookie to only be transmitted over secure protocol as https. Before Chrome 52, this flag could appear with cookies from http domains.

Resources - more on cookies

localStorage

The read-only localStorage property allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions. localStorage is similar to sessionStorage, except that while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the page session ends — that is, when the page is closed.

Pros:
Cons:
API
  1. setItem

    setItemDescription
    methodlocalStorage.setItem(key, value)
    paramskey(String/Number),<br>value(Number/String),<br>though it accepts other types, the value is always stored as a string, so, stringifying data before storing is preferred and the best way to avoid errors while reading
    descriptionadds the key to the storage with the value, or update the key's value if it already exists.
    examplelocalStorage.setItem('test', document.getElementById('js-btn').value);
  2. getItem

    getItemDescription
    methodlocalStorage.getItem(key)
    paramskey(String/Number)
    descriptionreturns the value of the key passed.
    examplelocalStorage.getItem('test')
  3. removeItem

    removeItemDescription
    methodlocalStorage.removeItem(key)
    paramskey(String/Number)
    descriptionremoves the key from the storage.
    examplelocalStorage.removeItem('test')
  4. clear

    clearDescription
    methodlocalStorage.clear()
    paramsNone
    descriptionempties all the keys out of the storage.
    examplelocalStorage.clear()
  5. key

    keyDescription
    methodlocalStorage.key(n)
    paramsn(a Number)
    descriptionreturns the name of the nth key in the storage.
    examplelocalStorage.key(0)
  6. length

    lengthDescription
    propertylocalStorage.length
    descriptionreturns the length of all the keys
    examplelocalStorage.length
Resources - more on localStorage

sessionStorage

The sessionStorage property allows you to access a session Storage object for the current origin. sessionStorage is similar to Window.localStorage, the only difference is while data stored in localStorage has no expiration set, data stored in sessionStorage gets cleared when the page session ends. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated, which differs from how session cookies work.

Pros:
Cons:
API
  1. setItem

    setItemDescription
    methodsessionStorage.setItem(key, value)
    paramskey(String/Number),<br>value(Number/String),<br>though it accepts other types, the value is always stored as a string, so, stringifying data before storing is preferred and the best way to avoid errors while reading
    descriptionadds the key to the storage with the value, or update the key's value if it already exists.
    examplesessionStorage.setItem('test', document.getElementById('js-btn').value);
  2. getItem

    getItemDescription
    methodsessionStorage.getItem(key)
    paramskey(String/Number)
    descriptionreturns the value of the key passed.
    examplesessionStorage.getItem('test')
  3. removeItem

    removeItemDescription
    methodsessionStorage.removeItem(key)
    paramskey(String/Number)
    descriptionremoves the key from the storage.
    examplesessionStorage.removeItem('test')
  4. clear

    clearDescription
    methodsessionStorage.clear()
    paramsNone
    descriptionempties all the keys out of the storage.
    examplesessionStorage.clear()
  5. key

    keyDescription
    methodsessionStorage.key(n)
    paramsn(a Number)
    descriptionreturns the name of the nth key in the storage.
    examplesessionStorage.key(0)
  6. length

    lengthDescription
    propertysessionStorage.length
    descriptionreturns the length of all the keys
    examplesessionStorage.length
Resources - more on sessionStorage
<hr>

Comparison Table

Web StoragecookieslocalStoragesessionStorage
Size limitMax 4kb (~2K chars)Max 5mb (~2M chars)Max 5mb (~2M chars)
Data StorageFileSytemFileSytemFileSytem
PayloadIn every HTTP reqNothingNothing
APIFairSimpleSimple
PersistentYesYesNo
Data FormatStringStringString
Same-originYesYesYes
Cross-originNoNoNo
Browser SipportAllIE8+, Edge12+, Firefox3.5+, Safari4+, Opera11.5+IE8+, Edge12+, Firefox3.5+, Safari4+, Opera11.5+
Size limits infolimitlimitlimit

Worth mentioning API for tackling cross-origin restriction

postMessage

One very interesting API, PostMessage is not a web-storage technique but it's the most efficient and reliable way of communicating between cross-origin browser windows/tabs. Along with web-storage APIs, it overcomes the web-storage restrictions of cross-origin.

Pros:
Cons:
API:
otherWindow.postMessage(message, targetOrigin, [transfer]);

otherWindow - A reference to another window; such a reference may be obtained, for example, using the contentWindow property of an iframe element, the object returned by window.open, or by named or numeric index on Window.frames, if you're trying to start the communication from iframe to parent window then parent is also a valid reference

message - Data to be sent to the other window. The data is serialized using the structured clone algorithm. This means you can pass a broad variety of data objects safely to the destination window without having to serialize them yourself. [1]

targetOrigin - Specifies what the origin of otherWindow must be for the event to be dispatched, either as the literal string "*" (indicating no preference) or as a URI. If at the time the event is scheduled to be dispatched the scheme, hostname, or port of otherWindow's document does not match that provided in targetOrigin, the event will not be dispatched; only if all three match will the event be dispatched. This mechanism provides control over where messages are sent; for example, if postMessage() was used to transmit a password, it would be absolutely critical that this argument be a URI whose origin is the same as the intended receiver of the message containing the password, to prevent interception of the password by a malicious third party. Always provide a specific targetOrigin, not *, if you know where the other window's document should be located. Failing to provide a specific target discloses the data you send to any interested malicious site.

transfer(Optional) - Is a sequence of Transferable objects that are transferred with the message. The ownership of these objects is given to the destination side and they are no longer usable on the sending side.

Security concerns

If you do not expect to receive messages from other sites, do not add any event listeners for message events. This is a completely foolproof way to avoid security problems.

If you do expect to receive messages from other sites, always verify the sender's identity using the origin and possibly source properties. Any window (including, for example, http://evil.example.com) can send a message to any other window, and you have no guarantees that an unknown sender will not send malicious messages. Having verified identity, however, you still should always verify the syntax of the received message. Otherwise, a security hole in the site you trusted to send only trusted messages could then open a cross-site scripting hole in your site.

Always specify an exact target origin, not *, when you use postMessage to send data to other windows. A malicious site can change the location of the window without your knowledge, and therefore it can intercept the data sent using postMessage.

Resources - more on postMessage:

FAQs

  1. How to store data that works Cross-Origin too?

    A combination of postMessage and localStorage / sessionStorage

    Using postMessage to communicate between multiple tabs and at the same time using localStorage/sessionStorage in all the newly opened tabs/windows to persist data being passed. Data will be persisted as long as the tabs/windows remain open in case of sessionStorage and in the case of localStorage unless the data is deleted by the system or manually flushing it using dev tools. So, even if the opener tab/window gets closed, the opened tabs/windows will have the entire data even after getting refreshed.

    • across-tabs - Easy communication between cross-origin browser tabs

Contributing Guidelines

Please ensure your pull request adheres to the following guidelines:

LICENSE

CC0

To the extent possible under law, Varun Malhotra has waived all copyright and related or neighboring rights to this work.