Building Modern Web Apps with Next.js
Next.js has become the go-to framework for building modern web applications. Let's explore why it's so powerful and how to use it effectively.
Key Features
Next.js provides several powerful features out of the box:
- Server-side rendering
- Static site generation
- API routes
- File-based routing
- Built-in image optimization
Getting Started
First, create a new Next.js project:
npx create-next-app@latest my-app --typescript
cd my-app
npm run devFile Structure
A typical Next.js project structure looks like this:
my-app/
├── app/
│ ├── layout.tsx
│ └── page.tsx
├── components/
│ └── ui/
├── public/
│ └── images/
└── package.jsonCode Examples
Basic Page Component
Here's a simple page component with TypeScript:
interface Post {
title: string;
content: string;
}
export default function BlogPost({ post }: { post: Post }) {
return (
<article className="prose dark:prose-invert">
<h1>{post.title}</h1>
<p>{post.content}</p>
</article>
);
}Using Server Components
Server components are a game-changer:
async function getData() {
const res = await fetch('https://api.example.com/data');
return res.json();
}
export default async function Page() {
const data = await getData();
return (
<main className="max-w-2xl mx-auto">
<pre>{JSON.stringify(data, null, 2)}</pre>
</main>
);
}Styling
Next.js works seamlessly with Tailwind CSS. Here's an example of a clean, minimal card:
export function Card({ title, description }: CardProps) {
return (
<div className="p-6 space-y-2">
<h2 className="text-xl font-medium text-gray-200">
{title}
</h2>
<p className="text-gray-400">
{description}
</p>
</div>
);
}Best Practices
-
Use TypeScript
- Better type safety
- Enhanced developer experience
- Fewer runtime errors
-
Optimize Images
typescriptimport Image from 'next/image'; export function Avatar() { return ( <Image src="/avatar.jpg" alt="Avatar" width={40} height={40} className="rounded-full" /> ); } -
Implement Error Boundaries
typescriptexport default function Error({ error, reset, }: { error: Error; reset: () => void; }) { return ( <div className="space-y-4"> <h2>Something went wrong</h2> <button onClick={reset}>Try again</button> </div> ); }
Performance Tips
Always optimize your images and implement proper caching strategies.
Loading States
export default function Loading() {
return (
<div className="animate-pulse">
<div className="h-8 w-64 bg-gray-700 rounded" />
<div className="mt-4 space-y-3">
<div className="h-4 w-full bg-gray-700 rounded" />
<div className="h-4 w-3/4 bg-gray-700 rounded" />
</div>
</div>
);
}Conclusion
Next.js provides an excellent developer experience while ensuring great performance and SEO capabilities. By following these patterns and best practices, you can build robust, maintainable web applications.
Remember to:
- Use TypeScript for better type safety
- Implement proper error handling
- Optimize for performance
- Follow the file-based routing convention
- Leverage server components when appropriate
Happy coding! 🚀