T3 Browser DevTools · Lesson 5
The Application panel and storage
What the browser remembers for a site: localStorage and sessionStorage, cookies and their flags, IndexedDB and Cache storage, clearing a site's data, and the bugs that come from stored data, with what must never be stored.
- open the Application panel, and name what each store is for
- use localStorage and sessionStorage, and choose between them
- read a cookie's fields, and explain HttpOnly, Secure and SameSite
- say when IndexedDB or Cache storage is the right place
- find bugs that come from stored data, and keep secrets out of the wrong store
The back cover
Some bugs live not in the code but in the browser's memory of a site: an old value, a forgotten cookie, a store that outlived the code that wrote it. The page is the same for everyone, yet each browser remembers something different.
These pages open the Application panel and its five drawers. They show localStorage and sessionStorage and why both hold only text, and cookies with the flags that guard them. They show the two large stores, IndexedDB and Cache storage, and how to clear a site's data. They end with the bugs that stored data causes, and what must never be stored.
Before you start
The story: The panel that remembered
The lending library's reading-room panel has begun greeting every reader as Mrs Bose, and listing her books. Its frame is sound, its diary is quiet, and all its parcels arrive. Corin reloads it twice, and Mrs Bose comes back each time.
Ilse sends him to the Row with a note that says only, "Show me the clue." Ottilie Sen reads it, and hands you a fifth lens, shaped like a small brass key.
"A panel has a memory," she says. "It keeps things in drawers, inside the glass itself, where no reader can see them. Your Mrs Bose is still in there."
You close a shop's page, come back next week, and your cart is still there. You stay signed in for a month. Your choice of dark mode survives a reload. A web page is thrown away each time it closes, so something else must be remembering. The browser keeps small stores for each site, and this panel opens them. It builds on these ideas; each has a tiny example.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| An address has a scheme, a host and a port. | https://study.rohannetworks.com | Those three together are the origin, and each origin has its own stores. |
| A request carries headers. | cookie: city=Pune | A cookie travels in a header. |
| A response can set things. | set-cookie: sid=abc | A server stores a cookie this way. |
| Text and numbers differ. | "3" + 1 is "31". | Storage holds only text. |
| JSON turns data into text. | {"item": "Rice"} | It is how an object is stored. |
| A page runs scripts. | Any script on a page runs with the page's powers. | So any script can read what the page stores. |
| The cache keeps copies of files. | A 304 means use your copy. | The stores here are for your own data. |
| The Console can ask questions. | localStorage.getItem("cart") | You can read and change the stores from it. |
By the end of this lesson you will be able to:
- open the Application panel, and name what each store is for;
- use localStorage and sessionStorage, and choose between them;
- read a cookie's fields, and explain HttpOnly, Secure and SameSite;
- say when IndexedDB or the Cache storage is the right place, and clear a site's data;
- find the bugs that come from stored data, and keep private things out of the wrong store.
How to follow this lesson
A block's page runs in a sandboxed frame, which may not use storage. So the blocks here have no Run button. The kit served each from a real small server, visited it as a browser would, and listed underneath what the panel would show. To try the panel yourself, open it on this page: the site keeps your own progress in these stores. Look as much as you like, but do not clear them, or you may lose progress that has not reached your account yet.
In one line
The browser keeps small stores for each site, so that a page can remember; you need the idea of an origin, a header and JSON.
What the browser keeps for a site
At a glance
A bank gives each customer a locker that no other customer can open. A browser gives each site a set of lockers on your device. The Application panel lets you open them, and read, change or delete what is inside.
There are five drawers. The localStorage drawer stays until deleted, sessionStorage goes with its tab, cookies travel with every request, IndexedDB holds whole objects, and Cache storage holds files. One memory aid keeps them apart: local stays, session goes with the tab, cookies travel, IndexedDB holds a lot, and Cache holds files.
Take a guess: Which of the five drawers is sent to the server with every request?
A bank gives each customer a locker. The customer keeps what they like in it, and no other customer can open it. A browser gives each site a set of lockers on your device. The Application panel lets you open them.
A DevTools panel that shows what a site has stored in the browser, and lets you read, change and delete it. Find it in the row of panels, under the » button if it is hidden, or through the Command Menu by typing Application.
The scheme, host and port of an address together: https://study.rohannetworks.com is one origin. Each origin has its own stores, and a page from one origin cannot read another's.
The five drawers
| Store | Holds | Size | Lasts | Sent to the server? |
|---|---|---|---|---|
| localStorage | pairs of text | about 52 lakh characters | until deleted | no |
| sessionStorage | pairs of text | the same | until the tab closes | no |
| cookies | small pairs of text, with rules | about 4000 characters each | until they expire | yes, with every request |
| IndexedDB | a real database of objects | large | until deleted | no |
| Cache storage | whole files, with their headers | large | until deleted | no |
The sizes for localStorage and cookies were measured in Chrome 155. The fourth column is the question to ask first: how long should this be remembered?
A memory aid
Local stays. Session goes with the tab. Cookies travel. IndexedDB holds a lot. Cache holds files.
Finding them
In the panel's left side, under Storage, each store has a line. Open one, and choose this site's origin.
| In the panel | You will see |
|---|---|
| Local storage | a table of keys and values for each origin |
| Session storage | the same, for this tab |
| Cookies | a table with a column for each field |
| IndexedDB | databases, then their object stores, then records |
| Cache storage | named caches, then the files in each |
| Storage, at the top | how much the site uses, and a Clear site data button |
Look at this site
This site keeps your own work in these stores, so you can see them in use. Open the Application panel here, then Local storage, then this site. If you have solved a game item in this path, there is a key such as tech:games:devtools/05, holding what you solved. Under IndexedDB is the database that holds your progress until it reaches your account.
When you are stuck: the Application panel
You cannot find the panel: click the » button at the end of the panels, or use the Command Menu.
A store is empty: choose the origin under it, as each site's data is listed separately.
A value you just changed in the page still shows the old one: click the refresh button above the table.
Your turn
Which store is sent to the server with every request?Tap to reveal
- They travel.
Cookies.
Which store empties when the tab is closed?Tap to reveal
- It goes with the tab.
sessionStorage.
Can a page from one origin read another origin's localStorage?Tap to reveal
- Each customer has a locker.
No.
Did you know?
One locker per origin
Each set of lockers belongs to an origin, not to a whole site. Two addresses share an origin only if the protocol, the port and the host are all the same. The localStorage of a page belongs to the document's origin, so the http and https versions of a site, or two of its subdomains, see separate stores. The HTML standard calls origins the fundamental currency of the web's security model.
Why it fits here: If a stored value seems to vanish, check the address bar: another protocol or subdomain opens another set of lockers.
Sources: MDN: Same-origin policy; MDN: Window.localStorage; HTML Standard: Origins
A sixth drawer, closed
There was once another drawer: Web SQL, a database in the browser that used SQL. Work on its standard stopped in 2010 at an impasse, because every interested implementor had used the same backend, SQLite, and a standard needs several independent ones. Firefox never implemented it. Chrome took it away in steps, and from Chromium 119 it is no longer available anywhere.
Why it fits here: It is why the panel's database drawer is IndexedDB: the W3C points those who need a web database there instead.
Sources: W3C: Web SQL Database; Chrome for Developers: Deprecating and removing Web SQL
In one line
The Application panel opens the stores the browser keeps for each origin: localStorage stays, sessionStorage goes with the tab, cookies travel with every request, IndexedDB holds objects, and Cache storage holds files.
Two drawers of text: localStorage and sessionStorage
At a glance
A shopkeeper's notebook stays on the counter for years, while the slate by the till is wiped at closing time. In this picture, localStorage is the notebook and sessionStorage is the slate. Both are used in exactly the same way.
Data in localStorage lasts until it is deleted, and every tab of the site shares it. Data in sessionStorage lasts as long as its tab. Both hold only text, so objects go in through JSON.stringify and come out through JSON.parse. The panel lets you read, edit and delete every pair.
Take a guess: If you store the number 5 in localStorage, what type comes back out?
A shopkeeper's notebook stays on the counter for years. A slate by the till is wiped at closing time. localStorage is the notebook, and sessionStorage is the slate. Both are used in exactly the same way.
| To do this | Write |
|---|---|
| store a value | localStorage.setItem("city", "Pune") |
| read it | localStorage.getItem("city") |
| remove it | localStorage.removeItem("city") |
| remove everything | localStorage.clear() |
| read a key that is not there | it gives null |
How long each one lasts
This page reads both stores, then writes to both. The kit visited it, reloaded it, then opened it in a new tab.
On a real site, visited three times
<!DOCTYPE html>
<script>
console.log("local", localStorage.getItem("a"), "session", sessionStorage.getItem("b"));
localStorage.setItem("a", "1");
sessionStorage.setItem("b", "2");
</script>
The Console shows
First visit
Info local null session null
Second visit
Info local 1 session 2
In a new tab
Info local 1 session null
Read the three lines. On the first visit, both are empty. After a reload, both are there. In a new tab, the local value is there and the session value is not: each tab has its own session store, and it goes when the tab closes.
Everything is text
These stores hold only text. A number goes in and text comes out, and an object goes in and comes out as something useless.
On a real site
<!DOCTYPE html>
<script>
localStorage.setItem("qty", 3);
const qty = localStorage.getItem("qty");
console.log(typeof qty, qty + 1);
localStorage.setItem("cart", { item: "Rice" });
console.log(localStorage.getItem("cart"));
</script>
The Application panel shows
Local storage: qty = 3
Local storage: cart = [object Object]
The Console shows
Info string 31
Info [object Object]
The 3 came back as the text "3", so adding 1 joined them: 31. The object came back as the words [object Object], and the data is gone for good. The panel shows those very words in the Value column, which is how you find this bug.
The cure is JSON. JSON.stringify turns data into text before storing it, and JSON.parse turns it back after reading.
On a real site
<!DOCTYPE html>
<script>
const cart = [{ item: "Rice", qty: 2 }];
localStorage.setItem("cart", JSON.stringify(cart));
const back = JSON.parse(localStorage.getItem("cart"));
console.log(back[0].item, back[0].qty + 1);
</script>
The Application panel shows
Local storage: cart = [{"item":"Rice","qty":2}]
The Console shows
Info Rice 3
You can try the two halves here, with no store at all. This block runs on the page: change the cart and run it again.
const cart = [{ item: "Rice", qty: 2 }];
const text = JSON.stringify(cart);
console.log(text);
const back = JSON.parse(text);
console.log(back[0].item, back[0].qty + 1);
[{"item":"Rice","qty":2}]
Rice 3
A memory aid
Stringify going in, parse coming out.
Working in the panel
| To do this | Do this |
|---|---|
| see a value | click its row: the full value appears below, opened out if it is JSON |
| change a key or value | double-click it, type, and press Enter |
| add a pair | double-click the empty row at the bottom |
| delete a pair | select it, and press Delete |
| find a key | type in the Filter box |
Changing a value here is a quick way to test a page. What does the cart do with 99 items? What happens if the theme is a word the code has never seen? Edit it, reload, and find out. Unlike most things in DevTools, this change stays: it is in the store, not in the page's copy.
Worked example 1
A shop keeps the cart in sessionStorage. A customer opens a product in a new tab, adds it, and the cart in the first tab does not show it. Why, and what should the shop use?
What we have: a cart that differs between two tabs of one site
What we need: the reason, and the right store
The plan: each tab has its own sessionStorage; a cart should be shared by all tabs and survive closing
- Open the panel in each tab: each shows a different Session storage.
- sessionStorage belongs to one tab, so that is the design, not a fault.
- A cart should last and be shared, so it belongs in localStorage.
With localStorage, in two tabs
<!DOCTYPE html>
<script>
const count = Number(localStorage.getItem("items")) + 1;
localStorage.setItem("items", count);
console.log("items", count);
</script>
The Console shows
First visit
Info items 1
In a new tab
Info items 2
sessionStorage is per tab. Use localStorage, which all tabs of the site share.
Check: the new tab read 1 and made it 2: the count was shared.
Worked example 2
A site saves a reader's settings, and on the next visit they are all lost. The panel's Value column reads [object Object]. Fix the saving and the loading.
What we have: a stored value that reads [object Object]
What we need: settings that survive
The plan: storage holds only text, so stringify before setItem, and parse after getItem
- The value in the panel shows what was stored: the object was turned into the words [object Object].
- Wrap it:
localStorage.setItem("settings", JSON.stringify(settings)). - Unwrap it when reading, and allow for a first visit, when it is null.
On a real site, visited twice
<!DOCTYPE html>
<script>
const saved = localStorage.getItem("settings");
const settings = saved ? JSON.parse(saved) : { theme: "light" };
console.log("theme", settings.theme);
settings.theme = "dark";
localStorage.setItem("settings", JSON.stringify(settings));
</script>
The Application panel shows
First visit
Local storage: settings = {"theme":"dark"}
Second visit
Local storage: settings = {"theme":"dark"}
The Console shows
First visit
Info theme light
Second visit
Info theme dark
JSON.stringify going in, JSON.parse coming out, with a default for the first visit.
Check: the second visit read dark, the value the first visit stored.
When you are stuck: local and session storage
A number behaves oddly after reading: it came back as text; use Number().
The panel shows [object Object]: the object was stored directly; use JSON.stringify.
JSON.parse fails with an Unexpected token: the stored text is not JSON; read it in the panel.
Your turn
You store the number 5 in localStorage. What does getItem give?Tap to reveal
- Everything is text.
The text "5".
Does sessionStorage survive a reload?Tap to reveal
- It goes with the tab.
Yes; it goes when the tab closes.
What does getItem give for a key that is not there?Tap to reveal
- Not undefined.
null.
Did you know?
Five megabytes each
These two drawers are small. Browsers can store up to 5 MiB of localStorage and 5 MiB of sessionStorage for each origin. When that limit is reached, the browser throws a QuotaExceededError, which the page's code should handle. A list of short notes fits with room to spare; a single large photograph may not.
Why it fits here: It is one more reason this topic keeps only small text here, and sends large data to IndexedDB.
Sources: MDN: Storage quotas and eviction criteria; HTML Standard: Web storage
Tabs that talk
One tab can learn of another tab's change through storage. When a document changes a storage area, the browser fires a storage event in every other document that shares that area, but not in the window that made the change. The event carries the key, the old value, the new value and the address of the page. So two open tabs of one shop can keep a cart in step.
Why it fits here: If a value changes in one tab while another is open, this event is how the second tab can find out.
Sources: MDN: Window storage event; HTML Standard: Web storage
In one line
localStorage lasts until deleted, and all tabs share it; sessionStorage lasts as long as its tab. Both hold only text, so stringify going in and parse coming out, and the panel lets you read and edit every pair.
The drawer that travels: cookies
At a glance
A cloakroom attendant gives you a numbered token, and each time you come back to the counter, you show it. The attendant does not remember your face; the token does the remembering. A cookie is that token: the server gives it to the browser, and the browser shows it again with every request to that site.
The panel lists each cookie with its name, value, domain, path, expiry and flags. Three flags guard a sign-in cookie. HttpOnly hides it from scripts, Secure keeps it to HTTPS, and SameSite holds it back from other sites.
Take a guess: Which cookie flag keeps a cookie out of reach of JavaScript?
A cloakroom attendant gives you a numbered token, and each time you come back to the counter, you show it. The attendant does not remember your face; the token does the remembering. A cookie is that token. The server gives it to the browser, and the browser shows it again with every request to that site.
A small named value kept by the browser for a site, and sent to the server automatically with every request to it. Its most common job is to say who you are, so that you stay signed in.
Set by a script, sent by the browser
A script can set a cookie by writing to document.cookie. Notice what happens on the next visit.
On a real site, visited twice
<!DOCTYPE html>
<script>
console.log("cookies now:", document.cookie);
document.cookie = "city=Pune";
document.cookie = "lang=hi; max-age=3600";
</script>
The Application panel shows
First visit
Cookie: city=Pune session
Cookie: lang=hi
Second visit
Cookie: city=Pune session
Cookie: lang=hi
The server received, for /: city=Pune; lang=hi
The Console shows
First visit
Info cookies now:
Second visit
Info cookies now: city=Pune; lang=hi
That odd line is how cookies are written: assigning to document.cookie adds one cookie, and does not replace the others. On the second visit, the server received both, though no script sent them. That is the difference from every other store: a cookie travels by itself. So keep them few and small, because every request carries them, images included.
city has no end date, so it is a session cookie, and the panel's Expires column reads Session. lang has a max-age in seconds, so it outlives the browser's closing.
The panel's columns
| Column | Means |
|---|---|
| Name, Value | the cookie itself |
| Domain, Path | which addresses it is sent to |
| Expires / Max-Age | when it ends; Session means when the browser closes |
| Size | its size in bytes |
| HttpOnly | scripts cannot read it |
| Secure | sent only over HTTPS |
| SameSite | whether it is sent when another site starts the request |
| Partition Key, Priority | details for advanced uses |
HttpOnly: a cookie scripts cannot touch
A sign-in cookie is the key to an account. If an attacker slips a script into a page, it runs with the page's powers, and could read anything a script can read. So a server marks such cookies HttpOnly. The browser still sends the cookie, and no script can see it.
On a real site whose server sets two cookies
<!DOCTYPE html>
<script>
console.log("a script sees:", document.cookie);
</script>
The Application panel shows
First visit
Cookie: sid=abc123 HttpOnly session
Cookie: theme=dark session SameSite=Strict
document.cookie reads: theme=dark
Second visit
Cookie: sid=abc123 HttpOnly session
Cookie: theme=dark session SameSite=Strict
document.cookie reads: theme=dark
The server received, for /: sid=abc123; theme=dark
The Console shows
First visit
Info a script sees: theme=dark
Second visit
Info a script sees: theme=dark
The server set two cookies, and the panel lists both. A script sees only one. On the second visit, the server received both. That is exactly the arrangement you want: the browser carries the key, and no script can steal it.
Secure and SameSite
| Flag | Means | Why |
|---|---|---|
Secure | sent only over HTTPS | so that nobody on the same Wi-Fi can read it on the way |
SameSite=Strict | sent only when the request starts on this site | the strongest protection, but a link from elsewhere arrives signed out |
SameSite=Lax | also sent when following a link to this site | the usual choice, and what Chrome assumes if none is set |
SameSite=None | sent everywhere; must also be Secure | for services embedded in other sites |
SameSite exists to stop a trick called cross-site request forgery. A bad site makes your browser send a request to your bank; without SameSite, your cookie goes along with it, and the bank thinks that you asked. A later step on sign-in and security goes deeper.
A memory aid
HttpOnly: no scripts. Secure: no plain HTTP. SameSite: not from other sites. A sign-in cookie should have all three.
Reading cookies in the Network panel
Open a request in the Network panel, and its Cookies tab shows which cookies went out and came back. If you are unexpectedly signed out, look there: is the cookie being sent at all? A cookie that was refused is marked, with the reason beside it.
Worked example 3
A site keeps signing you out. The server says that it sets a cookie called sid. Find out whether the browser kept it and whether it sends it.
What we have: a sign-in that does not stick, and a cookie's name
What we need: whether the cookie is stored, and whether it travels
The plan: the Application panel shows what is stored, and the Network panel shows what is sent
- Open Application, then Cookies, then the site. Is sid there? If not, read the sign-in request's Cookies tab: a refused cookie says why.
- If it is there, read its Expires column. Session means that it goes when the browser closes.
- Open Network, reload, and open the page's request. In its Cookies tab, see whether sid went out. A wrong Domain or Path keeps it at home.
A server sets sid, for one path only
<!DOCTYPE html>
<script>
console.log("a script sees:", document.cookie);
</script>
The Application panel shows
First visit
Cookie: sid=abc123 HttpOnly session
Second visit
Cookie: sid=abc123 HttpOnly session
The Console shows
First visit
Info a script sees:
Second visit
Info a script sees:
Check where it is stored, then where it goes. Here the cookie exists, and its Path keeps it from being sent to this page.
Check: the browser listed the cookie, and the server heard no cookie for that address.
When you are stuck: cookies
A cookie is missing from document.cookie: is it HttpOnly? Look in the panel, which lists every cookie.
A cookie is stored and not sent: read its Domain, Path, Secure and SameSite columns.
A cookie vanishes when the browser closes: Expires reads Session; set max-age.
Your turn
Who sends a cookie to the server?Tap to reveal
- No script is needed.
The browser, by itself, with every request.
Why can a script not read an HttpOnly cookie?Tap to reveal
- What is it protecting?
To keep a sign-in key from any script that gets into the page.
What does Session mean in the Expires column?Tap to reveal
- When does it end?
It ends when the browser closes.
Did you know?
Four hundred days at most
A cookie cannot ask to live for ever. As of Chrome 104, in August 2022, cookies can no longer set an expiration date more than 400 days in the future. A cookie that asks for longer is not rejected; its date is set to 400 days instead. The revision of the cookie standard recommends the same limit, 400 days, which is about thirteen months.
Why it fits here: It explains a date in this topic's cookie columns: an Expires far in the future is cut back to 400 days.
Sources: Chrome for Developers: Cookie Expires and Max-Age attributes now have upper limit; IETF: Cookies: HTTP State Management Mechanism (rfc6265bis)
Floors, not ceilings
The cookie standard, RFC 6265, sets floors, not ceilings. A browser should allow at least 4096 bytes per cookie, counting its name, value and attributes, at least 50 cookies per domain and at least 3000 cookies in all. Servers, for their part, should use as few and as small cookies as possible, to avoid reaching those limits. Every cookie also rides along with every request to its site, so small ones keep each request light.
Why it fits here: This topic's cookies hold a short key, not data: the floors are small, and the browser sends each cookie every time.
In one line
A cookie is a small value the browser sends with every request by itself. HttpOnly hides it from scripts, Secure keeps it to HTTPS, and SameSite holds it back from other sites; a sign-in cookie should have all three.
The two large drawers, and the Clear button
At a glance
Text pairs suit small things. A notes app with a thousand notes, or a page that must work offline, needs more, and two larger stores exist. You will mostly read them here, because libraries usually do the writing.
IndexedDB is a real database built into the browser, which keeps whole objects in named stores and finds them by key. Cache storage holds whole files, and a service worker can answer requests from it when the network is gone. The Clear site data button empties every store of a site at once.
Take a guess: Which store can keep whole objects without turning them into text first?
Text pairs suit small things. A notes app with a thousand notes, or a page that must work offline, needs more. Two larger stores exist, and you will mostly read them here, as libraries usually do the writing.
IndexedDB: a real database in the browser
A database built into the browser. It stores whole objects in named object stores, finds them by key, and can hold far more than localStorage. Numbers stay numbers, and dates stay dates.
On a real site
<!DOCTYPE html>
<script>
const open = indexedDB.open("shop", 1);
open.onupgradeneeded = () => open.result.createObjectStore("orders", { keyPath: "id" });
open.onsuccess = () => {
const write = open.result.transaction("orders", "readwrite");
write.objectStore("orders").put({ id: 1, item: "Rice", qty: 2 });
write.oncomplete = () => {
const read = open.result.transaction("orders").objectStore("orders").get(1);
read.onsuccess = () => console.log(read.result.item, read.result.qty + 1);
};
};
</script>
The Console shows
Info Rice 3
The code is long, and most people use a small library for it. What matters here: the quantity came back a number, so 2 + 1 is 3, not 21. In the panel, open IndexedDB, then the database, then the store, and each record is listed with its key. The panel does not refresh by itself, so use the refresh button, and you can delete records there.
This site keeps your progress in IndexedDB, so that it works offline and survives until it can reach your account.
Cache storage and service workers
A store for whole files, kept by your own code. It is separate from the browser's ordinary cache of the Network lesson: that one the browser manages, and this one your scripts manage.
On a real site
<!DOCTYPE html>
<script>
caches.open("v1")
.then((cache) => cache.add("style.css").then(() => cache.keys()))
.then((keys) => console.log(keys.length, new URL(keys[0].url).pathname));
</script>
The Console shows
Info 1 /style.css
A service worker is a script that sits between a page and the network, and can answer requests from Cache storage when there is no connection. That is how a site works offline, as this one does. Two lines in the panel go with it: Service workers shows the one running, with a way to update it, and Manifest shows the file that lets a site be installed like an app.
| Store | Choose it for |
|---|---|
| a cookie | something the server must see on every request, such as a sign-in |
| localStorage | small settings, such as a theme |
| sessionStorage | something for this tab only, such as a half-filled form |
| IndexedDB | lots of structured data, such as notes, or progress kept offline |
| Cache storage | files needed offline |
Clear site data
At the top of the panel's list is Storage. It shows how much the site uses, and it has a Clear site data button, which empties all of the stores for this site at once. It is the quickest way to become a new visitor. On a site you are building, use it freely. On a site where you have an account, it signs you out, and it deletes anything that has not reached the server.
Clearing and being surprised
A site you use may lose your unsaved work when you clear it. This site writes your progress to your account about every 20 seconds while you are signed in and online. Offline, or signed out, it waits in these stores, and the button would delete it.
An Incognito window is another way to test as a new visitor: its stores start empty and vanish when it closes, and it leaves your real data alone.
Worked example 4
A tiffin service's site must remember four things: who is signed in, the customer's language, a half-typed delivery address, and 2000 past orders, to show offline. Choose a store for each, and give the reason.
What we have: four things, each with its own life and size
What we need: the right store for each
The plan: ask of each: who must see it, how long should it last, and how large is it?
- Who is signed in: the server must see it on every request, and no script should read it, so an HttpOnly, Secure cookie.
- The language: small, kept for ever, and needed by the page, so localStorage.
- The half-typed address: for this tab, gone when it closes, so sessionStorage. Another tab should not fill in the same form.
- 2000 past orders: large, structured, and needed offline, so IndexedDB. A cookie would be far too small, and every request would have to carry it.
A cookie, localStorage, sessionStorage and IndexedDB. Who must see it, how long, and how large decide each one.
Check: each answer matches a row of the table above: who sees it, how long it lasts, and how much it holds.
When you are stuck: the large stores
A new record does not show in IndexedDB: click the refresh button; the panel does not update by itself.
A site still shows old files after you changed them: a service worker may be answering from its cache; open Service workers, and tick Update on reload.
You need a clean start: Clear site data, or an Incognito window.
Your turn
Which store keeps a number as a number?Tap to reveal
- A real database.
IndexedDB.
What is the difference between the browser's cache and Cache storage?Tap to reveal
- Who manages each?
The browser manages its cache; your code manages Cache storage.
What is the quickest way to become a new visitor on a site you are building?Tap to reveal
- A button at the top of the panel.
Clear site data.
Did you know?
Room measured in gigabytes
The large drawers are large indeed. In Chrome, the browser may use up to 80% of total disk space, and one origin up to 60%. MDN gives an example: on a device with a 1 TiB drive, one origin may use up to 600 GiB. Compare that with the 5 MiB of localStorage, and the reason for IndexedDB is plain.
Why it fits here: It shows why this topic sends big data to IndexedDB: its room is measured in gigabytes, not megabytes.
Sources: web.dev: Storage for the web; MDN: Storage quotas and eviction criteria
A header that empties the drawers
The Clear site data button has a twin on the server's side. A site can send the Clear-Site-Data header to instruct the browser to clear the data it stores for that site, such as "cache", "cookies" and "storage". The standard's first example is signing out: the site wants the locally stored data removed as the user leaves.
Why it fits here: A sign-out that only forgets the cookie may leave other drawers full; this header empties them all at once.
Source: W3C: Clear Site Data
In one line
IndexedDB is a real database that keeps objects as they are, Cache storage holds files for offline use through a service worker, and Clear site data empties every store for a site at once.
Too much memory, or too little
At a glance
A bug from stored data has a strange look: it happens to some people and not to others, or to you and not on a new computer. The page is the same for all. The difference is what each browser remembers.
So when a page behaves differently for different people, suspect stored data, and test in an Incognito window, which starts with empty drawers. New code must cope with old data. Each thing should be kept only as long as it should last. And secrets never belong where scripts can read them.
Take a guess: Where do you test a page that works for you but fails for a new visitor?
A bug from stored data has a strange look: it happens to some people and not to others, or to you and not on a new computer. The page is the same for all. The difference is what each browser remembers.
| Symptom | Likely cause | Look at |
|---|---|---|
| it works for me, and fails for a new visitor | your stored data hides a first-visit bug | an Incognito window |
| it fails for me, and works for a new visitor | old data in your browser | the stores, then Clear site data |
| it fails for a few old customers only | stored data in an old format | the value in the panel |
| a setting is lost at a reload | it was never stored, or stored badly | is the key there? Is the value [object Object]? |
| it forgets on another tab | it is in sessionStorage | Session storage in each tab |
| someone sees another person's data | a shared device, and data that should have gone | what is stored after signing out |
A memory aid
Same page, different result? Different memory.
Stored data outlives your code
This is the trap that catches experienced developers. Your code changes today, and yesterday's data is still sitting in your visitors' browsers. The new code must cope with the old data.
Worked example 5
A shop used to store the cart as the word Rice. A new version stores JSON. New visitors are fine, and old customers see a blank page. Find out why, and fix it.
What we have: a failure for returning visitors only
What we need: the stored value, and code that copes with it
The plan: read the value in the panel: the old format is still there, and the new code cannot read it
- A new visitor's getItem gives null, and JSON.parse takes that without trouble. That is why your own tests pass.
- An old customer's value is the word Rice. JSON.parse cannot read it, the error stops the script, and the page stays blank.
- Wrap the reading in a try, and start afresh if it fails.
An old customer's visit: before
<!DOCTYPE html>
<script>
const cart = JSON.parse(localStorage.getItem("cart"));
console.log(cart.length);
</script>
The Application panel shows
Local storage: cart = Rice
The Console shows
Error Uncaught SyntaxError: Unexpected token 'R', "Rice" is not valid JSON
An old customer's visit: after
<!DOCTYPE html>
<script>
let cart = [];
try {
cart = JSON.parse(localStorage.getItem("cart")) || [];
} catch (e) {
console.log("old data, starting afresh");
}
console.log(cart.length);
</script>
The Application panel shows
Local storage: cart = Rice
The Console shows
Info old data, starting afresh
Info 0
Old data in a format the new code could not read. A try and catch lets the page start afresh.
Check: with the old value in place, the first version stopped with a SyntaxError, and the second carried on.
Worked example 6
A library's panel is used by many readers, and it greets each new one by the last reader's name. The name is stored when a reader signs in. Fix it, so that the name goes when the visit ends.
What we have: a name that outlives the visit
What we need: the store whose lifetime matches
The plan: ask how long this should last: only this visit, so it belongs in sessionStorage
- The panel shows the name under Local storage, which lasts until deleted.
- A reader's name should last only for the visit. One word changes the store.
- On a shared device, also remove it on signing out, because a tab may stay open:
sessionStorage.removeItem("reader").
With sessionStorage, and a new reader in a new tab
<!DOCTYPE html>
<script>
console.log("hello", sessionStorage.getItem("reader"));
sessionStorage.setItem("reader", "Mrs Bose");
</script>
The Console shows
First visit
Info hello null
In a new tab
Info hello null
Use sessionStorage, and remove it on signing out. Choose the store by how long the thing should last.
Check: the new tab read null: the name did not follow.
What must never be stored here
Every store but an HttpOnly cookie can be read by any script on the page: yours, an advertiser's, or an attacker's if one gets in. And anyone with your device, or DevTools, can open the panel.
| Never store | Because | Instead |
|---|---|---|
| passwords | they can be read, and they are for life | send them once, over HTTPS |
| sign-in tokens in localStorage | any script can steal them | an HttpOnly, Secure cookie |
| card numbers | the law and the card networks forbid it | a payment provider's form |
| other people's personal data | a shared device shows it | keep it on the server |
Looking only at your own browser
When you test, you are the best-prepared visitor there will ever be. Every release, try the page in an Incognito window, as someone with nothing stored.
When you are stuck: stored-data bugs
It fails for one customer only: ask what their stores hold; old data is the usual cause.
It works in Incognito and not in your window: Clear site data, and see.
Not sure what a site remembers about you: open every store in the panel, and read.
Your turn
A page works in Incognito, and fails in your normal window. What do you suspect?Tap to reveal
- What does Incognito not have?
Stored data in your browser.
Where should a sign-in token go?Tap to reveal
- No script should read it.
An HttpOnly, Secure cookie.
Does stored data change when you release new code?Tap to reveal
- It is in the visitor's browser.
No: the new code must cope with the old data.
Did you know?
No secrets in localStorage
The security community puts this topic's rule in plain words. OWASP's HTML5 Security Cheat Sheet says: do not store session identifiers in local storage, as the data is always accessible by JavaScript. Cookies can lower that risk with the HttpOnly flag. With it, browsers that support it keep the cookie out of JavaScript APIs such as document.cookie.
Why it fits here: It is the reason for this topic's last rule: a sign-in key in localStorage is open to every script on the page.
HttpOnly, since 2002
HttpOnly is more than twenty years old. Microsoft first added support for it in Internet Explorer 6 SP1, around 2002. Over time it became part of all major browsers, and it is now treated as a standard cookie attribute: the cookie standard says it limits the scope of the cookie to HTTP requests. One word in the Set-Cookie header is enough.
Why it fits here: A sign-in cookie marked HttpOnly cannot be read by any script, even a harmful one that slipped onto the page.
Sources: OWASP: HttpOnly; RFC 6265: HTTP State Management Mechanism
In one line
When a page behaves differently for different people, suspect what each browser remembers. New code must cope with old data, a store should match how long a thing should last, and secrets do not belong where scripts can read them.
Where it is used
Almost every app you use keeps something in these stores.
| What you learned | Where it is used | A real example |
|---|---|---|
| localStorage | settings that stay | the dark mode you chose on a news site |
| sessionStorage | one tab's work | a railway booking that keeps your steps in one tab |
| session cookies | being signed in | your bank's sign-in, which ends when the browser closes |
| HttpOnly and Secure | protecting accounts | any serious site's sign-in cookie |
| IndexedDB | lots of data on the device | an email app that keeps messages offline |
| Cache storage | working offline | a reader app that opens with no signal |
| Clear site data | testing | seeing a site as a brand-new visitor |
| editing values in the panel | testing cases | setting a cart to 99 items to see the page cope |
Mistakes and debugging
These are the storage slips everyone makes.
1. Storing an object directly
It comes back as the words [object Object]. The fix: JSON.stringify going in, JSON.parse coming out.
2. Treating stored numbers as numbers
"3" + 1 is "31". The fix: Number() after reading.
3. Forgetting the first visit
On a real site, for a new visitor
<!DOCTYPE html>
<script>
const settings = JSON.parse(localStorage.getItem("settings"));
console.log(settings.theme);
</script>
The Console shows
Error Uncaught TypeError: Cannot read properties of null (reading 'theme')
A new visitor has nothing stored, so getItem gives null. The fix: give a default for the first visit.
4. Keeping tokens where scripts can reach them
A token in localStorage is one bad script away from being stolen. The fix: an HttpOnly, Secure cookie.
5. Too many cookies
Every cookie goes with every request, images included, so each adds weight to all of them. The fix: keep cookies for what the server must see; use localStorage for the rest.
6. Keeping things for ever
A store with no plan for removal grows until something fails, and a full store raises a QuotaExceededError. The fix: decide when each thing is deleted, before you store it.
7. Testing only with your own data
Your browser has everything set up, so first-visit bugs never show. The fix: test in an Incognito window.
8. Forgetting the code must cope with old data
A change of format breaks only returning visitors, who are your best customers. The fix: read old data carefully, and give each format a version number.
The debugging corner: the Console and the stores
Everything in the panel can be done from the Console. Type localStorage.getItem("cart") to read a value, and localStorage alone to see them all. In the Console, copy(localStorage.getItem("cart")) copies a long value for reading in an editor.
Quick card
The lesson on one card.
Every store in the lesson, on a real site, then in a new tab
<!DOCTYPE html>
<script>
let visits = 0;
try {
visits = JSON.parse(localStorage.getItem("visits")) || 0;
} catch (e) {
visits = 0;
}
visits = visits + 1;
localStorage.setItem("visits", JSON.stringify(visits));
sessionStorage.setItem("tab", "open");
document.cookie = "city=Pune";
console.log("visit", visits);
</script>
The Application panel shows
First visit
Local storage: visits = 1
Session storage: tab = open
Cookie: city=Pune session
Second visit
Local storage: visits = 2
Session storage: tab = open
Cookie: city=Pune session
The server received, for /: city=Pune
In a new tab
Local storage: visits = 3
Session storage: tab = open
Cookie: city=Pune session
The server received, for /: city=Pune
The Console shows
First visit
Info visit 1
Second visit
Info visit 2
In a new tab
Info visit 3
| To do this | Do this |
|---|---|
| open the stores | the Application panel, then Storage |
| store text | localStorage.setItem("k", "v") |
| read it | localStorage.getItem("k"): null if absent |
| store an object | setItem("k", JSON.stringify(obj)) |
| read it back | JSON.parse(getItem("k")) |
| for this tab only | the same, with sessionStorage |
| set a cookie | document.cookie = "city=Pune; max-age=3600" |
| protect a sign-in cookie | HttpOnly; Secure; SameSite=Lax |
| edit a value | double-click it in the panel |
| empty everything | Storage, Clear site data |
| be a new visitor | an Incognito window |
And the rule of the fifth lens: decide when to throw a thing away, before you keep it.
The story: Five drawers
The key opens the glass like a cabinet of five drawers. In the smallest is one slip of paper: reader, Mrs Bose. You pull it out, and the panel greets the next reader as a stranger.
The deeper fault is the choice of drawer. The slip had gone into the one that keeps things for ever, where the one that empties itself when a visit ends was the one wanted. One word in the script moves it across.
Ottilie turns the key in her fingers. "Anything you keep for a visitor," she says, "you must one day throw away. Decide when, before you keep it."
Reagents gathered: Application panel, localStorage, sessionStorage, cookie, HttpOnly, IndexedDB.
Sources
- Chrome DevTools documentation (Page content CC BY 4.0, code samples Apache 2.0 (the footer of its pages, read 2026-11-06); reused with credit)
- MDN Web Docs (Prose CC BY-SA 2.5 or later; code samples added since 2010-08-20 CC0 (its licensing page, read 2026-10-26); reused with credit)