localStorageis scoped to the browser’s window. If the user reloads the page or closes and re-opens the browser, the state persists. If the user opens multiple browser tabs, the state is shared across the tabs. Data persists in
localStorageuntil explicitly cleared.
sessionStorageis scoped to the browser tab. If the user reloads the tab, the state persists. If the user closes the tab or the browser, the state is lost. If the user opens multiple browser tabs, each tab has its own independent version of the data.
sessionStorage can be used in Blazor WebAssembly apps but only by writing custom code or using a third-party package.
sessionStorage is safer to use.
sessionStorage avoids the risk that a user opens multiple tabs and encounters the following:
- Bugs in state storage across tabs.
- Confusing behavior when a tab overwrites the state of other tabs.
localStorage is the better choice if the app must persist state across closing and re-opening the browser.
Users may view or tamper with the data stored in