Performance: Session management
MEDIUM IMPACT
Session management affects page load speed and interaction responsiveness by controlling how user data is stored and accessed during navigation.
import { getServerSession } from 'next-auth/next'; import { authOptions } from './authOptions'; export default async function handler(req, res) { const session = await getServerSession(req, res, authOptions); if (!session) { res.status(401).json({ error: 'Unauthorized' }); return; } // minimal async processing res.status(200).json({ user: session.user }); }
import { getSession } from 'next-auth/react'; export default async function handler(req, res) { const session = await getSession({ req }); if (!session) { res.status(401).json({ error: 'Unauthorized' }); return; } // heavy synchronous processing here res.status(200).json({ user: session.user }); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous session fetch with heavy processing | Minimal | Multiple due to blocking | High due to delayed paint | [X] Bad |
| Async session fetch with minimal processing | Minimal | Single or none | Low, smooth paint | [OK] Good |
| Immediate localStorage writes on session change | None | Multiple reflows | Medium due to blocking | [X] Bad |
| Deferred localStorage writes using requestIdleCallback | None | Single or none | Low, non-blocking | [OK] Good |