From 5304683150bd3f3ff5ebfdcaf6e681a4938d7cd0 Mon Sep 17 00:00:00 2001 From: Malte Ubl Date: Mon, 5 Feb 2024 06:40:52 -0800 Subject: [PATCH] Cache the cart content in VDC --- lib/shopify/index.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/shopify/index.ts b/lib/shopify/index.ts index e8b6637c8..09a61959b 100644 --- a/lib/shopify/index.ts +++ b/lib/shopify/index.ts @@ -210,6 +210,10 @@ export async function createCart(): Promise { return reshapeCart(res.body.data.cartCreate.cart); } +function revalidateCart(cartId: string) { + revalidateTag(`cart-${cartId}`); +} + export async function addToCart( cartId: string, lines: { merchandiseId: string; quantity: number }[] @@ -222,6 +226,7 @@ export async function addToCart( }, cache: 'no-store' }); + revalidateCart(cartId); return reshapeCart(res.body.data.cartLinesAdd.cart); } @@ -234,6 +239,7 @@ export async function removeFromCart(cartId: string, lineIds: string[]): Promise }, cache: 'no-store' }); + revalidateCart(cartId); return reshapeCart(res.body.data.cartLinesRemove.cart); } @@ -250,6 +256,7 @@ export async function updateCart( }, cache: 'no-store' }); + revalidateCart(cartId); return reshapeCart(res.body.data.cartLinesUpdate.cart); } @@ -258,8 +265,7 @@ export async function getCart(cartId: string): Promise { const res = await shopifyFetch({ query: getCartQuery, variables: { cartId }, - tags: [TAGS.cart], - cache: 'no-store' + tags: [TAGS.cart, `cart-${cartId}`] }); // Old carts becomes `null` when you checkout.