Accessing Request Context (Cookies, Headers)
You'll learn how server components can read request-specific data like cookies and headers directly.
The Server-Side Advantage
Because server components run on the server during the request, they have access to the full HTTP request context—something client components never see. Your framework (like Next.js) provides special functions to read cookies, headers, and other request metadata right inside your async component.
Think of it like being a restaurant server who can see the customer's order ticket directly, versus being the kitchen chef who only receives what the server tells them. Server components are the "server" with direct access to the request details.
Reading Request Data
Inside a server component, you can use framework-provided functions like cookies() or headers() to access request-specific information:
// Server Component
async function UserGreeting() {
const cookieStore = cookies();
const theme = cookieStore.get('theme')?.value || 'light';
const headersList = headers();
const userAgent = headersList.get('user-agent');
return (
<div className={theme}>
<p>Welcome! Theme: {theme}</p>
<p>Browser: {userAgent}</p>
</div>
);
}
This component reads the user's theme preference from cookies and their browser info from headers—all without any client-side JavaScript or useEffect waterfalls.
Why This Matters
This pattern eliminates the traditional client-side approach of checking cookies after hydration or making API calls just to access request data. The information is available immediately during rendering, making personalized server-rendered content trivial.
Key Takeaway: Server components can directly access cookies, headers, and request metadata through framework functions, enabling instant personalization without client-side JavaScript overhead.