Introduction to Next.js (server-side React)
In this page:
Why Use a Framework on Top of React
Plain React (via Vite or Create React App) only handles the component-rendering layer — you still need to add routing, data fetching patterns, and server-rendering yourself if needed. Next.js bundles all of this together with sensible defaults, letting you build full production apps faster.
Note: If your app needs SEO-friendly server-rendered pages, or you don't want to hand-configure routing/bundling yourself, Next.js is a common default choice.
Warning: Next.js is a real npm-installed framework with its own build process — it can't run inside this CDN-only browser sandbox.
Example: Why Use a Framework on Top of React
// Run in your local React project (npm install required)
// npx create-next-app@latest my-app
// cd my-app && npm run dev
// app/page.js
export default function HomePage() {
return <h1>Welcome to my Next.js app</h1>;
}
File-Based Routing
Instead of manually defining routes (as with React Router), Next.js's App Router determines a page's URL directly from its file location: app/page.js is the homepage, app/about/page.js becomes /about, and so on — no separate route configuration file needed.
Note: Nested folders naturally create nested routes — app/blog/[slug]/page.js creates a dynamic route like /blog/hello-world.
Warning: Unlike React Router where routes are explicit JavaScript, forgetting to name a file exactly 'page.js' (or page.tsx) inside a folder means that folder doesn't become a route at all.
Example: File-Based Routing
// Run in your local React project (npm install required)
// File structure -> resulting routes:
// app/page.js -> /
// app/about/page.js -> /about
// app/blog/[slug]/page.js -> /blog/:slug (dynamic)
// app/about/page.js
export default function AboutPage() {
return <p>About us</p>;
}
Server-Side Rendering by Default
Pages in Next.js's App Router are Server Components by default, meaning they render on the server and send finished output to the browser — good for SEO and fast initial loads. Interactivity is added selectively via 'use client' components nested inside.
Note: This default (server-first, add client interactivity where needed) is the opposite of a plain React SPA, which is entirely client-rendered by default.
Warning: Directly using browser-only APIs (like localStorage or window) in a Server Component throws an error, since that code never actually runs in a browser.
Example: Server-Side Rendering by Default
// Run in your local React project (npm install required)
// app/page.js -- runs on the server, can directly await data
async function getData() {
return { title: 'Server-rendered title' };
}
export default async function Page() {
const data = await getData();
return <h1>{data.title}</h1>;
}
- Treating Next.js as just 'React with extra CSS' — it fundamentally changes rendering (server-side by default) and routing (file-based) compared to a plain client-side React app.
- Putting client-only code (like directly accessing window) in a Server Component without a 'use client' directive.
- Not understanding file-based routing — a file's location under the app/ or pages/ folder directly determines its URL.
- Next.js is a popular React framework adding server-side rendering, file-based routing, and more, on top of React.
- The App Router organizes pages by folder structure under an 'app/' directory; each folder can have a page.js file.
- Server Components are the default in the App Router; add 'use client' for interactive pieces.
- Next.js handles much of the build/deployment tooling (bundling, routing, SSR) that a plain React app would need configured separately.
Requires npm install next react react-dom (typically via create-next-app) — not available via CDN in this sandbox.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Error Boundaries
- React Portals
- Modals using Portals (practical use)
- React Suspense
- Code Splitting with React.lazy
- Introduction to Server Components
- Introduction to Next.js (server-side React)
- Using React with TypeScript
- Scalable Folder Architecture
- Common React Design Patterns
- Component Documentation with Storybook
- Accessibility (a11y) in React
- i18n with react-i18next
- React Security Best Practices