PresentationInteractive, narrated presentation of the section content.

ContentDetailed description of the section content.

Next.js Foundations πŸš€

In this module of our Full-Stack Development Course, we dive into Next.js, a powerful React framework that makes building fast, scalable, and SEO-friendly web applications easier than ever. Next.js goes beyond traditional React by offering server-side rendering (SSR), static site generation (SSG), and API routes, making it perfect for modern full-stack applications. Throughout this course, we’ll progressively build a feature-rich blogging and content management system (CMS), learning how to fetch and mutate data, optimize performance, and secure our app with authentication. By the end, you’ll have the essential skills to develop and deploy production-ready full-stack applications with Next.js! πŸš€ 

While we will use a project with some boilerplate, let's first take a look at how can you quickly start a new project with Next.js!

🎬 Starting from Scratch

If you want to create a clean new Next.js project, you can use the the following command, which will take you through series of questions to set up your project:

pnpx create-next-app@latest

This is what you can observe in the console:

Let's break it down!

1. What is your project named?

Example: my-test-project

This defines the name of your project folder. Whatever you enter here will be used as the directory name where your Next.js project is initialized.

πŸ“Œ Effect: Creates a folder with this name and sets up a Next.js project inside it.


2. Would you like to use TypeScript? (No / Yes)

TypeScript is a superset of JavaScript that adds static typing, which helps catch errors at compile time rather than runtime.

β€’ Yes β†’ The project will be set up with TypeScript, and .ts/.tsx files will be used instead of .js/.jsx.
β€’ No β†’ The project will use plain JavaScript.

πŸ“Œ Effect: If enabled, Next.js generates a tsconfig.json file and ensures TypeScript is properly configured.


3. Would you like to use ESLint? (No / Yes)

ESLint is a tool that helps enforce consistent coding style and catch common errors in JavaScript/TypeScript.

β€’ Yes β†’ A .eslintrc.json file is created with Next.js-specific linting rules.
β€’ No β†’ No linting setup is included.

πŸ“Œ Effect: Helps maintain clean, readable, and bug-free code.


4. Would you like to use Tailwind CSS? (No / Yes)

Tailwind CSS is a utility-first CSS framework that allows for rapid UI styling using predefined classes.

β€’ Yes β†’ Tailwind CSS is installed and configured in tailwind.config.js.
β€’ No β†’ No CSS framework is included (you can add your own later). The initial project will use CSS modules

πŸ“Œ Effect: If enabled, Next.js will automatically configure Tailwind CSS and PostCSS for styling.


5. Would you like your code inside a src/ directory? (No / Yes)

By default, Next.js places its files (pages/, components/, etc.) in the root directory. Enabling this option moves everything inside a src/ folder, making the root cleaner.

β€’ Yes β†’ The project structure will look like:

/my-test-project
β”œβ”€β”€ /src
β”‚   β”œβ”€β”€ /app (or /pages if using Pages Router)
β”‚   β”œβ”€β”€ /components
β”‚   β”œβ”€β”€ /styles

β€’ No β†’ All project files remain at the root level.

πŸ“Œ Effect: Organizes the code into a src/ folder for better project structure.


6. Would you like to use App Router? (recommended) (No / Yes)

Next.js has two routing strategies:

β€’ Pages Router (Legacy) β†’ Uses pages/ directory and file-based routing (e.g., pages/index.js).
β€’ App Router (Recommended) β†’ Uses app/ directory and introduces React Server Components for improved performance.
β€’ Yes β†’ The project is set up with App Router (app/ directory), supporting modern features like Server Actions and Streaming.
β€’ No β†’ Uses the older Pages Router (pages/ directory).

πŸ“Œ Effect: Choosing App Router enables the latest Next.js features and optimizations.


7. Would you like to use Turbopack for next dev? (No / Yes)

Turbopack is Next.js’ new Rust-based bundler that replaces Webpack for local development.

β€’ Yes β†’ Uses Turbopack, which provides faster startup and hot reloading times.
β€’ No β†’ Uses Webpack instead, which is more stable but slower.

