Always try-catch optional auth in public pages
When calling getAuthSession() in a server component that serves both authenticated and anonymous users, the auth library may throw if there are no valid cookies — not just return null.
// Bad: crashes for anonymous visitors
const session = await getAuthSession();
// Good: graceful fallback
let isAuthenticated = false;
try {
const session = await getAuthSession();
isAuthenticated = !!session?.user?.id;
} catch {
// Anonymous visitor — no session
}Wrap optional auth calls in try-catch. A thrown error in a server component crashes the entire page with a 500.
Learned from: Shared note page was returning 500 for all anonymous visitors after adding auth detection.