Kazi Rahamatullah
Kazi Rahamatullah
AboutProjectsBlogContact
UsesBooks
ResumeView CV
Kazi Rahamatullah

© Copyright 2026 Kazi Rahamatullah

AboutProjectsBlogBooksUses
Twitter/XGitHubProduct HuntCodeSandbox
Back to Blog
MERNNext.jsSSRISRAPI RoutesSoftware

MERN Course: Next.js Essentials

Weeks 14–16 of the MERN syllabus — App Router, SSR, data fetching, API routes, dynamic routing, next/image, auth, and full-stack project deployment.

Jul 9, 20266 min read

Introduction

Next.js is how many MERN teams ship React in production. This guide maps the syllabus to App Router patterns — the same architecture powering this portfolio and blog.

Course Reference

Quick index

#TopicDescription
1Getting Started with Next.jsNext.js extends React with file-based routing, SSR, API routes, and built-in optimizations.
2Server-Side Rendering (SSR)SSR renders HTML per request — great for personalized or frequently changing data.
3Data Fetching StrategiesChoose by freshness needs: SSG (build time), ISR (revalidate interval), SSR (per request), client (SWR/React Query for interactive UI)..
4Building API RoutesRoute Handlers in app/api/ are serverless functions — ideal for BFF layers hiding secrets from the browser..
5Dynamic RoutingDynamic segments [slug] map to params.
6Image Optimization, SSG & ISRnext/image serves responsive WebP/AVIF, lazy loads, and prevents CLS with required dimensions..
7Authentication in Next.jsAuth belongs on the server: httpOnly cookies, session validation in middleware, protected Server Components and API routes..
8Final Next.js Full-Stack ProjectBuild a MERN-style app on Next.js: MongoDB via Mongoose in Route Handlers, React front-end, deploy to Vercel with env vars..

Week 14: Next.js & Server-Side Rendering

1. Getting Started with Next.js

Next.js extends React with file-based routing, SSR, API routes, and built-in optimizations. App Router (Next 13+) uses app/ with Server Components by default.

Example:

tsx
// app/blog/[slug]/page.tsx — Server Component
export default async function PostPage({ params }) {
  const { slug } = await params
  const post = getPostBySlug(slug)
  return <article>{post.title}</article>
}

Note

Add "use client" only when you need hooks, browser APIs, or event listeners — keep most UI as Server Components.

Tip

Apply this in your Week 14 course project before moving on.

Performance

Server Components send zero JS for static markup — smaller bundles and faster First Contentful Paint.

Back to index


2. Server-Side Rendering (SSR)

SSR renders HTML per request — great for personalized or frequently changing data. In App Router, dynamic pages opt into SSR by default unless cached.

Example:

tsx
// Force dynamic rendering (SSR on each request)
export const dynamic = 'force-dynamic'
 
export default async function Dashboard() {
  const stats = await fetch('https://api.example.com/stats', {
    cache: 'no-store',
  }).then((r) => r.json())
  return <pre>{JSON.stringify(stats, null, 2)}</pre>
}

Note

Pages Router used getServerSideProps. App Router uses async Server Components + fetch cache options instead.

Tip

Apply this in your Week 14 course project before moving on.

Performance

SSR costs server CPU per request — cache at CDN or use ISR when data can be slightly stale.

Back to index


3. Data Fetching Strategies

Choose by freshness needs: SSG (build time), ISR (revalidate interval), SSR (per request), client (SWR/React Query for interactive UI).

  • Static blog posts → SSG (generateStaticParams)
  • Product catalog → ISR with revalidate: 3600
  • User dashboard → SSR or client fetch with auth

Example:

tsx
// ISR — revalidate every hour
const res = await fetch('https://api.example.com/products', {
  next: { revalidate: 3600 },
})

Note

See the Next.js Caching & Rendering blog post on this site for the full App Router caching model.

Tip

Apply this in your Week 14 course project before moving on.

Performance

Default fetch in Server Components is cached — pass { cache: "no-store" } only when you need fresh data.

Back to index

Week 15: API Routes, Dynamic Routing & Advanced Features

4. Building API Routes

Route Handlers in app/api/ are serverless functions — ideal for BFF layers hiding secrets from the browser.

Example:

typescript
// app/api/tasks/route.ts
import { NextResponse } from 'next/server'
 
export async function GET() {
  const tasks = await db.task.findMany()
  return NextResponse.json(tasks)
}
 
export async function POST(request: Request) {
  const body = await request.json()
  const task = await db.task.create({ data: body })
  return NextResponse.json(task, { status: 201 })
}

Note

Validate request bodies with Zod before DB writes. Return proper HTTP status codes (400, 401, 404, 500).

Tip

Apply this in your Week 15 course project before moving on.

Performance

Keep route handlers thin — move business logic to lib/ services for testability and reuse.

Back to index


5. Dynamic Routing

Dynamic segments [slug] map to params. generateStaticParams pre-builds known paths; unknown paths can 404 or SSR on demand.

Example:

tsx
export function generateStaticParams() {
  return getPostSlugs().map((slug) => ({ slug }))
}
 
export async function generateMetadata({ params }) {
  const post = getPostBySlug((await params).slug)
  return { title: post?.title ?? 'Not Found' }
}

Note

In Next.js 15+, params and searchParams are Promises — always await them in Server Components.

Tip

Apply this in your Week 15 course project before moving on.

Performance

Pre-render popular dynamic routes at build time; use fallback patterns for long-tail content.

Back to index


6. Image Optimization, SSG & ISR

next/image serves responsive WebP/AVIF, lazy loads, and prevents CLS with required dimensions.

Example:

tsx
import Image from 'next/image'
 
;<Image
  src="/blog/cover.png"
  alt="MERN course cover"
  width={1200}
  height={630}
  priority={false}
  sizes="(max-width: 768px) 100vw, 720px"
/>

Note

Remote images need remotePatterns in next.config.js. Local images from public/ need no config.

Tip

Apply this in your Week 15 course project before moving on.

Performance

Use priority only for above-the-fold LCP images. All other images lazy-load by default.

Back to index

Week 16: Authentication & Final Project

7. Authentication in Next.js

Auth belongs on the server: httpOnly cookies, session validation in middleware, protected Server Components and API routes.

Example:

typescript
// middleware.ts
import { NextResponse } from 'next/server'
 
export function middleware(request) {
  const token = request.cookies.get('session')?.value
  if (!token && request.nextUrl.pathname.startsWith('/dashboard')) {
    return NextResponse.redirect(new URL('/login', request.url))
  }
  return NextResponse.next()
}

Note

Use Auth.js (NextAuth), Clerk, or Supabase Auth instead of rolling crypto yourself.

Tip

Apply this in your Week 16 course project before moving on.

Performance

Validate JWT/session in middleware edge — fail fast before rendering protected pages.

Back to index


8. Final Next.js Full-Stack Project

Build a MERN-style app on Next.js: MongoDB via Mongoose in Route Handlers, React front-end, deploy to Vercel with env vars.

  • Blog, e-commerce, or dashboard with auth
  • Mix SSG public pages + SSR authenticated views
  • Add API rate limiting and input validation

Note

Document env vars in .env.local.example — never commit secrets.

Tip

Apply this in your Week 16 course project before moving on.

Performance

Run next build locally before deploy. Fix dynamic import warnings and oversized client bundles early.

Back to index


Share this article

XLinkedInFacebook
Kazi Rahamatullah

Written by

Kazi Rahamatullah

FullStack Developer

X / TwitterGitHubLinkedIn

Subscribe to my newsletter

Stay up to date and get notified when I share new contents.

No spam ever, unsubscribe anytime