📖 Scenario: You are building a simple Next.js app that shows a list of books. You want to use server components to keep the client bundle small and fast.
🎯 Goal: Create a Next.js app with a server component that fetches and displays a list of books. Ensure the component does not add any client-side JavaScript bundle.
📋 What You'll Learn
Create a server component named BooksList that fetches book data.
Use a constant array books with exact book titles and authors.
Render the list of books inside a semantic <section> with a heading.
Export the component as default and ensure it is a server component by using the export const dynamic = 'force-static' directive.
💡 Why This Matters
🌍 Real World
Server components help keep your Next.js app fast by sending only HTML to the client, reducing JavaScript bundle size and improving load times.
💼 Career
Understanding server components and zero bundle size is important for building scalable, high-performance web apps in modern React and Next.js development.
Progress0 / 4 steps
1
Create the books data array
Create a constant array called books with these exact objects: { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald' }, { title: '1984', author: 'George Orwell' }, and { title: 'To Kill a Mockingbird', author: 'Harper Lee' }.
NextJS
Hint
Use const books = [ ... ] with exact objects inside the array.
2
Add a dynamic export to enforce static rendering
Add the line export const dynamic = 'force-static' below the books array to ensure the component is treated as a server component with zero client bundle.
NextJS
Hint
Write exactly export const dynamic = 'force-static' to mark the component as static.
3
Create the BooksList server component
Create a default exported async function component named BooksList that returns a <section> containing an <h2> with text Book List and an unordered list <ul> with each book's title and author in a <li>. Use books.map with book as the iterator variable.
NextJS
Hint
Use export default async function BooksList() and return JSX with a section, heading, and list using books.map.
4
Complete the component with accessibility and export
Add an aria-label attribute with value List of books to the <ul> element for accessibility. Confirm the component is exported as default and uses the dynamic export.
NextJS
Hint
Add aria-label="List of books" to the <ul> for screen readers.
Practice
(1/5)
1. What is the main benefit of using server components in Next.js with zero bundle size?
easy
A. They send no JavaScript to the browser, reducing bundle size and improving load speed.
B. They allow client-side interactivity without any server code.
C. They automatically cache data on the client for offline use.
D. They enable real-time updates without refreshing the page.
Solution
Step 1: Understand server components behavior
Server components run only on the server and do not send JavaScript to the browser.
Step 2: Identify the benefit of zero bundle size
Because no JavaScript is sent, the bundle size is smaller, which improves page load speed.
Final Answer:
They send no JavaScript to the browser, reducing bundle size and improving load speed. -> Option A
Quick Check:
Zero bundle size = faster load [OK]
Hint: Server components send zero JS to browser, so bundle size shrinks [OK]
Common Mistakes:
Thinking server components run on the client
Assuming server components add client interactivity
Confusing caching with zero bundle size
2. Which of the following is the correct way to declare a server component in Next.js to ensure zero bundle size?
easy
A. "use client"; export default function MyComponent() { return <div>Hello</div>; }
B. "use server"; export default function MyComponent() { return <div>Hello</div>; }
C. export default function MyComponent() { return <div>Hello</div>; }
D. import React from 'react'; export default function MyComponent() { return <div>Hello</div>; }
Solution
Step 1: Identify server component syntax
By default, components without "use client" directive are server components in Next.js.
Step 2: Check the options for client/server directives
export default function MyComponent() { return <div>Hello</div>; } has no "use client" directive, so it is a server component with zero bundle size.
Final Answer:
export default function MyComponent() { return <div>Hello</div>; } -> Option C
Quick Check:
No "use client" means server component [OK]
Hint: No "use client" means server component with zero bundle size [OK]
Common Mistakes:
Adding "use client" which makes it a client component
5. You want to build a Next.js page that fetches user data from a database and displays it without sending any JavaScript to the browser. Which approach ensures zero bundle size for this page?
hard
A. Fetch data in getStaticProps and pass it to a client component.
B. Create a client component with "use client" that fetches data using useEffect and displays it.
C. Create a server component but add "use client" to enable interactivity.
D. Create a server component that fetches data inside and returns JSX, without "use client" directive.
Solution
Step 1: Identify zero bundle size requirement
Zero bundle size means no JavaScript sent to browser, so no client components.
Step 2: Choose correct data fetching method
Fetching data inside a server component without "use client" sends only HTML with data, no JS bundle.
Step 3: Evaluate other options
Client components or getStaticProps with client components send JS bundles, so they don't meet zero bundle size.
Final Answer:
Create a server component that fetches data inside and returns JSX, without "use client" directive. -> Option D
Quick Check:
Server component + data fetch = zero bundle size [OK]
Hint: Use server component with data fetch, no "use client" [OK]
Common Mistakes:
Adding "use client" which sends JS bundle
Using client-side data fetching hooks
Assuming getStaticProps alone ensures zero bundle size
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.