fix: remove useTheme from Sonner (useContext null crash on global-error prerender)
This commit is contained in:
1 parent
d71bd76e23
commit
dd0b88b6eb
2 files changed
+10
-6
No files matched your search
@@ -1,8 +1,11 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
// global-error.tsx — Next.js special page, rendered OUTSIDE the root layout.
|
// 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.
|
// Must be fully self-contained: no ThemeProvider, no Sonner, no context hooks.
|
||||||
// Replaces the entire <html> + <body> tree when a global error occurs.
|
// 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({
|
export default function GlobalError({
|
||||||
error,
|
error,
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import { useTheme } from "next-themes"
|
|
||||||
import { Toaster as Sonner, ToasterProps } from "sonner"
|
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 <html> class attribute (set by next-themes).
|
||||||
const Toaster = ({ ...props }: ToasterProps) => {
|
const Toaster = ({ ...props }: ToasterProps) => {
|
||||||
const { theme = "system" } = useTheme()
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Sonner
|
<Sonner
|
||||||
theme={theme as ToasterProps["theme"]}
|
theme="system"
|
||||||
className="toaster group"
|
className="toaster group"
|
||||||
style={
|
style={
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user