Intermittent Hydration Error After Page Reload with Better Auth getSession() #89535
Replies: 3 comments 1 reply
|
I'd move the authentication check to Middleware ( import { NextResponse, type NextRequest } from "next/server";
import { getSessionCookie } from "better-auth/cookies"; // you said you were using better-auth
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const isAuthRoute = pathname.startsWith("/sign-in") || pathname.startsWith("/sign-up");
const sessionCookie = getSessionCookie(request);
// If user is logged in and trying to access auth pages -> redirect to home page
if (sessionCookie && isAuthRoute) {
return NextResponse.redirect(new URL("/", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
// Apply to your auth routes
"/sign-in",
"/sign-up",
],
};than update |
0 replies
|
I haven't taken a full look yet, but |
0 replies
|
This explicitly wrapping the session check in Suspense resolved the issue. import { Suspense } from "react";
import { headers } from "next/headers";
import { redirect } from "next/navigation";
import { auth } from "@/app/lib/auth";
export default function AuthLayout({
children
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<>
<Suspense fallback={<AuthLayoutFallback />}>
<AuthSessionCheck />
</Suspense>
<div className="flex h-full w-full items-center justify-center">
{children}
</div>
</>
);
}
async function AuthSessionCheck() {
const session = await auth.api.getSession({
headers: await headers()
});
if (session) {
redirect("/");
}
return null;
}
function AuthLayoutFallback() {
return <div className="flex h-full w-full items-center justify-center" />;
} |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Summary
Issue:
Intermittent Hydration failed error that appears only after one or multiple 'F5' or 'Crlt + Shift + r' reloads (non-deterministic timing - sometimes 2 reloads, sometimes 10+).
Error occurs exclusively in (auth)/... route, never in (routes)/....
Environment:
Next.js 16.1.6 (Turbopack)
React 19
Better Auth for authentication
Tests Performed (all unsuccessful):
✅ Removed ThemeProvider completely → error persists
✅ Removed Toaster → error persists
✅ Removed DynamicThemeToggle → error persists
✅ Added suppressHydrationWarning to all elements → error persists
✅ Tested in Incognito mode (no extensions) → error persists
✅ Removed enableSystem from theme → error persists
✅ Commented out redirect logic → error persists
✅ Added AuthProvider wrapper → error persists
Specific Observations:
Intermittent nature suggests non-deterministic factor
Tested in Incognito mode (no extensions)
Only happens after one or multiple (10+) page reloads
Only in (auth), never in (routes)
Trace indicates server-side Suspense boundary mismatch
Question:
Is this a known Next.js 16 issue with Turbopack and server components, or a subtle hydration timing issue I'm missing?
Component with error:
src/app/(pages)/(auth)/layout.tsxScreen.Recording.2026-02-05.111638.mp4
Additional information
Error Trace Shows:
...
<InnerScrollAndFocusHandler segmentPath={[...]} focusAndScrollRef={{apply:false, ...}}>
<HTTPAccessFallbackBoundary notFound={} forbidden={undefined} unauthorized={undefined}>
<HTTPAccessFallbackErrorBoundary pathname="/sign-in" notFound={} forbidden={undefined} ...>
<InnerLayoutRouter url="/sign-in" tree={[...]} params={{}} cacheNode={{...}} segmentPath={[...]} ...>
Example
No response
All reactions