Server vs Client Component (5 min)
Given an app/profile/page.tsx screen with a button and local state, decide which file needs 'use client' and explain why in 2-3 lines.
Frontend / Knowledge library
react framework for full-stack web apps
Next.js removes repetitive React infrastructure work by combining routing, rendering modes (SSR/SSG/ISR), backend endpoints, and performance defaults (images, fonts, code splitting) into one production-ready framework.
2016: Released by ZEIT (now Vercel) to simplify server-rendered React apps. 2019–2020: Static generation and Incremental Static Regeneration (ISR) became core capabilities. 2021: Next.js 12 introduced the SWC compiler and Edge Middleware. 2022: Next.js 13 introduced the App Router and React Server Components model. 2023+: App Router matured with stronger streaming, caching, and server-action workflows.
Work through these exercises in order. Try the task before opening a reference.
Given an app/profile/page.tsx screen with a button and local state, decide which file needs 'use client' and explain why in 2-3 lines.
Create app/posts/[slug]/page.tsx that fetches a post by slug. If fetch fails, render notFound(). Keep UI to title + short body preview.
Design a page that serves cached content but refreshes every 60 seconds. Briefly justify when ISR is better than full SSR for this case.
Build app/api/notes/route.ts with GET and POST. Return JSON, validate required fields, and use proper HTTP status codes.
You observe slow first load and large JS bundle. List 3 Next.js-specific fixes and map each fix to a measurable metric (LCP, TTFB, JS KB).
A regional logistics firm managing distributed warehouse inventory across multiple time zones.
The firm struggles with high latency and inconsistent data states when using separate frontend and backend services, leading to inventory synchronization errors during peak shipping hours.
By adopting the Next.js App Router, the firm can unify their frontend and backend logic into a single full-stack application, utilizing React Server Components to fetch inventory data directly on the server before rendering, thereby reducing client-side processing overhead.
This diagram illustrates how the App Router centralizes inventory requests.
User interface requesting inventory data
Executes Server Components and fetches data
Source of truth for stock levels
This code demonstrates the App Router capability of fetching data directly within a component, mapping to the solution of unifying frontend and backend logic.
// Example of a Server Component fetching data directly
async function InventoryList() {
// Fetching data on the server avoids client-side API waterfalls
const data = await fetch('https://api.logistics.internal/stock');
const inventory = await data.json();
return <ul>{inventory.map(item => <li key={item.id}>{item.name}</li>)}</ul>;
}A more cohesive development workflow and improved data consistency, as the server-side logic eliminates the need for complex client-side state synchronization between disparate services.
The primary trade-off is the increased complexity of managing server-side execution environments; developers must monitor server logs and resource utilization to ensure that heavy data fetching does not block the main event loop.