No history yet

Understanding Next.js

Your First Step into Next.js

So far, you've worked with React to build user interfaces. Now, it's time to build full applications. That's where Next.js comes in. It's a React framework that gives you the best of both worlds: the rich, interactive experience of a single-page application (SPA) and the performance and SEO benefits of a traditional server-rendered site.

At its core, Next.js handles the complex parts of building a production-ready application, like routing, data fetching, and optimization. This lets you focus on what makes your application unique. Two key concepts it introduces are Server-Side Rendering (SSR) and Static Site Generation (SSG). With SSR, the server prepares the HTML for each page when it's requested, which is great for dynamic content and SEO. With SSG, the HTML is generated at build time, making pages incredibly fast to load, perfect for blogs or marketing sites.

Next.js is a powerful React framework for building fast, scalable, and SEO-friendly web applications.

This means you're not just building a client-side app anymore; you're building a full-stack application where the front end and back end work together seamlessly.

Setting Up a New Project

Getting a Next.js project running is remarkably simple thanks to create-next-app. Open your terminal and run the following command. It will prompt you with a few questions to configure your project. For now, you can accept the defaults by pressing Enter for each one.

npx create-next-app@latest

Once the installation is complete, navigate into your new project directory and start the development server:

cd my-app
npm run dev

Now, open your browser and go to http://localhost:3000. You'll see the default Next.js starter page. You've just created and launched your first Next.js application!

Routing with the App Router

Next.js uses a file-system-based router. This means the structure of your files and folders inside the app directory directly determines the URL structure of your website. It’s an intuitive way to think about navigation.

Every folder inside /app creates a URL segment. To make a folder accessible as a page, you need to create a page.js file inside it. This file exports a React component that will be rendered for that route.

What about dynamic pages, like a blog post or a user profile? For this, you use folders with brackets in their names. For example, to create a page for individual blog posts, you would create a folder structure like app/blog/[slug]/page.js. The [slug] part is a dynamic segment.

In this page.js file, you can access the value of slug from the URL through the component's props.

// app/blog/[slug]/page.js

export default function Page({ params }) {
  return <div>My Post: {params.slug}</div>
}

If you navigate to /blog/hello-world, the page will display "My Post: hello-world". This powerful system makes creating clean, logical URLs straightforward.

Creating API Endpoints

Next.js isn't just for building what the user sees; it's a full-stack framework. You can create server-side API endpoints directly within your project. This is done by adding a route.js file to a folder inside /app. The folder name determines the API path, just like with pages.

For example, creating a file at app/api/hello/route.js will create an API endpoint at /api/hello.

// app/api/hello/route.js

import { NextResponse } from 'next/server'
 
export async function GET(request) {
  return NextResponse.json({ message: 'Hello from the API!' })
}

This file exports functions named after HTTP methods like GET, POST, PUT, and DELETE. Now, if you visit http://localhost:3000/api/hello in your browser, you'll see the JSON response {"message":"Hello from the API!"}. This feature allows you to build a complete backend for your application without needing a separate server, which is perfect for fetching data, handling form submissions, or connecting to a database.

Quiz Questions 1/5

What is a primary advantage of using the Next.js framework compared to a standard client-side React application?

Quiz Questions 2/5

Which of the following describes Static Site Generation (SSG) in Next.js?

You've now got the basics of creating a Next.js app, building pages with its routing system, and even setting up your own API routes. In the next section, we'll dive deeper into fetching and displaying data.