Skip to content
Niket Pathak edited this page Apr 15, 2024 · 6 revisions

FAQ (Frequently Asked Questions)

How to use localStorage and sessionStorage together with this library ?

currently, switching between 2 different storage types on the fly within the same instance is not supported. The typing info should have been more specific along with some documentation. Thanks to you, this typing info will be added in the next release. However, currently, you can use either localStorage or sessionStorage but not both on the same instance. So what you did in your JS fiddle (ls.set('x', {storage: sessionStorage}); ) has no effect because the storage option is completely ignored while using the get() or set() API. you can only define the storage globally.

If you need to use both sessionStorage and localStorage together with this library, you will have to duplicate your imports, for example

import ls from 'localstorage-slim';
import ss from 'localstorage-slim';


ss.config.storage = sessionStorage;  // configure sessionStorage globally
// ls.config.storage = localStorage; (this is not required since the library defaults to using localStorage)

ss.set('item 1', 'value 1'); // stored in sessionStorage
ls.set('item A', 'value A'); // stored in localStorage

If you are using a CDN, then the library is exposed at window.ls. so to use sessionStorage alone, you can update the global config, window.ls.config.storage = sessionStorage;

However, if you wish to use the same library for both local + session storage via CDN, you can achieve that by doing

// duplicate the library (i.e create 2 different instances of the same library)
window.ss = {
 ...window.ls
} ;

window.ss.config.storage = sessionStorage; // configure sessionStorage globally
// window.ls.config.storage = localStorage; // this line is not needed as it defaults to using localStorage

window.ss.set('item 1', 'value 1'); // stored in sessionStorage
window.ls.set('item A', 'value A'); // stored in localStorage

Clone this wiki locally