# localStorage and sessionStorage in Safari's private mode

DevFeed: [localStorage and sessionStorage in Safari's private mode](<https://devfeed.tech/articles/localstorage-and-sessionstorage-in-safari-s-private-mode-37306.md>)

Original publisher: [Read original article](<https://muffinman.io/blog/localstorage-and-sessionstorage-in-safaris-private-mode/>)

Author: Stanko

Published: 2017-08-09T00:00:00Z

Content type: tutorial

Language: en

Sources: [Stanko Tadić](<https://devfeed.tech/sources/stanko-tadic.md>)

Topics: [LocalStorage](<https://devfeed.tech/topics/localstorage.md>), [Code](<https://devfeed.tech/topics/code.md>), [App](<https://devfeed.tech/topics/app.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [error](<https://devfeed.tech/tags/error.md>), [export](<https://devfeed.tech/tags/export.md>), [ls](<https://devfeed.tech/tags/ls.md>), [null](<https://devfeed.tech/tags/null.md>), [object](<https://devfeed.tech/tags/object.md>), [qa](<https://devfeed.tech/tags/qa.md>), [quota](<https://devfeed.tech/tags/quota.md>), [return](<https://devfeed.tech/tags/return.md>), [safari](<https://devfeed.tech/tags/safari.md>), [storage](<https://devfeed.tech/tags/storage.md>), [test](<https://devfeed.tech/tags/test.md>), [value](<https://devfeed.tech/tags/value.md>), [version](<https://devfeed.tech/tags/version.md>)

## AI overview

This article explains that Safari private mode sets localStorage and sessionStorage limits to zero, preventing writes. It presents a localStorage facade that silently ignores storage operations when storage is unavailable, avoiding application errors.

## Source excerpt

If you didn't know, in Safari's private mode both localStorage and sessionStorage are not working. To be exact, Safari sets storage's limit to 0, so you can't write anything to it. I keep forgetting this, until QA people report it at some point. So I quickly wrote a small facade for it, which fails silently in this case. That means it still doesn't work but it won't throw an error and break your application. This is the version for localStorage, just replace it with sessionStorage if you need it. const LS_TEST_KEY = 'ls-test'; let isLocalStorageSupported = typeof localStorage === 'object'; // Try to try { localStorage.setItem(LS_TEST_KEY, 'test'); localStorage.removeItem(LS_TEST_KEY); } catch (e) { isLocalStorageSupported = false; // If we get error that we exceeded storage's quota // but storage is still empty we are in private mode if (e.code === DOMException.QUOTA_EXCEEDED_ERR && localStorage.length === 0) { // Private mode } else { throw e; } } const LocalStorage = { getItem: (key) => { if (isLocalStorageSupported) { return localStorage.getItem(key); } return null; }, setItem: (key, value) => { if (isLocalStorageSupported) { localStorage.setItem(key, value); } }, removeItem: (key) => { if (isLocalStorageSupported) { localStorage.removeItem(key); } }, }; export default LocalStorage;