Skip to content

๐Ÿ“˜ Next.js Documentation โ€‹


๐Ÿ“ Introduction to Next.js โ€‹

  • What is Next.js? A React-based framework for building full-stack and production-grade web applications with server-side rendering (SSR), static site generation (SSG), and API routes.

  • Key Features

    • File-based routing
    • Hybrid SSR + SSG support
    • API routes (backend support)
    • Built-in CSS & Sass support
    • Image Optimization
    • Fast refresh & hot reloading
    • TypeScript support out of the box

๐Ÿš€ Getting Started โ€‹

Installation โ€‹

bash
npx create-next-app@latest my-app
cd my-app
npm run dev

App runs on http://localhost:3000

Project Structure โ€‹

my-app/
โ”œโ”€โ”€ pages/
โ”‚   โ”œโ”€โ”€ index.js
โ”‚   โ”œโ”€โ”€ about.js
โ”‚   โ”œโ”€โ”€ api/
โ”œโ”€โ”€ public/
โ”œโ”€โ”€ styles/
โ”œโ”€โ”€ next.config.js
โ”œโ”€โ”€ package.json

๐Ÿ“‚ Routing (Pages) โ€‹

  • Pages = Routes (pages/index.js โ†’ /)
  • Dynamic Routes: /pages/post/[id].js โ†’ /post/1
js
// pages/post/[id].js
import { useRouter } from 'next/router';
export default function Post() {
  const router = useRouter();
  const { id } = router.query;
  return <p>Post: {id}</p>;
}
  • Catch-all Routes[...slug].js โ†’ /a/b/c

๐Ÿ› ๏ธ Pre-rendering โ€‹

Static Generation (SSG) โ€‹

js
export async function getStaticProps() {
  return { props: { data: 'Hello World' } };
}

Static Generation with Paths โ€‹

js
export async function getStaticPaths() {
  return { paths: [{ params: { id: '1' } }], fallback: false };
}

Server-Side Rendering (SSR) โ€‹

js
export async function getServerSideProps() {
  return { props: { data: 'SSR Data' } };
}

๐Ÿ–ผ๏ธ Image Optimization โ€‹

js
import Image from 'next/image';

<Image src="/me.png" alt="Me" width={500} height={500} />

โœจ CSS and Styling โ€‹

  • Global CSS โ€” pages/_app.js
js
import '../styles/global.css';
  • CSS Modules
js
import styles from './Home.module.css';
<p className={styles.title}>Hello</p>
  • Sass Support Install Sass โ†’ npm install sass

โš™๏ธ API Routes (Backend Support) โ€‹

js
// pages/api/hello.js
export default function handler(req, res) {
  res.status(200).json({ message: 'Hello API' });
}

๐Ÿ”Œ Environment Variables โ€‹

  • .env.local
NEXT_PUBLIC_API_URL=https://api.example.com
  • Usage: process.env.NEXT_PUBLIC_API_URL

๐Ÿ”„ Data Fetching with SWR โ€‹

bash
npm install swr
js
import useSWR from 'swr';
const fetcher = url => fetch(url).then(res => res.json());
const { data, error } = useSWR('/api/user', fetcher);

๐Ÿ“ˆ Deployment โ€‹

  • Vercel (Recommended)npx vercel

  • Custom Server Deployment

    bash
    npm run build
    npm start

๐Ÿงฉ Advanced Concepts โ€‹

Custom _app.js โ€‹

js
export default function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />;
}

Custom _document.js (HTML structure) โ€‹

js
import { Html, Head, Main, NextScript } from 'next/document';
export default function Document() {
  return (
    <Html>
      <Head />
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  );
}

๐Ÿ“œ API Middleware (Advanced) โ€‹

js
export default function handler(req, res) {
  if (req.method === 'POST') {
    res.status(200).json({ message: 'POST Success' });
  } else {
    res.status(405).end(); // Method Not Allowed
  }
}

โšก Middleware (Edge Functions) โ€‹

js
// middleware.ts
import { NextResponse } from 'next/server';
export function middleware(request) {
  return NextResponse.redirect(new URL('/login', request.url));
}

๐Ÿ› ๏ธ next.config.js (Configuration) โ€‹

js
module.exports = {
  images: { domains: ['example.com'] },
  reactStrictMode: true,
};

๐Ÿงน Incremental Static Regeneration (ISR) โ€‹

js
export async function getStaticProps() {
  return {
    props: { data: 'Hello ISR' },
    revalidate: 10, // Regenerate page every 10 seconds
  };
}

๐Ÿ”ฅ API Authentication (with NextAuth) โ€‹

bash
npm install next-auth
js
// pages/api/auth/[...nextauth].js
import NextAuth from 'next-auth';
export default NextAuth({ providers: [] });

๐Ÿ“ฆ TypeScript Support โ€‹

bash
touch tsconfig.json
npm run dev
  • Files become .tsx, .ts
  • Types auto-generated

๐Ÿ† Best Practices โ€‹

  • Use ISR for dynamic + fast updates
  • Prefer CSS Modules or Tailwind CSS for scalable styling
  • Use SWR or React Query for client data fetching
  • Host on Vercel for built-in optimizations
  • Use API Routes for lightweight backend tasks

<<>> with โ™ฅ๏ธ by S@Nchit