Skip to main content
← ブログに戻る

Getting Started with Next.js 15 and App Router

3 min read著者: pycabbage
TutorialFrontend

Getting Started with Next.js 15 and App Router

Next.js 15 brings significant improvements to the React framework, with the App Router at its core. This guide will walk you through the essential concepts and best practices for building modern web applications.

What's New in Next.js 15

Next.js 15 introduces several exciting features:

  • Improved App Router: Enhanced performance and stability
  • Turbopack Stable: Faster development builds
  • Server Actions: Simplified data mutations
  • Partial Prerendering: Better performance optimization

Setting Up Your Project

To get started with Next.js 15, create a new project:

npx create-next-app@latest my-app --typescript --tailwind --app

This command sets up a new Next.js project with:

  • TypeScript for type safety
  • Tailwind CSS for styling
  • App Router as the routing system

Understanding the App Router

The App Router uses a file-system based routing mechanism. Here's the basic structure:

src/app/
├── layout.tsx      # Root layout
├── page.tsx        # Home page
├── about/
│   └── page.tsx    # About page
└── blog/
    ├── page.tsx    # Blog listing
    └── [slug]/
        └── page.tsx # Dynamic blog post

Server Components by Default

In Next.js 15, components are Server Components by default. This means they:

  • Run on the server
  • Can directly access databases
  • Reduce client-side JavaScript
  • Improve initial page load

To create a Client Component, add the "use client" directive:

"use client";

import { useState } from "react";

export function Counter() {
  const [count, setCount] = useState(0);
  
  return (
    <button onClick={() => setCount(count + 1)}>
      Count: {count}
    </button>
  );
}

Data Fetching Patterns

Next.js 15 simplifies data fetching with async Server Components:

async function BlogPosts() {
  const posts = await fetch("https://api.example.com/posts");
  const data = await posts.json();
  
  return (
    <ul>
      {data.map((post) => (
        <li key={post.id}>{post.title}</li>
      ))}
    </ul>
  );
}

Best Practices

  1. Use Server Components: Default to Server Components for better performance
  2. Optimize Images: Use next/image for automatic optimization
  3. Implement Loading States: Use loading.tsx files for better UX
  4. Error Handling: Create error.tsx files for graceful error handling
  5. Metadata API: Use the Metadata API for SEO optimization

Conclusion

Next.js 15 with the App Router provides a powerful foundation for building modern web applications. By leveraging Server Components, improved data fetching, and the intuitive file-based routing, you can create fast, scalable applications with excellent developer experience.

Start exploring these features in your next project and experience the benefits of Next.js 15!