SkillPilotAI

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.
How an SPA loads the first time
  1. 1.Browser gets empty HTML (just div#root)

  2. 2.Downloads the whole JavaScript bundle

  3. 3.JS runs and fetches data (loading spinner)

  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.
PointReact SPA (Vite)Next.js
TypeUI library + build toolFull-stack React framework
First HTMLEmpty div#rootReady HTML with content
RoutingReact Router configFolders in app/ (file-based)
SEO / link previewsWeakGood
BackendSeparate projectRoute Handlers in the same project
HostingAny static hostNode 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 dev
Output
$ 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 926ms

58.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.json

58.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 deals

58.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 ShopKart

58.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>
How a page arrives in Next.js
  1. 1.Request: browser asks for /about

  2. 2.Server render: Next runs your React components on the server

  3. 3.Ready HTML: content shows in the browser immediately

  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.tsx
Output
GET / 200GET /about 200GET /products 200GET /products/ProductCard 404

58.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.tsx
Output
# Right: /contact worksapp/├── layout.tsx├── page.tsx└── contact/    └── page.tsx

58.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.