DEV Community

Mike Rispoli
Mike Rispoli

Posted on

5

Is using localStorage for a guest shopping cart a bad idea?

I've found myself building some custom, basic e-commerce sites these days in Rails. I have a few options when it comes to building out a basic guest user shopping cart. These sites will not have logged in users so it is not necessary to tie the cart to a user account in any way. This has led me to consider using the localStorage api to persist the cart. The other option is to go with the tried and true store the cart in a database temporarily and tie this to the user via cookie in the browser.

In terms of browser support, localStorage sports browser support of IE8 and up. This is more than sufficient. In terms of speed and simplicity to develop, I believe that localStorage also has the win here.

While a database is more secure, in this case it's just storing an array of products to be later turned into an order. This order content is going to be sanitized by the server so that isn't a huge strike against localStorage either.

So I pose the question to the community, what are the drawbacks of using a localStorage based shopping cart for this scenario? Has anyone done this only to regret it later?

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (4)

Collapse
 
rhymes profile image
rhymes •

I'd consider doing the same, worst case scenario the user opens the browser development tools, deletes the local storage and well... they have to re-do the cart :)

As long as you don't store sensitive data you're fine.

I'd consider using localForage that transparently abstracts on top of IndexedDB and uses localStorage as a fallback.

Collapse
 
mrispoli24 profile image
Mike Rispoli •

Ah this is an interesting. I was actually considering using Vue in a scaled down approach just for the cart and binding local storage to a global store to handle addition and removal from different components. It’s a very simple site rails site otherwist, so I wanted to take as light handed an approach as possible. I’ll be taking a look at localForage which might work out better.

Collapse
 
rhymes profile image
rhymes •

You can do the same, localForage is just a wrapper on top of the variouses client storage options. Check if it's not too big for your app and if it fits your requirements, otherwise toss it :D

Collapse
 
xngwng profile image
Xing Wang •

For storing Cart items, this is perfect use case for local storage in my opinion.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up