Hi.
I’m using the Storefront API to build an E-commerce platform with NextJS, but I’m having a problem where I run the cart query but get back an outdated version of the cart (at least in local dev).
When adding a new line I’m using the cart object I get in the response to refresh my state with that new cart, and it works, while also saving the cartId on cookies or local storage to retrieve it in case the user closes the page or refreshes, however, if the user does that (refresh or close and open later) I fetch the cart with the cart query, but I get an older version of my cart, and it updates eventually after several minutes.
I’ve tried multiple things, but I would expect the cart query to always return the latest cart. Also, the checkoutUrl is correct and the products shown in the checkout are correct, as well as the pricing, but if the user reloads the page with 6 items in the cart they might get 4 items on the new refreshed page.
Here’s the code for my query, I’m retrieving the first 100 variants just for testing. I’ve ensured the ID is correct and is always updated when a new cart is created or a new line is added to the existing cart (even though most of the time the ID doesn’t really change):
query cart($id: ID!) {
cart(id: $id) {
id
checkoutUrl
createdAt
updatedAt
lines(first: 100) {
edges {
node {
id
quantity
merchandise {
... on ProductVariant {
id
title
image {
url
}
price {
amount
currencyCode
}
compareAtPrice {
amount
currencyCode
}
product {
title
handle
}
}
}
}
}
}
cost {
subtotalAmount {
amount
currencyCode
}
totalAmount {
amount
currencyCode
}
}
}
}