Chapter 8 of 10All chapters
Chapter 8 of 10
State and storage
Remembering things in the browser.
The options
localStorage keeps small values on one device until cleared. sessionStorage lasts for the tab. Cookies go to the server with every request, which is why sessions use them.
- Never store anything sensitive in the browser; the user and their extensions can read it.
- Assume storage can fail or be empty and handle that path.
URLs are state too
Putting filters and page numbers in the URL makes a view shareable, bookmarkable and survivable across reloads. It is the cheapest state management there is.