4
0
forked from crowetic/commerce

Cache the cart content in VDC

This commit is contained in:
Malte Ubl 2024-02-05 06:40:52 -08:00
parent 0c3283f591
commit 5304683150

View File

@ -210,6 +210,10 @@ export async function createCart(): Promise<Cart> {
return reshapeCart(res.body.data.cartCreate.cart); return reshapeCart(res.body.data.cartCreate.cart);
} }
function revalidateCart(cartId: string) {
revalidateTag(`cart-${cartId}`);
}
export async function addToCart( export async function addToCart(
cartId: string, cartId: string,
lines: { merchandiseId: string; quantity: number }[] lines: { merchandiseId: string; quantity: number }[]
@ -222,6 +226,7 @@ export async function addToCart(
}, },
cache: 'no-store' cache: 'no-store'
}); });
revalidateCart(cartId);
return reshapeCart(res.body.data.cartLinesAdd.cart); return reshapeCart(res.body.data.cartLinesAdd.cart);
} }
@ -234,6 +239,7 @@ export async function removeFromCart(cartId: string, lineIds: string[]): Promise
}, },
cache: 'no-store' cache: 'no-store'
}); });
revalidateCart(cartId);
return reshapeCart(res.body.data.cartLinesRemove.cart); return reshapeCart(res.body.data.cartLinesRemove.cart);
} }
@ -250,6 +256,7 @@ export async function updateCart(
}, },
cache: 'no-store' cache: 'no-store'
}); });
revalidateCart(cartId);
return reshapeCart(res.body.data.cartLinesUpdate.cart); return reshapeCart(res.body.data.cartLinesUpdate.cart);
} }
@ -258,8 +265,7 @@ export async function getCart(cartId: string): Promise<Cart | undefined> {
const res = await shopifyFetch<ShopifyCartOperation>({ const res = await shopifyFetch<ShopifyCartOperation>({
query: getCartQuery, query: getCartQuery,
variables: { cartId }, variables: { cartId },
tags: [TAGS.cart], tags: [TAGS.cart, `cart-${cartId}`]
cache: 'no-store'
}); });
// Old carts becomes `null` when you checkout. // Old carts becomes `null` when you checkout.