diff --git a/app/page.tsx b/app/page.tsx index e63fea3e4..eca4fcd86 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,9 +1,9 @@ import { Carousel } from 'components/carousel'; +import { getCartId } from 'components/cart/actions'; import { ThreeItemGrid } from 'components/grid/three-items'; import Footer from 'components/layout/footer'; import { Wrapper } from 'components/wrapper'; import { getCart } from 'lib/fourthwall'; -import { cookies } from 'next/headers'; export const metadata = { description: 'High-performance ecommerce store built with Next.js, Vercel, and Fourthwall.', @@ -13,7 +13,7 @@ export const metadata = { }; export default async function HomePage({ searchParams }: { searchParams: { currency?: string } }) { - const cartId = cookies().get('cartId')?.value; + const cartId = await getCartId(); const currency = searchParams.currency || 'USD'; // Don't await the fetch, pass the Promise to the context provider const cart = getCart(cartId, currency); diff --git a/app/product/[handle]/page.tsx b/app/product/[handle]/page.tsx index ebaac2b1e..887abbcba 100644 --- a/app/product/[handle]/page.tsx +++ b/app/product/[handle]/page.tsx @@ -1,6 +1,7 @@ import type { Metadata } from 'next'; import { notFound } from 'next/navigation'; +import { getCartId } from 'components/cart/actions'; import Footer from 'components/layout/footer'; import { Gallery } from 'components/product/gallery'; import { ProductProvider } from 'components/product/product-context'; @@ -8,7 +9,6 @@ import { ProductDescription } from 'components/product/product-description'; import { Wrapper } from 'components/wrapper'; import { HIDDEN_PRODUCT_TAG } from 'lib/constants'; import { getCart, getProduct } from 'lib/fourthwall'; -import { cookies } from 'next/headers'; import { Suspense } from 'react'; export async function generateMetadata({ @@ -51,7 +51,7 @@ export async function generateMetadata({ export default async function ProductPage({ params, searchParams }: { params: { handle: string }, searchParams: { currency?: string } }) { const currency = searchParams.currency || 'USD'; - const cartId = cookies().get('cartId')?.value; + const cartId = await getCartId() const cart = getCart(cartId, currency) diff --git a/components/cart/actions.ts b/components/cart/actions.ts index b52e7de0b..ab8092138 100644 --- a/components/cart/actions.ts +++ b/components/cart/actions.ts @@ -6,8 +6,18 @@ import { revalidateTag } from 'next/cache'; import { cookies } from 'next/headers'; import { redirect } from 'next/navigation'; +export async function getCartId(): Promise { + const tokenHash = process.env.NEXT_PUBLIC_FW_STOREFRONT_TOKEN; + return cookies().get(`${tokenHash}/cartId`)?.value; +} + +function setCartId(cartId: string) { + const tokenHash = process.env.NEXT_PUBLIC_FW_STOREFRONT_TOKEN; + cookies().set(`${tokenHash}/cartId`, cartId); +} + export async function addItem(prevState: any, selectedVariantId: string | undefined) { - let cartId = cookies().get('cartId')?.value; + let cartId = await getCartId(); if (!cartId || !selectedVariantId) { return 'Error adding item to cart'; @@ -22,7 +32,7 @@ export async function addItem(prevState: any, selectedVariantId: string | undefi } export async function removeItem(prevState: any, merchandiseId: string) { - let cartId = cookies().get('cartId')?.value; + let cartId = await getCartId(); if (!cartId) { return 'Missing cart ID'; @@ -98,7 +108,7 @@ export async function updateItemQuantity( export async function redirectToCheckout(currency: string) { const CHECKOUT_URL = process.env.NEXT_PUBLIC_FW_CHECKOUT; - let cartId = cookies().get('cartId')?.value; + let cartId = await getCartId(); if (!cartId) { return 'Missing cart ID'; @@ -115,5 +125,5 @@ export async function redirectToCheckout(currency: string) { export async function createCartAndSetCookie() { let cart = await createCart(); - cookies().set('cartId', cart.id!); + setCartId(cart.id!!); } diff --git a/next.config.js b/next.config.js index 750a81193..15e6f25df 100644 --- a/next.config.js +++ b/next.config.js @@ -5,8 +5,8 @@ module.exports = { remotePatterns: [ { protocol: 'https', - hostname: 'storage.googleapis.com', - pathname: '/cdn.staging.fourthwall.com/**' + hostname: '**', + pathname: '**' } ] }