Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Saving data on the device with localStorage

After this lesson: I can keep data on the device and say when it can be lost.

55 min lesson1 commandMobile web lab: a responsive, installable web app to build in the browser

Web apps can keep data on the device

localStorage is a small store of text inside the browser. A web app can save a value under a name and read it again after the page is closed. This is how a habit list survives a reload.

It is simple and fast, but it has limits, and you need to know what can go wrong.

By the end of this lesson

Note

I can keep data on the device and say when it can be lost.

Key words

WordWhat it meansExample
localStoragea small browser store of names and text values
Keythe name a value is saved underhabits
Quotathe most data a site may store
Statewhat the app remembers right now
Serialiseturn data into text so it can be savedJSON.stringify

The cells students fill in are left blank in this preview.

localStorage saves a name and a text value

You call setItem with a key and a value. Later you call getItem with the same key to read it back. A key that was never saved gives null, which means nothing there.

Values are always text. To save a list you first turn it into text with JSON.stringify, and you turn it back with JSON.parse.

Saving and reading, line by line

Code: web
localStorage.setItem("habits", JSON.stringify(list));
const saved = localStorage.getItem("habits");
const list2 = JSON.parse(saved || "[]");

// setItem saves the list as text under the key habits.
// getItem gives that text back, or null if nothing was saved.
// "saved || []" uses an empty list when nothing is there yet.

Find where the app uses localStorage

Command
docker exec jupyter grep -o "localStorage\.[a-zA-Z]*" app/app.js

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

What the command prints

The command searches the app's script for each use of localStorage and prints only the matching part. You see one read, getItem, and one write, setItem.

The app reads its list once when it starts and writes it every time a habit changes. The lab has no browser, so you look at the stored value in your own browser's developer tools, under the storage section.

Worked example: what can go wrong

ProblemWhat happensWhat to do
Too much dataThe save is refused with an errorSave less, or catch the error
Person clears site dataEverything is goneOffer an export, or a server copy
Private windowData may vanish when it closesDo not rely on it
Two devicesEach has its own copySync through a server
Not textA list saves as a useless wordUse JSON.stringify first

The cells students fill in are left blank in this preview.

Storage has a limit and can be cleared

Each browser gives a site a limited amount of space, often a few megabytes, and the figure differs. When a save would go over the limit the browser raises an error.

The person can also clear the data, and a browser may clear it when the device is short of space. So localStorage is a convenience for the device, not a safe place for the only copy of something important.

A common mistake

Warning

Many people save a list straight into localStorage and read back the word object instead of the list. Values are always text. Use JSON.stringify to save and JSON.parse to read, and test by reloading the page.

About this lab and its source: Mobile web lab: a responsive, installable web app to build in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.