Storage

Save values in the browser with Salla.storage, read what the SDK saves, and expire values after a time.

Read this when browser code must remember something between pages, or read what the SDK remembers about the cart and the customer.

Salla.storage saves values in the browser's local storage, so they survive a reload and a new tab. If local storage is blocked, it falls back to session storage, then cookies, then memory.

Save, read and remove

Salla.storage.set('my-theme.last-category', 42);
Salla.storage.get('my-theme.last-category');          // 42
Salla.storage.get('my-theme.missing', 'default');     // 'default'
Salla.storage.remove('my-theme');

Dots reach into a nested object: set('my-theme.last-category', 42) saves { "last-category": 42 } under the key my-theme. Values can be any JSON value.

Expire a value

setWithTTL saves a value for a number of minutes (10 by default). getWithTTL returns the default once it has expired.

Salla.storage.setWithTTL('promo-closed', true, 60);   // one hour
Salla.storage.getWithTTL('promo-closed', false);      // true, then false after an hour

The last argument picks where to save: 'store' (local storage, the default), 'session' or 'cookie'. These keys are saved per store, so the same browser on two stores does not mix them up.

What the SDK saves

The SDK keeps these keys itself. Read them, but change them only through the SDK methods.

KeyHolds
cart.idThe current cart's id
cart.summarycount, total, sub_total, discount, tax_amount, total_discount, shipping_cost and real_shipping_cost
tokenThe logged-in customer's token
userThe customer's profile, saved at login
salla::wishlistThe ids of the products in the wishlist
salla::availabilityThe ids of products the customer asked to be told about when back in stock

For the cart count in a component, listen to cart::updated instead of reading storage: storage does not tell you when it changes. See Events.

Clear

Salla.storage.clearAll() removes only the keys in the table above, as at logout. Your own keys stay. Salla.storage.clearAll(true) removes everything in local storage.

Cookies

Salla.cookie works the same way for cookies:

MethodDoes
Salla.cookie.get(name)Reads a cookie
Salla.cookie.set(name, value, days?)Saves a cookie, for 10 days unless you pass days
Salla.cookie.remove(name)Deletes a cookie

Gotchas

  • Storage exists only in the browser. Read it in useEffect or an event handler, never while rendering.
  • Give your keys your own prefix, such as your theme name, so they never clash with the SDK's.
  • Do not write cart or token yourself. The SDK resets the cart when its saved data does not match the store or the customer.

See also

On this page