Skip to main content

Command Palette

Search for a command to run...

Next.js Explained

Published
β€’4 min readβ€’View as Markdown
Next.js Explained
A

I’m Arjun Saxena, a passionate software developer specializing in web engineering. I believe in writing code that creates real solutions to real problems. I love building efficient, user-friendly applications and constantly push myself to learn new technologies. Beyond coding, I enjoy sharing knowledge and growing together with others in the tech community.

Modern React Framework for Real-World Applications (Beginner Friendly)

React is great for building UI, but Next.js takes React to production level.
It solves common problems like SEO, performance, routing, and backend APIs β€” all in one place.

In this blog, you’ll learn:

  • React vs Next.js (simple comparison)

  • SSG vs SSR (when to use what)

  • Dynamic routing & file-based navigation

  • API routes in Next.js

  • Server Actions (Next.js modern feature)
    All explained in a very easy, example-based way.

React vs Next.js (Simple Difference)

Think of It Like This 🧠

  • React β†’ UI library (building blocks)

  • Next.js β†’ Full-featured framework (house + plumbing + electricity)

Key Differences

FeatureReactNext.js
RoutingManual (React Router)File-based
SEONeeds extra workBuilt-in
RenderingClient-side onlySSG, SSR, ISR
BackendSeparate serverBuilt-in API routes
PerformanceGoodExcellent

πŸ“Œ Next.js is built on top of React, not a replacement.

Rendering in Next.js (Big Concept)

Before showing pages, Next.js decides where and when HTML is generated.

Two most important strategies:

  • SSG – Static Site Generation

  • SSR – Server-Side Rendering

SSG vs SSR (Very Important)

https://delicate-dawn-ac25646e6d.media.strapiapp.com/performance_needs_b4458abf31.png

https://archive.smashing.media/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/94ac796d-f3fd-4984-8d62-5e3ca2134f13/ssg-ssr-08-ssr.png

https://miro.medium.com/v2/resize%3Afit%3A1400/1%2ADNVhQd11_ytjbS2qP_Bv6w.jpeg

SSG – Static Site Generation πŸ—οΈ

What It Means

  • HTML is generated at build time

  • Same content for every user

Example: Blog Website πŸ“

/blog/how-nextjs-works
  • Content doesn’t change often

  • Very fast loading

  • Perfect for SEO

Why Use SSG?

βœ” Super fast
βœ” Cheap hosting
βœ” Best for blogs, docs, landing pages

SSR – Server-Side Rendering βš™οΈ

What It Means

  • HTML is generated on every request

  • Content can be different per user

Example: Product Page πŸ›’

/product/iphone-15
  • Prices, stock, offers change

  • Needs fresh data

Why Use SSR?

βœ” Always up-to-date data
βœ” Good SEO
βœ” Personalized pages

One-Line Rule

Static content β†’ SSG | Dynamic content β†’ SSR

File-Based Routing in Next.js πŸ“‚

No More Router Configuration

In Next.js, folder = route.

https://forums.meteor.com/uploads/default/original/3X/3/e/3e397f076eef91c6df1fca2dd16ee1952c21543b.jpeg

Example Folder Structure

app/
 β”œβ”€ page.js           β†’ /
 β”œβ”€ blog/
 β”‚   └─ page.js       β†’ /blog
 β”œβ”€ about/
 β”‚   └─ page.js       β†’ /about

βœ” No router setup
βœ” Clean & predictable

Dynamic Routing in Next.js πŸ”

Used when URL changes based on data.

Example: Blog Posts

app/blog/[slug]/page.js

Now URLs like:

/blog/what-is-nextjs
/blog/ssg-vs-ssr

Accessing Dynamic Params

export default function BlogPage({ params }) {
  return <h1>{params.slug}</h1>;
}

πŸ“Œ Perfect for:

  • Blog posts

  • Product pages

  • User profiles

API Routes in Next.js πŸ”Œ

Backend Inside Frontend

Next.js allows you to create backend APIs inside the same project.

https://nextjs.org/_next/image?q=75&url=https%3A%2F%2Fh8DxKfmAPhn8O0p3.public.blob.vercel-storage.com%2Fdocs%2Flight%2Fauthentication-overview.png&w=3840

https://cdn.hashnode.com/res/hashnode/image/upload/v1741931970605/07259597-c220-4f50-b4d3-3a0407489369.png

Example API Route

app/api/users/route.js
export async function GET() {
  return Response.json({ users: ["Arjun", "Rahul"] });
}

Now this API is available at:

/api/users

βœ” No Express needed
βœ” Full-stack in one repo

Real-World Use of API Routes

  • Login & signup

  • Fetch data from database

  • Payment handling

  • Form submissions

Server Actions ⚑

Modern Way to Handle Server Logic

Server Actions allow you to:

Run server-side code directly from components

No need to:

  • Create separate API routes

  • Write fetch calls manually

Simple Server Action Example

"use server";

export async function addUser(formData) {
  const name = formData.get("name");
  // save to database
}

Using Server Action in Component

<form action={addUser}>
  <input name="name" />
  <button>Add</button>
</form>

βœ” Cleaner
βœ” More secure
βœ” Less boilerplate

When to Use Server Actions?

βœ” Form submissions
βœ” Database operations
βœ” Auth logic
βœ” Server-only tasks

Typical Next.js Project Structure πŸ—οΈ

https://h8dxkfmaphn8o0p3.public.blob.vercel-storage.com/docs/dark/nested-file-conventions-component-hierarchy.png

https://images.prismic.io/prismic-main/65257576fbd9a45bcec81165_App_Directory_Diagram.png?auto=format%2Ccompress

app/
 β”œβ”€ page.js
 β”œβ”€ layout.js
 β”œβ”€ api/
 β”œβ”€ blog/
 └─ components/

When Should You Use Next.js?

Use Next.js if:

  • SEO matters

  • Performance matters

  • You want full-stack in one app

  • You’re building real-world products