Web Storage API

Deno 1.10 introduced the Web Storage API which provides an API for storing string keys and values. Persisting data works similar to a browser, and has a 10MB storage limit. The global sessionStorage object only persists data for the current execution context, while localStorage persists data from execution to execution.

In a browser, localStorage persists data uniquely per origin (effectively the protocol plus hostname plus port). As of Deno 1.16, Deno has a set of rules to determine what is a unique storage location:

This means, unlike versions prior to 1.16, localStorage is always available in the main process.

Example

The following snippet accesses the local storage bucket for the current origin and adds a data item to it using setItem().

localStorage.setItem("myDemo", "Deno App");

The syntax for reading the localStorage item is as follows:

const cat = localStorage.getItem("myDemo");

The syntax for removing the localStorage item is as follows:

localStorage.removeItem("myDemo");

The syntax for removing all the localStorage items is as follows:

localStorage.clear();