Why Next.js and setup
A React SPA sends empty HTML, so the first load is slow and link previews show nothing. Next.js is a React framework that renders pages on the server and adds file-based routing, Server Components and backend features. This note covers why Next.js exists, setting it up and the basic file structure.
58.1 Problem: the empty SPA HTML
- This is the real HTML a Vite React SPA server sends: one empty div#root and a script tag.
- All content (products, headings, prices) appears only after the browser downloads and runs the JavaScript.
HTML
<!doctype html><html lang="en"> <head> <title>ShopKart</title> </head> <body> <div id="root"></div> <script type="module" src="/src/main.jsx"></script> </body></html>58.2 Why is it used?
- In an SPA the user waits for: empty HTML, then the whole JS bundle, then the data fetch.
- Google does run JavaScript to index pages, but it is slow and delayed.
- WhatsApp or LinkedIn link previews do not run JavaScript at all, so the page looks empty there.
- Step 1
1.Browser gets empty HTML (just div#root)
- Step 2
2.Downloads the whole JavaScript bundle
- Step 3
3.JS runs and fetches data (loading spinner)
- Step 4
4.UI finally shows: the user kept waiting
58.3 What Next.js adds to React
- React is a UI library; Next.js is a React framework that gives the structure for a whole app.
- Routes come automatically from folders (file-based routing).
- Pages are rendered on the server and arrive as ready HTML.
- Server Components reduce the JavaScript sent to the browser.
- Backend code (Route Handlers) in the same project, plus built-in image and font optimisation.
| Point | React SPA (Vite) | Next.js |
|---|---|---|
| Type | UI library + build tool | Full-stack React framework |
| First HTML | Empty div#root | Ready HTML with content |
| Routing | React Router config | Folders in app/ (file-based) |
| SEO / link previews | Weak | Good |
| Backend | Separate project | Route Handlers in the same project |
| Hosting | Any static host | Node server or a platform like Vercel for full features |
58.4 Setup: create-next-app
- Needs Node 20.9 or newer.
- npx create-next-app@latest shop asks one question: use the recommended defaults? Yes gives TypeScript, ESLint, Tailwind CSS and the App Router.
- npm run dev starts the Next.js 16 dev server on localhost:3000 with the Turbopack bundler.
Terminal
npx create-next-app@latest shopcd shopnpm run devOutput
$ npx create-next-app@latest shop? Would you like to use the recommended Next.js defaults?❯ Yes, use recommended defaults TypeScript, ESLint, Tailwind CSS, App Router, ...Success! Created shop $ npm run dev> shop@0.1.0 dev> next dev▲ Next.js 16.4.5 (Turbopack)- Local: http://localhost:3000✓ Ready in 926ms58.5 Project structure
- app/: all routes and pages live here; the two main files are layout.tsx and page.tsx.
- public/: static files like images.
- next.config.ts: Next settings; package.json already has dev, build and start scripts.
Output
shop/├── app/│ ├── favicon.ico│ ├── globals.css│ ├── layout.tsx│ └── page.tsx├── public/├── AGENTS.md├── eslint.config.mjs├── next-env.d.ts├── next.config.ts├── package.json└── tsconfig.json58.6 Root layout (app/layout.tsx)
- Every Next app needs a root layout: a normal React component with a children prop.
- html and body are written here because Next has no separate index.html file.
- Every page renders inside it in place of children, so a navbar or footer goes here.
React (TSX)
import "./globals.css"; export default function RootLayout({ children,}: { children: React.ReactNode;}) { return ( <html lang="en"> <body>{children}</body> </html> );}58.7 Home page (app/page.tsx)
- page.tsx is the UI of the home route (/); its default export is the page component.
- Saving the file updates the browser automatically.
React (TSX)
export default function Home() { return ( <main> <h1>ShopKart</h1> <p>Aaj ke best deals</p> </main> );}Output
# ShopKartAaj ke best deals58.8 Folder = route
- No React Router config: create app/about/page.tsx and /about is ready.
- This is called file-based routing.
React (TSX)
export default function About() { return <h1>About ShopKart</h1>;}Output
# About ShopKart58.9 Proof: the HTML has content
- Fetch the raw HTML of /about with curl and search for the h1: the heading is right in the HTML because Next rendered it on the server.
- The same test on the Vite SPA only finds the empty root div.
Terminal
curl -s localhost:3000/about \ | grep -o "<h1>.*</h1>"Output
<h1>About ShopKart</h1>- Step 1
1.Request: browser asks for /about
- Step 2
2.Server render: Next runs your React components on the server
- Step 3
3.Ready HTML: content shows in the browser immediately
- Step 4
4.Hydration: JavaScript loads and makes the HTML interactive
Hydration is covered in detail in the Server Components video
58.10 Checkpoint: which URLs exist?
- Only a page.tsx file makes a route public.
- /, /about and /products work; /products/ProductCard is a 404.
- Keeping helper files like ProductCard.tsx next to the page is safe: this is called colocation.
Output
app/├── layout.tsx├── page.tsx├── about/│ └── page.tsx└── products/ ├── page.tsx └── ProductCard.tsxOutput
GET / 200GET /about 200GET /products 200GET /products/ProductCard 40458.11 Common mistakes
- Creating app/contact.tsx for a contact page: /contact gives 404.
- Right way: a contact folder with page.tsx inside. The folder names the URL, the page file is its UI.
- Thinking you must leave React behind for Next.js: it is the same React inside; components, props, state and hooks work the same.
Output
# Wrong: /contact gives 404app/├── layout.tsx├── page.tsx└── contact.tsxOutput
# Right: /contact worksapp/├── layout.tsx├── page.tsx└── contact/ └── page.tsx58.12 When to use which
- Public pages where SEO and a fast first load matter (e-commerce, blog, landing page): Next.js.
- Frontend and backend in one project: Next.js.
- Dashboard or internal tool behind a login: a Vite SPA is simpler and runs on any static hosting.
- Remember: full Next.js features need a Node server or a platform like Vercel.
58.13 Interview tips
- React is a library for building UI; Next.js is a full-stack framework built on React.
- Next gives file-based routing, server rendering (SSR/SSG), Server Components and backend features like Route Handlers out of the box.
- SPA: the server sends empty HTML; Next: ready HTML, so SEO and first load are better.
- In the App Router, only page.tsx makes a route public; a folder gives the URL segment.