Performance: Why authentication matters
MEDIUM IMPACT
Authentication affects initial page load speed and interaction responsiveness by controlling access to content and resources.
import { cookies } from 'next/headers'; export default async function Page() { const cookieStore = cookies(); const token = cookieStore.get('authToken')?.value; if (!token) { return <div>Please login</div>; } const response = await fetch('http://localhost:3000/api/user', { headers: { Authorization: `Bearer ${token}`, }, }); const user = await response.json(); return <div>Welcome {user.name}</div>; }
'use client'; import { useState, useEffect } from 'react'; export default function Page() { const [user, setUser] = useState(null); useEffect(() => { fetch('/api/user').then(res => res.json()).then(setUser); }, []); if (!user) { return <div>Please login</div>; } return <div>Welcome {user.name}</div>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side auth fetch | More DOM nodes due to delayed rendering | Multiple reflows as content updates | Higher paint cost due to delayed content | [X] Bad |
| Server-side auth check | Minimal DOM nodes initially | Single reflow on initial render | Lower paint cost with immediate content | [OK] Good |