Skip to content

Commit 1ed594d

Browse files
committed
feat: perf enhancements
1 parent 018858e commit 1ed594d

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

apps/storefront/app/routes/checkout._index.tsx

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,16 @@ const findCheapestShippingOption = (shippingOptions: StoreCartShippingOption[])
3636
};
3737

3838
const ensureSelectedCartShippingMethod = async (request: Request, cart: StoreCart) => {
39-
const shippingOptions = await fetchShippingOptions(cart.id);
40-
4139
const selectedShippingMethod = cart.shipping_methods?.[0];
4240

43-
const selectedShippingOption =
44-
selectedShippingMethod && shippingOptions.find((option) => option.id === selectedShippingMethod.shipping_option_id);
41+
if (selectedShippingMethod) return;
4542

46-
if (
47-
!selectedShippingMethod || // No shipping method has been selected
48-
!selectedShippingOption // The selected shipping method is no longer available
49-
) {
50-
const cheapestShippingOption = findCheapestShippingOption(shippingOptions);
43+
const shippingOptions = await fetchShippingOptions(cart.id);
5144

52-
if (cheapestShippingOption) {
53-
await setShippingMethod(request, { cartId: cart.id, shippingOptionId: cheapestShippingOption.id });
54-
}
45+
const cheapestShippingOption = findCheapestShippingOption(shippingOptions);
5546

56-
return;
57-
} else if (selectedShippingMethod.amount !== selectedShippingOption.amount) {
58-
await setShippingMethod(request, { cartId: cart.id, shippingOptionId: selectedShippingOption.id });
47+
if (cheapestShippingOption) {
48+
await setShippingMethod(request, { cartId: cart.id, shippingOptionId: cheapestShippingOption.id });
5949
}
6050
};
6151

@@ -64,9 +54,10 @@ const ensureCartPaymentSessions = async (request: Request, cart: StoreCart) => {
6454

6555
let activeSession = cart.payment_collection?.payment_sessions?.find((session) => session.status === 'pending');
6656

67-
const paymentProviders = await listCartPaymentProviders(cart.region_id!);
57+
if (!activeSession) {
58+
const paymentProviders = await listCartPaymentProviders(cart.region_id!);
59+
if (!paymentProviders.length) return activeSession;
6860

69-
if (!activeSession && paymentProviders.length) {
7061
const provider = paymentProviders.find((p) => p.id !== SYSTEM_PROVIDER_ID) || paymentProviders[0];
7162

7263
const { payment_collection } = await initiatePaymentSession(request, cart, {

apps/storefront/libs/util/server/data/cart.server.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export const retrieveCart = withAuthHeaders(async (request, authHeaders) => {
2222
});
2323
});
2424

25+
export const createCart = withAuthHeaders(async (request, authHeaders, data: HttpTypes.StoreCreateCart) => {
26+
return await sdk.store.cart.create({ ...data }, {}, authHeaders);
27+
});
28+
2529
export const getOrCreateCart = withAuthHeaders(async (request, authHeaders) => {
2630
let cart = await retrieveCart(request);
2731

@@ -67,23 +71,25 @@ export const addToCart = withAuthHeaders(
6771
throw new Error('Missing variant ID when adding to cart');
6872
}
6973

70-
const cart = await getOrCreateCart(request);
71-
72-
if (!cart) {
73-
throw new Error('Error retrieving or creating cart');
74-
}
74+
const cartId = await getCartId(request.headers);
7575

76-
return await sdk.store.cart
77-
.createLineItem(
78-
cart.id,
76+
if (cartId) {
77+
return await sdk.store.cart.createLineItem(
78+
cartId,
7979
{
8080
variant_id: variantId,
8181
quantity,
8282
},
8383
{},
8484
authHeaders,
85-
)
86-
.catch(medusaError);
85+
);
86+
}
87+
88+
const region = await getSelectedRegion(request.headers);
89+
90+
const cart = await createCart(request, { region_id: region.id, items: [{ variant_id: variantId, quantity }] });
91+
92+
return cart;
8793
},
8894
);
8995

0 commit comments

Comments
 (0)