Next.js 14 Tutorial: Building Modern Web Applications
Next.js 14 brings exciting new features and improvements that make building modern web applications easier and more efficient than ever. In this comprehensive tutorial, we'll explore the key features and build a complete application.
What is Next.js?
Next.js is a React framework that provides an excellent developer experience with features like server-side rendering, static site generation, and automatic code splitting. It's designed to make building production-ready React applications simple and efficient.
Key Features of Next.js 14
1. App Router
The new App Router introduces a file-system based routing system that's more intuitive and powerful than the previous Pages Router.
2. Server Components
React Server Components allow you to write components that run on the server, reducing the JavaScript bundle size sent to the client.
3. Improved Performance
- Automatic code splitting
- Image optimization
- Font optimization
- Built-in performance monitoring
Getting Started
Prerequisites
- Node.js 18.17 or later
- Basic knowledge of React and JavaScript
Installation
npx create-next-app@latest my-nextjs-app
cd my-nextjs-app
npm run dev
Project Structure
my-nextjs-app/
├── app/
│ ├── layout.tsx
│ ├── page.tsx
│ ├── globals.css
│ └── (routes)/
├── components/
├── lib/
├── public/
└── package.json
Building Components
Server Components
Server components run on the server and can directly access backend resources:
// app/page.tsx
import { getData } from '@/lib/data'
export default async function HomePage() {
const data = await getData()
return (
<div>
<h1>Welcome to Next.js 14</h1>
<p>Data: {data}</p>
</div>
)
}
Client Components
For interactive components, use the 'use client' directive:
'use client'
import { useState } from 'react'
export default function Counter() {
const [count, setCount] = useState(0)
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
</div>
)
}
Routing
File-based Routing
Create routes by adding files to the app directory:
app/
├── page.tsx // Home page (/)
├── about/
│ └── page.tsx // About page (/about)
├── blog/
│ ├── page.tsx // Blog index (/blog)
│ └── [slug]/
│ └── page.tsx // Dynamic blog posts (/blog/[slug])
└── api/
└── hello/
└── route.ts // API route (/api/hello)
Dynamic Routes
Use square brackets for dynamic segments:
// app/blog/[slug]/page.tsx
export default function BlogPost({ params }: { params: { slug: string } }) {
return (
<div>
<h1>Blog Post: {params.slug}</h1>
</div>
)
}
Data Fetching
Server-side Data Fetching
Fetch data directly in server components:
async function getPosts() {
const res = await fetch('https://api.example.com/posts')
return res.json()
}
export default async function BlogPage() {
const posts = await getPosts()
return (
<div>
{posts.map((post) => (
<article key={post.id}>
<h2>{post.title}</h2>
<p>{post.excerpt}</p>
</article>
))}
</div>
)
}
Styling
CSS Modules
Create component-specific styles:
/* components/Button.module.css */
.button {
padding: 12px 24px;
background: #0070f3;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
import styles from './Button.module.css'
export default function Button({ children }) {
return <button className={styles.button}>{children}</button>
}
Tailwind CSS
Next.js has excellent Tailwind CSS integration:
export default function Card({ title, description }) {
return (
<div className="bg-white rounded-lg shadow-md p-6">
<h3 className="text-xl font-semibold mb-2">{title}</h3>
<p className="text-gray-600">{description}</p>
</div>
)
}
Deployment
Vercel (Recommended)
Deploy to Vercel with zero configuration:
npm install -g vercel
vercel
Other Platforms
Next.js can be deployed to any platform that supports Node.js:
- Netlify
- AWS
- Google Cloud
- DigitalOcean
Best Practices
1. Use Server Components by Default
Start with server components and only add 'use client' when you need interactivity.
2. Optimize Images
Use the Next.js Image component for automatic optimization:
import Image from 'next/image'
export default function OptimizedImage() {
return (
<Image
src="/hero.jpg"
alt="Hero image"
width={800}
height={400}
priority
/>
)
}
3. Implement SEO
Use metadata API for better SEO:
import { Metadata } from 'next'
export const metadata: Metadata = {
title: 'My Next.js App',
description: 'A modern web application built with Next.js',
}
export default function Page() {
return <div>Content</div>
}
Conclusion
Next.js 14 provides an excellent foundation for building modern web applications. Its combination of performance, developer experience, and flexibility makes it an ideal choice for projects of all sizes.
The App Router and Server Components represent the future of React development, and Next.js 14 makes these features accessible and easy to use.
Start building with Next.js 14 today and experience the future of web development!