From dd0b88b6eb54c7ec924ef567603817098efe5528 Mon Sep 17 00:00:00 2001 From: Z User Date: Wed, 28 Oct 2026 19:03:44 +0000 Subject: [PATCH] fix: remove useTheme from Sonner (useContext null crash on global-error prerender) --- src/app/global-error.tsx | 7 +++++-- src/components/ui/sonner.tsx | 9 +++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/app/global-error.tsx b/src/app/global-error.tsx index b909a6b..1c17f12 100644 --- a/src/app/global-error.tsx +++ b/src/app/global-error.tsx @@ -1,8 +1,11 @@ "use client"; // global-error.tsx — Next.js special page, rendered OUTSIDE the root layout. -// It must be fully self-contained: no ThemeProvider, no Sonner, no context hooks. -// Replaces the entire + tree when a global error occurs. +// Must be fully self-contained: no ThemeProvider, no Sonner, no context hooks. +// dynamic = 'force-dynamic' prevents static prerendering which crashes because +// the build tries to resolve the full module graph including layout providers. + +export const dynamic = "force-dynamic"; export default function GlobalError({ error, diff --git a/src/components/ui/sonner.tsx b/src/components/ui/sonner.tsx index 957524e..b48dd04 100755 --- a/src/components/ui/sonner.tsx +++ b/src/components/ui/sonner.tsx @@ -1,14 +1,15 @@ "use client" -import { useTheme } from "next-themes" import { Toaster as Sonner, ToasterProps } from "sonner" +// NOTE: We intentionally do NOT import useTheme from next-themes here. +// During global-error prerendering, ThemeProvider is not mounted, and +// useTheme() crashes with "Cannot read properties of null (reading 'useContext')". +// Instead, we read the theme from the class attribute (set by next-themes). const Toaster = ({ ...props }: ToasterProps) => { - const { theme = "system" } = useTheme() - return (