Bird
Raised Fist0
NextJSframework~8 mins

When to keep components on server in NextJS - Performance & Optimization

Choose your learning style10 modes available
Performance: When to keep components on server
HIGH IMPACT
This affects the page load speed and interaction responsiveness by controlling what renders on the server versus the client.
Rendering a data-heavy component in Next.js
NextJS
export const dynamic = 'force-static';
export default async function DataComponent() {
  const res = await fetch('https://example.com/api/data');
  const data = await res.json();
  return <div>{data.content}</div>;
}
Fetching and rendering on server sends ready HTML to client, reducing JS bundle and speeding up LCP.
📈 Performance GainImproves LCP by 300-500ms; reduces client JS bundle by 50-100kb.
Rendering a data-heavy component in Next.js
NextJS
import React from 'react';

export default function DataComponent() {
  const [data, setData] = React.useState(null);
  React.useEffect(() => {
    fetch('/api/data').then(res => res.json()).then(setData);
  }, []);
  if (!data) return <p>Loading...</p>;
  return <div>{data.content}</div>;
}
Fetching data and rendering on client causes larger JS bundle and delays content display until JS loads and runs.
📉 Performance CostBlocks LCP until JS is downloaded and executed; adds 50-100kb to client bundle.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Client-side data fetching componentHigh (many nodes created after JS loads)Multiple reflows during hydrationHigh paint cost due to delayed content[X] Bad
Server-rendered component with dataLow (HTML sent ready)Single reflow on initial paintLow paint cost, fast content display[OK] Good
Rendering Pipeline
Server components are rendered to HTML on the server, then sent to the browser. This skips client JS execution for those parts, reducing layout and paint delays.
→HTML Generation
→Network Transfer
→Client JS Execution
→Paint
⚠️ BottleneckClient JS Execution and Hydration
Core Web Vital Affected
LCP
This affects the page load speed and interaction responsiveness by controlling what renders on the server versus the client.
Optimization Tips
1Render data-fetching components on the server to reduce client JS bundle size.
2Use server components to speed up Largest Contentful Paint (LCP).
3Avoid client-side fetching for initial content to prevent delayed rendering.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance benefit of keeping components on the server in Next.js?
AFaster initial content display by reducing client JS bundle
BMore client-side interactivity by loading all JS upfront
CIncreased bundle size for better caching
DLonger hydration time for better SEO
DevTools: Performance
How to check: Record page load and look for long JS execution and hydration phases; check when main content appears.
What to look for: Short JS execution time and early content paint indicate good server component usage.

Practice

(1/5)
1. Which of the following is the best reason to keep a component as a server component in Next.js?
easy
A. It requires frequent updates based on user interaction.
B. It needs to handle user clicks and input events.
C. It fetches data securely and renders static content.
D. It uses browser-only APIs like localStorage.

Solution

  1. Step 1: Understand server component purpose

    Server components run on the server and are ideal for static content and secure data fetching.
  2. Step 2: Identify interactive needs

    Components that handle user clicks or browser APIs must be client components, not server components.
  3. Final Answer:

    It fetches data securely and renders static content. -> Option C
  4. Quick Check:

    Server components = static + secure data [OK]
Hint: Server components = static or secure data only [OK]
Common Mistakes:
  • Choosing server component for interactive UI
  • Using browser APIs in server components
  • Thinking server components handle events
2. Which syntax correctly forces static rendering for a server component in Next.js 14+?
easy
A. export const dynamic = 'force-static';
B. export const runtime = 'edge';
C. export const dynamic = 'force-dynamic';
D. export const client = true;

Solution

  1. Step 1: Recall server component syntax

    Server components can be forced static with export const dynamic = 'force-static';.
  2. Step 2: Differentiate other options

    export const dynamic = 'force-dynamic'; (dynamic = 'force-dynamic') forces dynamic rendering, B (runtime = 'edge') sets edge runtime without forcing static, D is invalid syntax.
  3. Final Answer:

    export const dynamic = 'force-static'; -> Option A
  4. Quick Check:

    Force static = server component [OK]
Hint: Use 'force-static' to keep component server-only [OK]
Common Mistakes:
  • Confusing client directive with server component
  • Using 'force-dynamic' for server-only components
  • Mixing runtime and dynamic exports
3. Consider this Next.js server component code snippet:
export default function Greeting() {
  const name = fetchNameFromDB();
  return <h1>Hello, {name}!</h1>;
}

What will be the output rendered in the browser?
medium
A. <h1>Hello, [name from DB]!</h1>
B. Error: fetchNameFromDB is not defined
C. <h1>Hello, undefined!</h1>
D. Nothing renders because server components cannot fetch data

Solution

  1. Step 1: Understand server component data fetching

    Server components can fetch data directly on the server before rendering.
  2. Step 2: Analyze the code behavior

    The function fetchNameFromDB() is not defined in the snippet, so this will cause a runtime error.
  3. Final Answer:

    Error: fetchNameFromDB is not defined -> Option B
  4. Quick Check:

    Undefined functions cause errors [OK]
Hint: Undefined functions cause errors in server components [OK]
Common Mistakes:
  • Assuming fetchNameFromDB is defined
  • Thinking server components can't fetch data
  • Confusing client-side data fetching with server
4. You have a Next.js component that fetches user data and also handles a button click event. You mistakenly keep it as a server component. What error or issue will you face?
medium
A. The component will render twice causing performance issues.
B. The component will throw a syntax error at build time.
C. The data fetching will fail because server components cannot fetch data.
D. The button click event will not work because server components cannot handle client events.

Solution

  1. Step 1: Understand event handling in Next.js components

    Only client components can handle browser events like button clicks.
  2. Step 2: Identify the problem with server components

    Server components run on the server and cannot respond to client-side events, so the button click won't work.
  3. Final Answer:

    The button click event will not work because server components cannot handle client events. -> Option D
  4. Quick Check:

    Server components = no client events [OK]
Hint: Client events need client components [OK]
Common Mistakes:
  • Expecting server components to handle clicks
  • Thinking server components cause syntax errors
  • Confusing data fetching ability
5. You want to build a Next.js page that shows a list of blog posts fetched from a database and includes a search box that filters posts as the user types. How should you split components between server and client?
hard
A. Keep the blog list as a server component and the search box as a client component.
B. Make both blog list and search box client components for interactivity.
C. Make both blog list and search box server components for performance.
D. Keep the search box server component and blog list client component.

Solution

  1. Step 1: Identify static and interactive parts

    The blog list is static content fetched from the database, suitable for server components.
  2. Step 2: Identify interactive UI needs

    The search box needs to respond to user typing, so it must be a client component.
  3. Step 3: Combine components properly

    Use server component for data fetching and rendering list, client component for search input and filtering.
  4. Final Answer:

    Keep the blog list as a server component and the search box as a client component. -> Option A
  5. Quick Check:

    Static data = server, interactive UI = client [OK]
Hint: Static content server, interactive UI client [OK]
Common Mistakes:
  • Making everything client component unnecessarily
  • Trying to handle user input in server component
  • Mixing up which part fetches data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard