Your admin section was crashing with error: "Application error: a server-side exception has occurred (Digest: 1336551846)"
This happened because the code had a fundamental architectural flaw that violated Next.js 13+ best practices.
Imagine a building where the front door (a client) is supposed to open to an internal secure room (server). But the architect made the front door itself control the building's security system - that's impossible because the door doesn't have access to the building's security infrastructure.
That's what was happening:
- The root admin layout was marked as "use client" (a client-side component)
- Inside it, there were server components (like the login page) that needed server-side access
- JavaScript couldn't serialize these server-side operations through a client boundary
- Result: Cryptic Server Components rendering error
- File:
app/admin/layout.tsx - Problem: Marked as
"use client"but wrapping server components - Fix: Converted to server component - simple wrapper only
- File:
lib/auth.ts - Problem: Exception thrown when Supabase unconfigured
- Fix: Added try-catch to gracefully return null on error
- File:
app/api/admin/session/route.ts - Problem: API endpoint crashed on auth failure
- Fix: Added try-catch to return proper error response
- Removed:
"use client"directive - Removed:
useState,useEffect,useRouter,usePathname - Removed: 140+ lines of client-side auth management
- Added: Simple server-side wrapper
- Result: -140 lines, much simpler, architecturally correct
- New client-side component for dashboard UI only
- Receives user data as props
- Renders sidebar, topbar, content
- Result: Proper client boundary
- Added try-catch for error handling
- Uses new
DashboardLayoutcomponent - Maintains server-side auth checks
- Result: More robust, proper error handling
- Added try-catch wrapper in
getAdminSession() - Catches and logs errors gracefully
- Returns null instead of throwing
- Result: No more unhandled exceptions
- Added try-catch wrapper in POST handler
- Logs errors for debugging
- Returns proper error response
- Result: Handles failures gracefully
"use client" ROOT LAYOUT
├─ manages auth state with useState/useEffect
├─ wraps server components (login page)
└─ tries to initialize Supabase on client
↓
CRASH: Can't serialize server code through client boundary
SERVER ROOT LAYOUT (no "use client")
├─ Simple wrapper
├─ Wraps in providers
└─ Passes children through
↓
LOGIN ROUTE: renders login form (client)
↓
DASHBOARD ROUTE: server layout checks auth, uses DashboardLayout (client)
├─ Server: getAdminSession() with error handling
└─ Client: DashboardLayout renders UI
Go to Netlify Dashboard → Your Site → Site Settings → Build & Deploy → Environment
Ensure these 5 variables are SET WITH VALUES:
✓ NEXT_PUBLIC_SUPABASE_URL
✓ SUPABASE_URL
✓ NEXT_PUBLIC_SUPABASE_ANON_KEY
✓ SUPABASE_ANON_KEY
✓ ADMIN_EMAILS
If any are missing:
- Get values from your Supabase project
- Add them in Netlify environment variables
- Netlify will auto-redeploy
git add .
git commit -m "Fix admin section Server Components errors"
git push origin mainThat's it! Netlify will automatically deploy.
- Clear browser cache (Ctrl+Shift+Del)
- Visit
https://your-domain.com/admin/login - You should see login form (no errors)
- Try logging in with your admin email
- You should see dashboard
- Check browser console - should be clean
✅ Can visit /admin/login without errors
✅ Login form displays
✅ Can type credentials
✅ Click "Sign in" submits
✅ After login, see /admin dashboard
✅ Sidebar and topbar visible
✅ No errors in browser console
✅ No "Application error" messages
"Supabase configuration required" message:
- Normal if env vars not set
- Action: Set the 5 environment variables in Netlify
Other errors:
- Check Netlify deployment logs
- Look for specific error messages
- Hard refresh browser (Ctrl+F5)
- Try incognito window
Still stuck:
- Check
ADMIN_SECTION_FIX_ANALYSIS.mdfor technical details - See
ADMIN_DEPLOYMENT_CHECKLIST.mdfor troubleshooting
| File | Status | What Changed |
|---|---|---|
app/admin/layout.tsx |
✏️ Modified | Client → Server component, removed auth logic |
app/admin/(dashboard)/layout.tsx |
✏️ Modified | Added error handling, uses new component |
lib/auth.ts |
✏️ Modified | Added try-catch in getAdminSession() |
app/api/admin/session/route.ts |
✏️ Modified | Added try-catch in POST handler |
components/admin/dashboard-layout.tsx |
✨ New | Client boundary for dashboard UI |
ADMIN_SECTION_FIX_ANALYSIS.md |
✨ New | Technical analysis (for reference) |
ADMIN_DEPLOYMENT_CHECKLIST.md |
✨ New | Step-by-step deployment guide |
ADMIN_QUICK_REFERENCE.md |
✨ New | Quick reference guide |
ADMIN_FIX_SUMMARY.md |
✨ New | Detailed summary |
- Server components for auth - Auth checks happen server-side where they're secure and have proper access
- Client components for UI - Sidebars, forms, and UI use React hooks in proper client boundary
- Clear separation - No trying to do server things in client context
- Error handling - Every potential failure point has try-catch and proper error messages
- Proper hydration - No conflicts between server code and client rendering
- Monitor admin usage - make sure it stays stable
- Keep deployment history in case rollback needed
- Consider adding 2FA if handling sensitive content
- Could add rate limiting on login endpoint
- Could add audit logging for admin actions
The Fix: Convert auth logic from client-side to server-side, maintain clear boundaries between server and client code, add error handling everywhere.
Result: Admin section now works, no more cryptic errors, better security, better architecture.
Effort: Just push the code, set env vars if needed, test 5 minutes. Done.
If something breaks after deployment:
- Netlify Dashboard → Deploys
- Find previous working deploy
- Click ... → Rollback to this deploy
- Takes ~30 seconds
- Site reverts to previous version
Ready to Deploy? → git push origin main → Done! 🚀
For more details, see:
ADMIN_QUICK_REFERENCE.md- Quick overviewADMIN_DEPLOYMENT_CHECKLIST.md- Detailed checklistADMIN_SECTION_FIX_ANALYSIS.md- Technical deep dive