beginner

Server Components example 7

A focused Next.js example for server components with output and explanation.

Server Components example 7
lesson.js
1
2
3
4
5
6
7
8
javascript8 linesWrap
Input

Terminal

Success

Ready.

Run code to see output here.

What this example teaches

Server Components

Output

NextJS pre-renders page static HTML during build or dynamic JSON on runtime fetch.

Line-by-line explanation

  • Line 1 sets up the Server Components example: // NextJS Server Component.
  • Line 2 adds one required part of the working pattern: import { SITE_URL } from "@/lib/constants";.
  • Line 3 adds one required part of the working pattern: blank line.
  • Line 4 adds one required part of the working pattern: export default async function Page() {.
  • Line 5 adds one required part of the working pattern: const res = await fetch(`${SITE_URL}/api/orders`);.
  • Line 6 adds one required part of the working pattern: const orders = await res.json();.

Why this example is useful

This example is useful because it isolates server components without surrounding noise, so you can see the idea clearly.

Where it is used in real projects

Server Components appears in real Next.js work when a feature needs a clear pattern that can be reviewed and changed safely.

Beginner variation

Change one label, value or condition in the Server Components example and run it again.

Advanced variation

Combine Server Components with validation, error handling or reusable structure.