From fc661b75ec947445c10e2496d86a89cfa2e7969b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 21 May 2025 06:19:11 +0000 Subject: [PATCH] Fix: Gracefully handle cart fetching for _not-found page The prerendering of the `/_not-found` page was failing due to an error in the Server Components render, specifically when the `RootLayout` attempted to fetch cart data using `getCart()`. The `getCart()` function uses `cookies()`, which is not available or reliable during the static prerendering of error pages. This commit modifies `app/layout.tsx` to wrap the `getCart()` call in a `try...catch` block. If an error occurs during this call (e.g., during build-time rendering of `_not-found.tsx`), it now logs the error and defaults to a `Promise.resolve(undefined)` for the cart state. The `CartProvider` and related components are already designed to handle an undefined cart, ensuring that your application remains stable and the build process can complete successfully for static pages like `_not-found`. --- app/layout.tsx | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/app/layout.tsx b/app/layout.tsx index 5e3355ce9..82ff768f9 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -3,6 +3,7 @@ import { Navbar } from 'components/layout/navbar'; import { WelcomeToast } from 'components/welcome-toast'; import { GeistSans } from 'geist/font/sans'; import { getCart } from 'lib/shopify'; +import type { Cart } from 'lib/shopify/types'; import { ReactNode } from 'react'; import { Toaster } from 'sonner'; import './globals.css'; @@ -27,13 +28,20 @@ export default async function RootLayout({ }: { children: ReactNode; }) { - // Don't await the fetch, pass the Promise to the context provider - const cart = getCart(); + let cartPromise: Promise; + + try { + // Don't await the fetch, pass the Promise to the context provider + cartPromise = getCart(); + } catch (e) { + console.error('Failed to get cart during layout rendering (possibly build time for static page):', e); + cartPromise = Promise.resolve(undefined); + } return ( - +
{children}