nextjs
Overview
Next.js is a React framework for production. It provides hybrid static and server rendering, TypeScript support, smart bundling, route pre-fetching, and more with zero configuration.
Quick Start
Create Repository from Template
On your dashboard, go to the Tools & Deployment tab. Click the "New Repository" button, select the Next.js template from the dropdown, enter a repository name and subdomain, then click "Create Repository".
Clone Your Repository
git clone https://<git-server>/<username>/<repo-name>.git
cd <repo-name>
Install Dependencies
npm install
Start Development Server
npm run dev
Open http://localhost:4200 in your browser.
Build & Push
npm run build
git add .
git commit -m "Initial setup"
git push origin main
Local Development with Docker
If you don't have Node.js installed locally, you can use Docker to run Next.js commands.
Running Commands with Docker
# Run npm install
docker run --rm -v $(pwd):/app -w /app node:lts-alpine npm install
# Run development server
docker run --rm -v $(pwd):/app -w /app -p 4200:4200 node:lts-alpine npm run dev
# Build for production
docker run --rm -v $(pwd):/app -w /app node:lts-alpine npm run build
Project Structure
├── app/ # App Router (Next.js 13+)
│ ├── layout.js # Root layout
│ ├── page.js # Home page
│ ├── api/ # API routes
│ └── [routes]/ # Dynamic routes
├── public/ # Static files
├── components/ # React components
├── styles/ # CSS files
├── next.config.js # Next.js configuration
├── package.json # Dependencies
└── Dockerfile # Docker config
Common Tasks
Creating a Page
Create files in app/ directory:
// app/about/page.js
export default function AboutPage() {
return <h1>About Us</h1>;
}
Creating an API Route
// app/api/users/route.js
export async function GET() {
return Response.json({ users: [] });
}
export async function POST(request) {
const data = await request.json();
return Response.json({ created: data });
}
Server vs Client Components
// Server Component (default) - runs on server
export default async function Page() {
const data = await fetch('https://api.example.com');
return <div>{data}</div>;
}
// Client Component - add 'use client' directive
'use client';
import { useState } from 'react';
export default function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
Useful Commands
npm run dev
Start dev server with Turbopack
npm run build
Create production build
npm start
Start production server
npm run lint
Run ESLint
Troubleshooting
Hydration mismatch errors
Solution: Ensure server and client render the same content. Use 'use client' for interactive components.
useState not working
Solution: Add 'use client' at the top of your file. Server Components can't use hooks.
Build fails after deployment
Solution: Run npm run build locally first to check for errors before pushing.