Pricing Blog

shopping cart - ✨instant feedback✨

  • needtheanswers-1366814561415594216

    needtheanswers

    3 months ago

    Hi there,
    just wondering how do you guys handle the shopping cart functionality in your onlineshop. Normally when as user clicks on "add to cart" I would call my api(s) and let the backend handle this (add item, delete item, update quantity,..). For safety reasons I didn´t want the frontend to handle all of that ( reducing the risk of manipulation). The problem is that it kinda disrupts the flow because it takes a second or two until I get the data with the updated cart. So my question now is how do you handle this ? Do you mantain a second cart in the local storage or what is your solution for that?
    Thanks
  • lucasg-1366825284413948095

    Lucas G

    3 months ago

    Optimistic updates
  • Is the name for the concept
  • Basically you update the UI but track the updates/retries in the background
  • Something like a websocket for the cart could also be an option
  • needtheanswers-1366827145015595009

    needtheanswers

    3 months ago

    Never heard of that, but I´m gonna google that right away!
  • needtheanswers-1366827243174629427

    needtheanswers

    3 months ago

    That´s exactly what I had in mind
  • needtheanswers-1366827353296207944

    needtheanswers

    3 months ago

    Going to look into that as well
  • Thank you @Lucas G 🙂
  • needtheanswers-1367030035919409242

    needtheanswers

    3 months ago

    One question. If I go for optimistic updates, what would be a good way to sync the data? @Lucas G
  • lucasg-1367137268275609723

    Lucas G

    3 months ago

    You would still make the calls when the users performs an action, you're just not waiting for the API
  • Simplest way is to manage the UI via a 'cart' variable for example
  • Set the variable and call your API then handle errors/retries if any on the background
  • needtheanswers-1367140784695935088

    needtheanswers

    3 months ago

    That makes sense! I think I should also add a debounce so the api doesn´t get called too frequently. Generally speaking, would you prefer this approach to using websockets?
  • lucasg-1367275597058932826

    Lucas G

    3 months ago

    Yeah you could though I'm not sure if carts usually get updated that often
  • And it depends, probably not.
  • Simple calls are good enough for something like a cart. It's not something like a chat
  • needtheanswers-1367381859788390461

    needtheanswers

    3 months ago

    Hmm interesting, I´ll look into that and try to only update it where/when it makes sense
  • Thanks for sharing your opinion and giving me advice 🙏