Best way to implement a cart?
I've implemented a cart using jotai's atomWithStorage to save the items in lcoalStorage, but I was wondering if I should store the cart in the server instead?
In this specific app only logged in users can access the store. Are there other better ways of implementing a cart?
25 Replies
Server side is probably the better way plus you can have the user save their carts across devices and also it'll let you solve a super super common race condition in ecommerce where you sell more items than you have quantity for
If this is a project for fun you'd get a lot of clout for talking about this during an interview
Just moving to serverside isnt enough to solve the problem but its a start, i just wasn't spoiling it since its kinda fun to discover on your own
It's actually a real project π
I've implemented carts before but on a different framework
but I guess the requirement that only logged in users can add to cart really helps me here
Oh yeah then i'd definitely do serverside and to spill the beans you'd want some kind of way of knowing if you should proceed with the transaction or not
Something like locks in a database, using redis etc
Some ways people solve it is to hold stock for x minutes
yeah that's interesting
this specific app doesn't really care about stock so I guess I should just create a Cart model and associate it with the user
Cart in local storage works and if that meets your needs its fine. The sharing carts across device is a fluff thing but its kinda cute
there is actually an argument I don't know how to counter, which is that developers tell me - "just fetch the items on initial render and store them in a global store"
and same here - one can tell me "why send a request on each 'add to cart'"?
React Conferences by GitNation
YouTube
React Query: Itβs Time to Break up with your "Global Stateβ! βTanne...
Find the latest React.js talks & workshops at https://portal.gitnation.org
Β
Β π Talk recording from React Summit Remote Edition 2020
Website - https://remote.reactsummit.com/
See other React conferences by GitNation
React Summit β https://reactsummit.com
React Advanced London β https://reactadvanced.com
React Day Berlin β https://reactday.be...
In my opinion it's a bad idea to hold items in a global store but dunno how to make the case that sending requests on each page load or 'add to cart' click isn't that bad
This is a really goooooooood talk and if you aren't familiar with it tanner is one of the people behind react-query
yeah am familiar with this wizard
ill watch it now thank you
Its just really about trade offs and how accurate you want to be
Using react query makes it a non issue honestly since the api makes it easy to refetch/render and you'll also have the most recent fetch. Networking is also cheap now so I just think they are being stingy
yeah that's a good point
thanks for the help I really appreciate it π
np np gl!!
@CFKeef Oh btw one last question π - say I have a Product model with all the relevant info, and I also have a Cart model now that I go with serverside Cart, I probably have to add a third CartProduct model that has a relation to a Product and a quantity property right?
we're talking Prisma sql
It'd be a 1 to many from product -> cart so you can just do something like this and you don't need to write a bridge table
Have you worked with redis before?
I'd just use redis for something like this since using the TTL lets you nicely clear up old data without explicitly having to do it
so basically the quantity of a product is the num of times it's in the cart?
So I'd use a compound key of something like
userId:cart
and have a json blob of the relevant details with a time to live like idk a week or something
Oh sorry yeah you'd prob need a bridge table if you want to "enhance" the relationshipbasically all i need is quantity
so was wondering if i could get it done without a bridge
Redis π
i'll look into it now
You could also use a metadata JSON field in your record if you want to get a little dirty about it but I think redis is the cleaner approach
Cause using records in your database I'd bring up how would you deal with "ancient" carts?
yeah that's right
thank you again for the valuable info π
Redis is a REALLLLLY GOOOOD PIECE OF TECH
So its well worth learning it
np np np