Deno 1.10 released support for WebStorage

Deno 1.10 got released and already became the talk for the Deno community because of the release of the new APIs for WebStorage, which allows you to store data server-side natively.

But first things first, run the below command to upgrade your Deno version, if you are running an older version of it.

                     

 deno upgrade --version 1.10.1 

or If you never installed Deno before, run the below commands to install that

                     

# macOS and Linux:
curl -fsSL https://deno.land/x/install/install.sh | sh

# Windows inside Powershell
iwr https://deno.land/x/install/install.ps1 -useb | iex

Just like the frontend versions of LocalStorage & SessionStorage, Deno came with a similar idea of Web Storage but on the server-side, which is very interesting to see.

Deno Web Storage APIs have two types of storage to offer

  • LocalStorage – Just like the frontend version, this will a persistent storage that will be stored on the disk, this will maintain the data across process restarts.
  • SessionStorage – They are offered as temporary storage that will be persisted across the lifespan of the process, but will erase the moment the process terminates or restarts, it can be compared with the App.Locals in ExpressJS.

LocalStorage can store up to 5 MBs, whereas SessionStorage can hold up a small amount of data for that process.

                     

// web_storage.ts
const key = Deno.args[0];

if (!key) {
  // if user passes no args, display total records 
  console.log(localStorage.length);
} else {
  const value = Deno.args[1];

  if (value) { // set value if present
     localStorage.setItem(key, value);
  } else { 
    console.log(localStorage.getItem(key));
  }
}

To execute them, we need to pass –location while running because the data will be keyed with an origin, just like the frontend version.

Run the below commands, and see Deno’s magic.

                     

$ deno run --location https://findthedifficult.com ./web_storage.ts
// 0
$ deno run --location https://findthedifficult.com ./web_storage.ts hello world
$ deno run --location https://findthedifficult.com ./web_storage.ts deno_version 1.10.1
$ deno run --location https://findthedifficult.com ./web_storage.ts deno_version
// 1.10.1
$ deno run --location https://findthedifficult.com ./web_storage.ts
// 2

And, we are done.
If you haven’t read about Deno, you can check out our recent articles here.