DEV Community

Cover image for Save Data To Offline Storage With Localforage
Vishnu Damwala
Vishnu Damwala

Posted on โ€ข Originally published at meshworld.in

1

Save Data To Offline Storage With Localforage

The localForage is an open-source JavaScript library that refines the experience of saving data to web browser databases like localStorage, IndexedFB or WebSQL API.

It supports the majority of all types of browser databases.

It is absolutely asynchronous that simply uses browser databases and improves the universal offline experience of the web app.

In this tutorial, we'll see how to save data to browsers in a much easier way with localforage setItem() method.

Localforage setItem() method

  • With localforage setItem(), developers are allowed to store multiple types of data not just strings.
  • It automatically manages and loads the best web browser driver(IndexedDB, WebSQL, and localStorage drivers).
  • It also supports to store blob and arbitrary type of data, so that we can store images, files, etc.
  • It also supports ES6 Promises.

Syntax

setItem(key, value, successCallback);

Types of supported JavaScript objects

  • Array
  • ArrayBuffer
  • Blob
  • Float32Array
  • Float64Array
  • Int8Array
  • Int16Array
  • Int32Array
  • Number
  • Object
  • Uint8Array
  • Uint8ClampedArray
  • Uint16Array
  • Uint32Array
  • String

Example

const user = {
  firstName: "Sunidhi",
  lastName: "Chauhan",
  profession: "Singer ๐ŸŽค",
};

localforage
  .setItem("userDetails", user)
  .then((value) => {
    console.log("๐Ÿ‘ฉโ€๐ŸŽค " + value.firstName + " " + value.lastName);
  })
  .catch((error) => {
    console.error(error);
  });

Output in console

๐Ÿ‘ฉโ€๐ŸŽค Sunidhi Chauhan

Alt Text

Read the complete post on our site MeshWorld โ€Š- โ€ŠSave Data To Offline Storage With Localforage

Read others post on our site MeshWorld

Hope you like this
Happy ๐Ÿ˜„ coding
With โค๏ธ from India ๐Ÿ‡ฎ๐Ÿ‡ณ

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, weโ€™ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, weโ€™ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay