DEV Community

Vaibhav Bhatia
Vaibhav Bhatia

Posted on

Understanding Server Components

Server Components: The New Default

Server Components are the default in Next.js 13+ and represent a fundamental shift in how we write React code. They run exclusively on the server and help reduce the JavaScript bundle sent to the client.

Key Benefits of Server Components

  1. Reduced Bundle Size: Since they run on the server, their code is never sent to the client 2.** Direct Backend Access**: Can directly query databases and access backend resources
  2. Improved Initial Page Load: No client-side JavaScript overhead
  3. Better Security: Sensitive data remains on the server

Example of a Server Component

// app/users/page.js
async function UsersPage() {
  // Direct database query - only possible in server components
  const users = await db.query('SELECT * FROM users');

  return (
    <div className="p-4">
      <h1>Users List</h1>
      {users.map(user => (
        <div key={user.id} className="mb-2">
          {user.name} - {user.email}
        </div>
      ))}
    </div>
  );
}

export default UsersPage;
Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more