πŸ“Œ Effect: Choosing Turbopack makes development faster but is still in progress for some features.


8. Would you like to customize the import alias (@/* by default)? (No / Yes)

Next.js allows you to use import aliases to make importing files easier. By default, it uses:

import Button from '@/components/Button';

Instead of:

import Button from '../../components/Button';

β€’ Yes β†’ You can define a custom alias (e.g., ~/ instead of @/).
β€’ No β†’ Uses the default alias @/.

πŸ“Œ Effect: Helps with cleaner imports, especially in large projects.


9. What import alias would you like configured? (Default: @/*)

If you chose β€œYes” in the previous question, you can customize the alias.

β€’ Example: Entering ~/ would let you do:

import Button from '~/components/Button';

πŸ“Œ Effect: Creates a tsconfig.json or jsconfig.json file with a custom alias setting.


Final Thoughts

These options let you tailor your Next.js project based on your preferences and needs. If you’re new to Next.js, using the recommended defaults (TypeScript, ESLint, Tailwind, App Router) is a great way to get started with modern best practices. πŸš€

Folder Structure

Next.js follows a convention-based folder structure that helps organize your project efficiently. Depending on whether you use the App Router (app/ directory) or Pages Router (pages/ directory), the structure will differ slightly. We will focus on app router only

Introduced in Next.js 13, the App Router uses React Server Components by default and enables powerful features like layouts, streaming, and server actions.

/my-next-app
β”œβ”€β”€ /app
β”‚   β”œβ”€β”€ /layout.tsx
β”‚   β”œβ”€β”€ /page.tsx
β”‚   β”œβ”€β”€ /dashboard
β”‚   β”‚   β”œβ”€β”€ /page.tsx
β”‚   β”‚   β”œβ”€β”€ /settings
β”‚   β”‚   β”‚   β”œβ”€β”€ /page.tsx
β”‚   β”œβ”€β”€ /api
β”‚   β”‚   β”œβ”€β”€ /route.ts
β”œβ”€β”€ /components
β”œβ”€β”€ /lib
β”œβ”€β”€ /public
β”œβ”€β”€ /styles
β”œβ”€β”€ /utils
β”œβ”€β”€ /node_modules
β”œβ”€β”€ .gitignore
β”œβ”€β”€ next.config.js
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json

Key Folders & Files in app/ Directory
    β€’    app/ β†’ The main folder where pages, layouts, and API routes live.
    β€’    app/layout.tsx β†’ Defines the root layout for the application (persists across all pages).
    β€’    app/page.tsx β†’ The default home page (/).
    β€’    app/dashboard/page.tsx β†’ A route for /dashboard.
    β€’    app/dashboard/settings/page.tsx β†’ A nested route for /dashboard/settings.
    β€’    app/api/route.ts β†’ Defines API endpoints (e.g., /api).

πŸ“Œ App Router Benefits:

βœ… Uses React Server Components for better performance.
βœ… Supports nested layouts and loading states.
βœ… Enables streaming and server-side rendering (SSR) by default.

Running The Project

To run the Next.js project, please run the following command in the web project directory

pnpm run

This starts the development server at http://localhost:3000. If you are running multiple applications, the port may change and you may see the following message:

$ pnpm dev

> my-test-project@0.1.0 dev /Users/30031005/Github/WSU/FSD/my-test-project
> next dev --turbopack

 ⚠ Port 3000 is in use, trying 3001 instead.
   β–² Next.js 15.1.7 (Turbopack)
   - Local:        http://localhost:3001
   - Network:      http://192.168.1.184:3001

In this case I would open the project at the following URL: http://localhost:3001. When you open the browser at that URL, you should see the following:

 

Our (Monorepo) Project

In this chapter we will be working in the same code base from your second assignment. To learn how to set it up, please follow instructions here:.

We recommend you to create a new β€œLecture” branch to play with exercises in the lecture.

 

No hints available.

Discuss with OthersAsk questions, share your thoughts, and discuss with other learners.

Loading discussion ...