Twilight
← Back to all posts

Building Modern Websites with Next.js

February 15, 2023
2 min read

Next.js has become one of the most popular frameworks for building React applications. In this post, I'll explore why Next.js is a great choice for modern web development and how to get started.

Why Next.js?

Next.js offers several advantages:

  • Server-Side Rendering (SSR) - Improves performance and SEO
  • Static Site Generation (SSG) - Pre-renders pages at build time
  • API Routes - Build API endpoints within your Next.js app
  • File-based Routing - Intuitive page creation
  • Image Optimization - Automatic image optimization

Getting Started

Creating a new Next.js app is simple:

npx create-next-app@latest my-next-app
cd my-next-app
npm run dev

Basic Page Structure

Here's a simple Next.js page component:

// app/page.tsx
export default function HomePage() {
  return (
    <div className="container mx-auto px-4 py-8">
      <h1 className="text-3xl font-bold">Welcome to My Next.js Site</h1>
      <p className="mt-4">This is a simple Next.js page.</p>
    </div>
  );
}

Data Fetching

Next.js makes data fetching straightforward:

// app/products/page.tsx
async function getProducts() {
  const res = await fetch('https://api.example.com/products');
  if (!res.ok) throw new Error('Failed to fetch products');
  return res.json();
}
 
export default async function ProductsPage() {
  const products = await getProducts();
 
  return (
    <div>
      <h1>Products</h1>
      <ul>
        {products.map((product) => (
          <li key={product.id}>{product.name}</li>
        ))}
      </ul>
    </div>
  );
}

Conclusion

Next.js provides an excellent developer experience while delivering high-performance websites. Whether you're building a blog, e-commerce site, or complex web application, Next.js has the tools you need to succeed.

Stay tuned for more in-depth tutorials on specific Next.js features!