createLocalStorage
Create a storage instance backed by globalThis.localStorage. The underlying storage is resolved lazily from globalThis when the instance is used, so it can be imported before a browser-like global exists. Values persist across page reloads and are synced across tabs via the storage event when available.
Usage
js
import { createLocalStorage } from '@studiometa/js-toolkit/utils';
const storage = createLocalStorage({ prefix: 'myapp:' });
storage.set('theme', 'dark');
storage.get('theme'); // 'dark'
storage.get('lang', 'en'); // 'en' (default)
// React to changes (including from other tabs)
const unsubscribe = storage.subscribe('theme', (value) => {
document.documentElement.dataset.theme = value;
});Parameters
options(StorageOptions): Optional configuration.options.prefix(string): Key prefix for namespacing.options.serializer(StorageSerializer): Custom serializer. Defaults to JSON.
Return value
StorageInstance<T>: See createStorage for the full API.