fix warnings on cart page refresh

This commit is contained in:
2025-09-10 06:32:48 +03:00
parent cb11244d79
commit 229b3d7c27
2 changed files with 37 additions and 26 deletions

View File

@@ -8,6 +8,7 @@ import Cart from '../../_components/cart';
import { listProductTypes } from '@lib/data/products'; import { listProductTypes } from '@lib/data/products';
import CartTimer from '../../_components/cart/cart-timer'; import CartTimer from '../../_components/cart/cart-timer';
import { Trans } from '@kit/ui/trans'; import { Trans } from '@kit/ui/trans';
import { withI18n } from '~/lib/i18n/with-i18n';
export async function generateMetadata() { export async function generateMetadata() {
const { t } = await createI18nServerInstance(); 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) => { const cart = await retrieveCart().catch((error) => {
console.error(error); console.error("Failed to retrieve cart", error);
return notFound(); return notFound();
}); });
@@ -50,3 +51,5 @@ export default async function CartPage() {
</PageBody> </PageBody>
); );
} }
export default withI18n(CartPage);

View File

@@ -1,12 +1,20 @@
import "server-only" import "server-only"
import { cookies as nextCookies } from "next/headers" 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< export const getAuthHeaders = async (): Promise<
{ authorization: string } | {} { authorization: string } | {}
> => { > => {
try { try {
const cookies = await nextCookies() const cookies = await nextCookies()
const token = cookies.get("_medusa_jwt")?.value const token = cookies.get(CookieName.MEDUSA_JWT)?.value
if (!token) { if (!token) {
return {} return {}
@@ -23,7 +31,7 @@ export const getMedusaCustomerId = async (): Promise<
> => { > => {
try { try {
const cookies = await nextCookies() const cookies = await nextCookies()
const customerId = cookies.get("_medusa_customer_id")?.value const customerId = cookies.get(CookieName.MEDUSA_CUSTOMER_ID)?.value
if (!customerId) { if (!customerId) {
return { customerId: null } return { customerId: null }
@@ -38,7 +46,7 @@ export const getMedusaCustomerId = async (): Promise<
export const getCacheTag = async (tag: string): Promise<string> => { export const getCacheTag = async (tag: string): Promise<string> => {
try { try {
const cookies = await nextCookies() const cookies = await nextCookies()
const cacheId = cookies.get("_medusa_cache_id")?.value const cacheId = cookies.get(CookieName.MEDUSA_CACHE_ID)?.value
if (!cacheId) { if (!cacheId) {
return "" return ""
@@ -66,51 +74,51 @@ export const getCacheOptions = async (
return { tags: [`${cacheTag}`] } 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) => { export const setAuthToken = async (token: string) => {
const cookies = await nextCookies() const cookies = await nextCookies()
cookies.set("_medusa_jwt", token, { cookies.set(CookieName.MEDUSA_JWT, token, {
maxAge: 60 * 60 * 24 * 7, ...getCookieSharedOptions(),
httpOnly: true,
sameSite: "strict",
secure: process.env.NODE_ENV === "production",
}) })
} }
export const setMedusaCustomerId = async (customerId: string) => { export const setMedusaCustomerId = async (customerId: string) => {
const cookies = await nextCookies() const cookies = await nextCookies()
cookies.set("_medusa_customer_id", customerId, { cookies.set(CookieName.MEDUSA_CUSTOMER_ID, customerId, {
maxAge: 60 * 60 * 24 * 7, ...getCookieSharedOptions(),
httpOnly: true,
sameSite: "strict",
secure: process.env.NODE_ENV === "production",
}) })
} }
export const removeAuthToken = async () => { export const removeAuthToken = async () => {
const cookies = await nextCookies() const cookies = await nextCookies()
cookies.set("_medusa_jwt", "", { cookies.set(CookieName.MEDUSA_JWT, "", {
maxAge: -1, ...getCookieResetOptions(),
}) })
} }
export const getCartId = async () => { export const getCartId = async () => {
const cookies = await nextCookies() 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) => { export const setCartId = async (cartId: string) => {
const cookies = await nextCookies() const cookies = await nextCookies()
cookies.set("_medusa_cart_id", cartId, { cookies.set(CookieName.MEDUSA_CART_ID, cartId, {
maxAge: 60 * 60 * 24 * 7, ...getCookieSharedOptions(),
httpOnly: true,
sameSite: "strict",
secure: process.env.NODE_ENV === "production",
}) })
} }
export const removeCartId = async () => { export const removeCartId = async () => {
const cookies = await nextCookies() const cookies = await nextCookies()
cookies.set("_medusa_cart_id", "", { cookies.set(CookieName.MEDUSA_CART_ID, "", {
maxAge: -1, ...getCookieResetOptions(),
}) })
} }