|
| 1 | +# Rendering Methods Demo |
| 2 | + |
| 3 | +A comprehensive demonstration of three different rendering approaches in modern web development: |
| 4 | + |
| 5 | +- **Client-Side Rendering (CSR)** - Dynamic data fetching in the browser |
| 6 | +- **Server-Side Rendering (SSR)** - HTML generated on each request |
| 7 | +- **Static Site Generation (SSG)** - Pre-built HTML at build time |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +### 🌐 CSR (Client-Side Rendering) |
| 12 | +- Demonstrates data fetching after page load |
| 13 | +- Shows loading states and error handling |
| 14 | +- Interactive refetch functionality |
| 15 | +- Real-time performance metrics |
| 16 | + |
| 17 | +### ⚡️ SSR (Server-Side Rendering) |
| 18 | +- Server-side data fetching with `getServerSideProps` |
| 19 | +- Shows render timestamp for each request |
| 20 | +- SEO-friendly pre-populated content |
| 21 | +- Server performance metrics |
| 22 | + |
| 23 | +### 🏗️ SSG (Static Site Generation) |
| 24 | +- Build-time data fetching with `getStaticProps` |
| 25 | +- Static HTML generation |
| 26 | +- Optional ISR (Incremental Static Regeneration) |
| 27 | +- Build timestamp tracking |
| 28 | + |
| 29 | +### 💧 Hydration Error Demo |
| 30 | +- Demonstrates common hydration mismatches |
| 31 | +- Shows server vs client rendering differences |
| 32 | +- Browser console error examples |
| 33 | +- Best practices for preventing hydration issues |
| 34 | + |
| 35 | +## Technology Stack |
| 36 | + |
| 37 | +- **Framework**: Next.js 14 |
| 38 | +- **Language**: TypeScript |
| 39 | +- **Styling**: Tailwind CSS |
| 40 | +- **Data Source**: JSONPlaceholder API |
| 41 | +- **Deployment**: Vercel-ready |
| 42 | + |
| 43 | +## Getting Started |
| 44 | + |
| 45 | +### Prerequisites |
| 46 | +- Node.js 18+ |
| 47 | +- npm or yarn |
| 48 | + |
| 49 | +### Installation |
| 50 | + |
| 51 | +1. Clone or download this project |
| 52 | +2. Install dependencies: |
| 53 | +```bash |
| 54 | +npm install |
| 55 | +``` |
| 56 | + |
| 57 | +3. Run the development server: |
| 58 | +```bash |
| 59 | +npm run dev |
| 60 | +``` |
| 61 | + |
| 62 | +4. Open [http://localhost:3000](http://localhost:3000) in your browser |
| 63 | + |
| 64 | +### Build for Production |
| 65 | + |
| 66 | +```bash |
| 67 | +npm run build |
| 68 | +npm run start |
| 69 | +``` |
| 70 | + |
| 71 | +## Project Structure |
| 72 | + |
| 73 | +``` |
| 74 | +├── components/ |
| 75 | +│ └── Layout.tsx # Shared layout component |
| 76 | +├── pages/ |
| 77 | +│ ├── _app.tsx # Next.js app configuration |
| 78 | +│ ├── index.tsx # Home page with explanations |
| 79 | +│ ├── csr.tsx # Client-Side Rendering demo |
| 80 | +│ ├── ssr.tsx # Server-Side Rendering demo |
| 81 | +│ ├── ssg.tsx # Static Site Generation demo |
| 82 | +│ ├── ssg-error.tsx # SSG error handling demo |
| 83 | +│ └── hydration-error.tsx # Hydration mismatch demo |
| 84 | +├── styles/ |
| 85 | +│ └── globals.css # Global styles and Tailwind |
| 86 | +└── README.md |
| 87 | +``` |
| 88 | + |
| 89 | +## Key Differences Demonstrated |
| 90 | + |
| 91 | +### CSR (Client-Side Rendering) |
| 92 | +- ✅ Fast initial page load (minimal HTML) |
| 93 | +- ✅ Rich interactivity |
| 94 | +- ✅ Reduced server load |
| 95 | +- ❌ SEO challenges |
| 96 | +- ❌ Slower perceived performance |
| 97 | +- ❌ Loading states required |
| 98 | + |
| 99 | +### SSR (Server-Side Rendering) |
| 100 | +- ✅ Excellent SEO |
| 101 | +- ✅ Fast perceived performance |
| 102 | +- ✅ Works without JavaScript |
| 103 | +- ❌ Higher server load |
| 104 | +- ❌ Slower page navigation |
| 105 | +- ❌ Complex caching |
| 106 | + |
| 107 | +### SSG (Static Site Generation) |
| 108 | +- ✅ Fastest loading times |
| 109 | +- ✅ Perfect SEO |
| 110 | +- ✅ CDN-friendly |
| 111 | +- ✅ Lower hosting costs |
| 112 | +- ❌ Static content only |
| 113 | +- ❌ Rebuild required for updates |
| 114 | +- ❌ Longer build times |
| 115 | + |
| 116 | +## Performance Comparison |
| 117 | + |
| 118 | +Navigate between the three examples to observe: |
| 119 | +- **Load times**: SSG fastest, then SSR, then CSR |
| 120 | +- **Interactivity**: CSR most interactive, SSR/SSG less so |
| 121 | +- **SEO**: SSG/SSR excellent, CSR requires additional setup |
| 122 | +- **Caching**: SSG easiest to cache, SSR complex, CSR variable |
| 123 | +- **Hydration**: SSR/SSG can have hydration mismatches, CSR doesn't |
| 124 | + |
| 125 | +## Use Cases |
| 126 | + |
| 127 | +### When to use CSR: |
| 128 | +- Single Page Applications (SPAs) |
| 129 | +- Highly interactive dashboards |
| 130 | +- Applications requiring frequent updates |
| 131 | +- User-specific content |
| 132 | + |
| 133 | +### When to use SSR: |
| 134 | +- E-commerce product pages |
| 135 | +- News websites |
| 136 | +- Dynamic content that needs SEO |
| 137 | +- Personalized content |
| 138 | + |
| 139 | +### When to use SSG: |
| 140 | +- Marketing websites |
| 141 | +- Blogs and documentation |
| 142 | +- Landing pages |
| 143 | +- Content that changes infrequently |
| 144 | + |
| 145 | +## API Used |
| 146 | + |
| 147 | +This demo uses the [JSONPlaceholder](https://jsonplaceholder.typicode.com/) API for sample data: |
| 148 | +- Posts: `/posts` |
| 149 | +- Users: `/users` |
| 150 | + |
| 151 | +## Deployment |
| 152 | + |
| 153 | +This project is ready to deploy on Vercel: |
| 154 | + |
| 155 | +1. Push to GitHub |
| 156 | +2. Connect to Vercel |
| 157 | +3. Deploy with default settings |
| 158 | + |
| 159 | +Or deploy manually: |
| 160 | +```bash |
| 161 | +npm run build |
| 162 | +npm run start |
| 163 | +``` |
| 164 | + |
| 165 | +## Learning Resources |
| 166 | + |
| 167 | +- [Next.js Documentation](https://nextjs.org/docs) |
| 168 | +- [React Documentation](https://react.dev) |
| 169 | +- [Tailwind CSS Documentation](https://tailwindcss.com/docs) |
| 170 | + |
| 171 | +## License |
| 172 | + |
| 173 | +MIT License - feel free to use this project for learning and demonstration purposes. |
0 commit comments