From 229b3d7c270ad221f76cfe7d69f15306b0470bf3 Mon Sep 17 00:00:00 2001 From: Karli Date: Wed, 10 Sep 2025 06:32:48 +0300 Subject: [PATCH] fix warnings on cart page refresh --- app/home/(user)/(dashboard)/cart/page.tsx | 7 ++- .../medusa-storefront/src/lib/data/cookies.ts | 56 +++++++++++-------- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/app/home/(user)/(dashboard)/cart/page.tsx b/app/home/(user)/(dashboard)/cart/page.tsx index 250412b..1bb2675 100644 --- a/app/home/(user)/(dashboard)/cart/page.tsx +++ b/app/home/(user)/(dashboard)/cart/page.tsx @@ -8,6 +8,7 @@ import Cart from '../../_components/cart'; import { listProductTypes } from '@lib/data/products'; import CartTimer from '../../_components/cart/cart-timer'; import { Trans } from '@kit/ui/trans'; +import { withI18n } from '~/lib/i18n/with-i18n'; export async function generateMetadata() { const { t } = await createI18nServerInstance(); @@ -17,9 +18,9 @@ export async function generateMetadata() { }; } -export default async function CartPage() { +async function CartPage() { const cart = await retrieveCart().catch((error) => { - console.error(error); + console.error("Failed to retrieve cart", error); return notFound(); }); @@ -50,3 +51,5 @@ export default async function CartPage() { ); } + +export default withI18n(CartPage); diff --git a/packages/features/medusa-storefront/src/lib/data/cookies.ts b/packages/features/medusa-storefront/src/lib/data/cookies.ts index 7694904..ede7537 100644 --- a/packages/features/medusa-storefront/src/lib/data/cookies.ts +++ b/packages/features/medusa-storefront/src/lib/data/cookies.ts @@ -1,12 +1,20 @@ import "server-only" + import { cookies as nextCookies } from "next/headers" +const CookieName = { + MEDUSA_CUSTOMER_ID: "_medusa_customer_id", + MEDUSA_JWT: "_medusa_jwt", + MEDUSA_CART_ID: "_medusa_cart_id", + MEDUSA_CACHE_ID: "_medusa_cache_id", +} + export const getAuthHeaders = async (): Promise< { authorization: string } | {} > => { try { const cookies = await nextCookies() - const token = cookies.get("_medusa_jwt")?.value + const token = cookies.get(CookieName.MEDUSA_JWT)?.value if (!token) { return {} @@ -23,7 +31,7 @@ export const getMedusaCustomerId = async (): Promise< > => { try { const cookies = await nextCookies() - const customerId = cookies.get("_medusa_customer_id")?.value + const customerId = cookies.get(CookieName.MEDUSA_CUSTOMER_ID)?.value if (!customerId) { return { customerId: null } @@ -31,14 +39,14 @@ export const getMedusaCustomerId = async (): Promise< return { customerId } } catch { - return { customerId: null} + return { customerId: null } } } export const getCacheTag = async (tag: string): Promise => { try { const cookies = await nextCookies() - const cacheId = cookies.get("_medusa_cache_id")?.value + const cacheId = cookies.get(CookieName.MEDUSA_CACHE_ID)?.value if (!cacheId) { return "" @@ -66,51 +74,51 @@ export const getCacheOptions = async ( return { tags: [`${cacheTag}`] } } +const getCookieSharedOptions = () => ({ + maxAge: 60 * 60 * 24 * 7, + httpOnly: false, + secure: process.env.NODE_ENV === "production", +}); +const getCookieResetOptions = () => ({ + maxAge: -1, +}); + export const setAuthToken = async (token: string) => { const cookies = await nextCookies() - cookies.set("_medusa_jwt", token, { - maxAge: 60 * 60 * 24 * 7, - httpOnly: true, - sameSite: "strict", - secure: process.env.NODE_ENV === "production", + cookies.set(CookieName.MEDUSA_JWT, token, { + ...getCookieSharedOptions(), }) } export const setMedusaCustomerId = async (customerId: string) => { const cookies = await nextCookies() - cookies.set("_medusa_customer_id", customerId, { - maxAge: 60 * 60 * 24 * 7, - httpOnly: true, - sameSite: "strict", - secure: process.env.NODE_ENV === "production", + cookies.set(CookieName.MEDUSA_CUSTOMER_ID, customerId, { + ...getCookieSharedOptions(), }) } export const removeAuthToken = async () => { const cookies = await nextCookies() - cookies.set("_medusa_jwt", "", { - maxAge: -1, + cookies.set(CookieName.MEDUSA_JWT, "", { + ...getCookieResetOptions(), }) } export const getCartId = async () => { const cookies = await nextCookies() - return cookies.get("_medusa_cart_id")?.value + return cookies.get(CookieName.MEDUSA_CART_ID)?.value } export const setCartId = async (cartId: string) => { const cookies = await nextCookies() - cookies.set("_medusa_cart_id", cartId, { - maxAge: 60 * 60 * 24 * 7, - httpOnly: true, - sameSite: "strict", - secure: process.env.NODE_ENV === "production", + cookies.set(CookieName.MEDUSA_CART_ID, cartId, { + ...getCookieSharedOptions(), }) } export const removeCartId = async () => { const cookies = await nextCookies() - cookies.set("_medusa_cart_id", "", { - maxAge: -1, + cookies.set(CookieName.MEDUSA_CART_ID, "", { + ...getCookieResetOptions(), }) }