diff --git a/s3_full_stack_using_nextjs/01.introduction_to_nextjs/01.introduction_to_nextjs.md b/s3_full_stack_using_nextjs/01.introduction_to_nextjs/01.introduction_to_nextjs.md new file mode 100644 index 00000000..d8ab3e57 --- /dev/null +++ b/s3_full_stack_using_nextjs/01.introduction_to_nextjs/01.introduction_to_nextjs.md @@ -0,0 +1,396 @@ +# 01. Introduction to Next.js + +## What is Next.js? + +**Next.js** is a React-based **full-stack framework** for building modern web applications. + +React mainly provides the tools for building user interfaces with components. **Next.js** adds a complete application structure and features around React, such as routing, server-side capabilities, rendering strategies, data fetching, caching, and production optimizations. + +In simple terms: + +```text +React + ↓ +Build user interfaces + +Next.js + ↓ +Build complete web applications with React +``` + +Next.js is open source project developed and maintained by **Vercel**. + +--- + +# Library vs Framework + +Understanding the difference between a **library** and a **framework** helps explain why Next.js is built around React. + +## Library + +A library provides functionality that you can use when you need it. + +You generally control the application structure and decide how different libraries work together. + +```text +Your Application + ↓ +You choose + ↓ +Libraries and tools +``` + +### Example + +React is a library focused mainly on building user interfaces. + +You can choose additional tools for: + +- Routing +- State management +- Data fetching +- Backend communication +- Build configuration + +--- + +## Framework + +A framework provides a more complete structure for building an application. + +It gives you conventions and built-in features for common application requirements. + +```text +Framework + ↓ +Application structure + ↓ +Built-in conventions + ↓ +Application code +``` + +Next.js is a framework built on top of React. + +It provides conventions and features for building complete web applications while still allowing you to use the React ecosystem. + +--- + +# React vs Next.js + +| Feature | React | Next.js | +| ---------------------- | ----------------------------------------------- | ------------------------------------------------- | +| Type | UI library | React framework | +| UI Components | Yes | Yes | +| Routing | Usually added separately | Built in | +| Rendering Options | Client-side rendering (SPA) | Static, dynamic/server, and client-side rendering | +| Server-side Features | Not provided by React itself | Built in | +| Backend Endpoints | Requires a separate backend or additional setup | Route Handlers / server-side capabilities | +| Data Fetching | Developer chooses the approach | Provides framework-level patterns | +| Caching | Requires additional tools/patterns | Built-in caching and revalidation features | +| Image Optimization | Requires additional tools | Built in | +| Font Optimization | Requires additional tools | Built in | +| Application Structure | Flexible | Convention-based | +| Full-Stack Development | Usually requires additional setup | Supported in the same project | + +> **Important:** Next.js does not replace React. Next.js uses React and adds framework features around it. + +--- + +# Problems with a Traditional React SPA + +A traditional React SPA can work very well, especially for highly interactive applications. + +However, as the application becomes larger, several additional decisions are required. + +- Routing +- Rendering +- Server-Side Code +- Application Structure +- SEO + +--- + +# Why Choose Next.js? + +Next.js is useful when you want React's component-based development model together with a structured framework for building complete web applications. + +These features allow developers to focus more on application development instead of assembling and configuring every part of the application themselves. + +# Key Features of Next.js + +The following are the major features of Next.js. + +## 1. Built-in File-Based Routing + +Next.js uses the file system to define application routes. + +For example: + +```text +app/ +├── page.tsx +├── about/ +│ └── page.tsx +└── products/ + └── page.tsx +``` + +These files represent routes such as: + +```text +/ +/about +/products +``` + +--- + +## 2. Multiple Rendering Strategies + +Next.js supports different ways of rendering application content. + +The major concepts are: + +```text +CSR → Client-Side Rendering +SSR → Server-Side Rendering +SSG → Static Site Generation +ISR → Incremental Static Regeneration +``` + +--- + +## 3. Server and Client Components + +The App Router supports two types of components: + +```text +Server Components +Client Components +``` + +They allow developers to decide where component code should execute and which features are needed on the client. + +--- + +## 4. Server-Side Capabilities + +Next.js allows server-side functionality to be implemented within the same application. + +For example: + +```text +Frontend + ↓ +Server-side code + ↓ +Database / External APIs +``` + +This makes it possible to build full-stack applications without necessarily maintaining a completely separate frontend and backend project. + +--- + +## 5. Data Fetching and Caching + +Next.js provides patterns and features for: + +- Fetching data +- Caching data +- Revalidating cached data +- Handling loading states +- Handling errors + +These features help applications manage data efficiently. + +--- + +## 6. API Endpoints (Backend Routes) + +Next.js can provide backend HTTP endpoints using **Route Handlers**. + +For example: + +```text +app/ +└── api/ + └── products/ + └── route.ts +``` + +This can create an API endpoint such as: + +```text +GET /api/products +``` + +--- + +## 7. Built-in Optimizations + +Next.js provides several built-in performance features. + +Examples include: + +- Image optimization +- Font optimization +- Automatic code splitting +- Lazy loading +- Optimized production builds + +These features help reduce unnecessary client-side work and improve application performance. + +--- + +## 8. SEO and Metadata + +Next.js provides APIs for defining metadata such as: + +```text +Page title +Description +Open Graph metadata +Robots +Sitemap +``` + +For example: + +```text +
Loading products...
; +} +``` + +Detailed loading and streaming concepts are covered in **Data Fetching & UI States**. + +--- + +## `error.tsx` + +`error.tsx` provides an error UI for a route segment. + +It must be a **Client Component**. + +```text +app/ +└── products/ + ├── error.tsx + └── page.tsx +``` + +Example: + +```tsx +"use client"; + +export default function Error({ + error, + reset, +}: { + error: Error; + reset: () => void; +}) { + return ( +
+```
+
+For optimized images in Next.js applications, the `next/image` component is generally preferred.
+
+---
+
+# 13. Configuration Files
+
+Several important project-level files are normally kept at the root of the project.
+
+```text
+my-next-app/
+├── next.config.ts
+├── package.json
+├── tsconfig.json
+└── .env
+```
+
+---
+
+## `next.config.ts`
+
+`next.config.ts` is used to customize Next.js behavior.
+
+Example:
+
+```ts
+const nextConfig = {
+ images: {
+ remotePatterns: [
+ {
+ protocol: "https",
+ hostname: "images.example.com",
+ },
+ ],
+ },
+};
+
+export default nextConfig;
+```
+
+Only add configuration when the project actually needs it.
+
+> Do not modify `next.config.ts` just because the file exists.
+
+---
+
+## `package.json`
+
+`package.json` contains project metadata, dependencies, development dependencies, and scripts.
+
+Common scripts include:
+
+```bash
+npm run dev
+npm run build
+npm run start
+```
+
+---
+
+## `tsconfig.json`
+
+`tsconfig.json` contains TypeScript compiler configuration.
+
+Next.js can create and configure this file when TypeScript is enabled in the project.
+
+---
+
+# 14. Environment Variables
+
+Environment variables allow configuration values to be stored outside the source code.
+
+Example:
+
+```env
+DATABASE_URL="..."
+API_SECRET="..."
+NEXT_PUBLIC_API_URL="https://api.example.com"
+```
+
+## Server-Only Variables
+
+Variables without the `NEXT_PUBLIC_` prefix are not automatically exposed to browser code.
+
+Example:
+
+```env
+DATABASE_URL="..."
+API_SECRET="..."
+```
+
+They can be accessed on the server:
+
+```ts
+const databaseUrl = process.env.DATABASE_URL;
+```
+
+## Browser-Exposed Variables
+
+Variables that need to be available in browser code must use the `NEXT_PUBLIC_` prefix.
+
+```env
+NEXT_PUBLIC_API_URL="https://api.example.com"
+```
+
+### Important
+
+Never put secrets in a `NEXT_PUBLIC_` variable.
+
+```env
+# ❌ Do not do this
+NEXT_PUBLIC_API_SECRET="my-secret"
+```
+
+Anything using the `NEXT_PUBLIC_` prefix can be exposed to client-side code.
+
+### `.env.example`
+
+`.env.example` is a template that shows which environment variables are required by the project.
+
+It contains the variable names, but should not contain real secrets.
+
+Example:
+
+```env
+DATABASE_URL=
+API_KEY=
+JWT_SECRET=
+NEXT_PUBLIC_API_URL=
+```
+
+---
+
+# 15. Recommended Project Structure
+
+For a new medium-sized App Router project, a good starting structure is:
+
+```text
+my-next-app/
+│
+├── src/
+│ ├── app/
+│ │ ├── layout.tsx
+│ │ ├── page.tsx
+│ │ ├── globals.css
+│ │ │
+│ │ ├── about/
+│ │ │ └── page.tsx
+│ │ │
+│ │ └── products/
+│ │ ├── page.tsx
+│ │ └── [id]/
+│ │ └── page.tsx
+│ │
+│ ├── components/
+│ │ ├── Navbar.tsx
+│ │ └── ProductCard.tsx
+│ │
+│ ├── lib/
+│ │ └── api.ts
+│ │
+│ └── hooks/
+│ └── useProducts.ts
+│
+├── public/
+│ └── images/
+│
+├── next.config.ts
+├── package.json
+├── tsconfig.json
+└── .env
+└── .env.example
+```
+
+The structure separates the main concerns:
+
+```text
+src/
+ ↓
+Application source code
+
+public/
+ ↓
+Static assets
+
+Root files/
+ ↓
+Configuration and project files
+```
+
+For a Pages Router project, the main difference is:
+
+```text
+src/app/
+ ↓
+src/pages/
+```
+
+---
+
+# Summary
+
+- Next.js has two routing systems: **Pages Router** and **App Router**.
+- The **Pages Router** uses `pages/` and is important for existing projects.
+- The **App Router** uses `app/` and is recommended for new projects.
+- A Pages Router route is usually created from a file such as `pages/about.tsx`.
+- An App Router route normally requires a `page.tsx` file.
+- `src/` is optional, but it is a useful convention for medium and large projects.
+- `public/` contains static assets.
+- `next.config.ts` is used to customize Next.js behavior.
+- Environment variables are commonly stored in `.env`, `.env.local`, `.env.development`, `.env.production`.
+- Never expose secrets using the `NEXT_PUBLIC_` prefix.
+- `components/`, `lib/`, and `hooks/` are useful organization conventions, but they are not required by Next.js.
+- Avoid unnecessary folders and configuration.
+- For new projects, **App Router + `src/`** is a recommended starting structure.
diff --git a/s3_full_stack_using_nextjs/02.project_setup_and_structure/assignment.md b/s3_full_stack_using_nextjs/02.project_setup_and_structure/assignment.md
new file mode 100644
index 00000000..b658680b
--- /dev/null
+++ b/s3_full_stack_using_nextjs/02.project_setup_and_structure/assignment.md
@@ -0,0 +1,42 @@
+# Project Setup & Structure — Assignments
+
+## Assignment 1 — App Router Project Structure
+
+**Title:** Create and Organize a Next.js App Router Project
+
+**Implementation:**
+
+- Create a new Next.js project with TypeScript, ESLint, App Router, import alias, and `src/` directory enabled
+- Inside `src/`, create three folders: `components/`, `lib/`, and `hooks/`
+- In `components/`, create a `Navbar` component returning a `