Hubert
8 min
December 29, 2024

Next.js - first steps and installation

Next.js is a modern React-based framework that makes it easy to create high-performance web applications. It provides features such as server-side rendering (SSR), static page generation (SSG) and automatic code splitting. It allows you to quickly build applications optimized for SEO and based on modern standards. Next.js streamlines developers' work by offering ready-made solutions for routing, APIs and dynamic content loading.

Read more
Next.js - first steps and installation

Prerequisites to start a project with Next.js

Before getting started with Next.js, it’s a good idea to familiarize yourself with the basic technical requirements and gain some basic programming knowledge. Adequate tooling and understanding of key issues will make it easier to start working with the framework smoothly.

Familiarity with React

To use Next.js effectively, you should know the basics of React. Special attention should be paid to:

  • Components – building and using functional components,
  • JSX – syntax that allows you to create user interfaces,
  • Hooksom – basic hacks such as useState, useEffect or useContext.
    Understanding these aspects will make it easier to get into the structure and logic of Next.js faster.

Installed tools

  • Node.js (version 16 or higher) – allows you to run the server environment and build the project.
  • NPM or Yarn – one of the package managers for managing project dependencies.

To check the version of Node.js and the package manager, use in terminal:

bash

node -v

npm -v

yarn -v

Development environment

The recommended editor is Visual Studio Code, which offers support for web technologies. It is worth installing plugins:

  • ESLint – for analyzing and improving code quality,
  • Prettier – for automatic code formatting,
  • Tailwind CSS IntelliSense (optional) – for supporting projects with Tailwind.

Project installation and configuration

Preparing a project in Next.js is quick and intuitive thanks to easily accessible official tools. Here are three basic steps to install, configure and launch your first project using this framework.

Step 1: Install Next.js using create-next-app

To quickly create a new Next.js project, you can use the create-next-app tool, which automatically generates a basic project structure. In the terminal, type one of the following commands:

  • For NPM:
    bash

    npx create-next-app@latest my-next-app
  • For Yarn:
    bash

    yarn create next-app my-next-app

During installation, you can configure the project according to your needs by selecting options such as:

  • TypeScript – add support for TypeScript,
  • ESLint – a tool for analyzing and improving code quality.

Step 2: Project structure

After the installation, you will get a generated set of folders:

  • /pages – files located here automatically create URL paths (e.g. index.js corresponds to the home page).
  • /public – a folder for static resources, such as images or PDF files.
  • /styles – a place for CSS sheets, e.g. global styles for an application.
  • /components – recommended place for creating and storing reusable components.

Step 3: Start the development server

Once the project configuration is complete, start the development server using:

  • NPM:
    bash

    npm run dev
  • Yarn:
    bash

    yarn dev

The project will be available in your browser at: http://localhost:3000. Thanks to the automatic refresh, you can keep an eye on the changes made to the code.

Are you looking for a trusted IT company?
Are you looking for a trusted IT company?
Are you looking for a trusted IT company?
Contact us!

Basic concepts – Next.js

Next.js is based on several key concepts that facilitate the creation of efficient and modern applications. Getting to know them allows you to effectively use the capabilities of this language.

1. File-Based Routing
Next.js uses file-based routing, which means that each page of the application corresponds to a file in the /pages folder. For example:

  • An about.js file in the /pages folder automatically creates a /about path.
  • Subfolders in /pages generate nested URL paths, e.g. /pages/blog/post.js yields /blog/post.

This makes the creation of new pages fast and does not require the configuration of additional tools.

2. Rendering and page generation strategy
Next.js offers three main rendering strategies:

  • Server-Side Rendering (SSR): Generates a page on the server side with each request. Uses the getServerSideProps function to retrieve data.
  • Static Site Generation (SSG): Generates static pages when the project is built, using the getStaticProps function. This is ideal for content that doesn’t change frequently.
  • Client-Side Rendering (CSR): Data is fetched and rendered directly on the browser side, allowing content to be dynamically loaded when the application is loaded.

3. Default Styles
Next.js supports global CSS styles using a file, e.g. globals.css, imported in the _app.js file. For more modular solutions, you can use CSS Modules – files with a .module.css extension that allow you to isolate styles for individual components.

Creating your first application

Once you have installed and configured your project in Next.js, you can start creating your first application. By default, the index.js file in the /pages folder corresponds to the home page. To display a simple page, create a component that displays the basic text or header.

For example:

jsx

export default function Home() {

return <h1>Welcome to my Next.js!</h1> application;

}

You can also add dynamic elements, such as the current date and time, using React hooks. Use useState to store values and useEffect to update the time:

jsx

import { useState, useEffect } from ‘react’;

export default function Home() {

const [time, setTime] = useState(new Date().toLocaleTimeString());

useEffect(() => {

const interval = setInterval(() => {

setTime(new Date().toLocaleTimeString());

}, 1000);

return () => clearInterval(interval;)

}, []);

return <h1>Current time: {time}</h1>;

}

Next.js also allows you to create dynamic pages using dynamic paths. All you need to do is create a [id].js file in the /pages folder, where id is a variable.

The example structure /pages/blog/[id].js generates dynamic URLs, such as /blog/1 or /blog/2. In the dynamic page file, you can retrieve the path parameter using the getStaticProps or getServerSideProps functions. This allows the application to handle dynamic content, such as blog articles, by generating them in real time or while building the project.

Creating simple and dynamic components in Next.js is the first step to building a more advanced application.

Tools and best practices for working with Next.js

Next.js offers a number of built-in tools that streamline the development of modern applications and improve their performance. One of them is next/image, a dedicated component for optimizing images. It automatically compresses files, generates different resolutions and lazy loading, which significantly reduces page loading time. Just use it instead of the classic <img> tag to make images responsive and optimized:

jsx

import Image from ‘next/image’;

<Image src=“/example.jpg” width={500} height={300} alt=“Image description” />.

Another useful tool is next/link, which improves linking between pages in the application. This component supports navigation without reloading the page, which improves the user experience:

jsx

import Link from ‘next/link’;

<Link href=“/about”>About us</Link>;

In the context of debugging and testing, it’s a good idea to use ESLint, which is integrated with Next.js and enables code analysis for bugs and best practices. The tool’s configuration allows you to maintain project consistency and quality. For unit testing, tools such as Jest and React Testing Library are recommended, making it easy to check the performance of components in isolation.

Also worth noting are techniques for optimizing applications for SEO and performance. Next.js supports advanced meta tag management through next/head, which allows you to create search engine optimized content. In addition, a tool such as next/image enables automatic resolution adjustment and lazy loading for images, improving the user experience on mobile devices. When creating a multilingual application, it’s a good idea to take advantage of i18n’s built-in functionality, allowing you to easily configure support for different languages in your project.

With these built-in features and support tools, working with Next.js becomes more efficient and applications meet the standards of modern web development.

Summary and next steps

This is just the beginning of the possibilities that this framework offers. Once you’ve mastered the basics, it’s worth exploring more advanced features that can significantly improve application development.

One of the key features is Incremental Static Regeneration (ISR), which allows you to refresh static pages without having to fully rebuild the project. The next step is to explore the Routes API, allowing you to create server endpoints within your application, which is particularly useful for backend integration.

Learning the advanced features of Next.js will open the door to building scalable, efficient and modern web applications that meet even the most demanding user needs.

FAQ – Frequently Asked Questions

Can I use Next.js without prior knowledge of React?

Although familiarity with React is not a formal requirement, it is recommended to master basic concepts such as function components, JSX, and hooks (e.g. useStateuseEffect). This will make it easier to work with Next.js, which extends React’s functionalities.

What are the differences between Next.js and Create React App (CRA)?

Next.js offers features such as file-based routing, server-side rendering (SSR), static site generation (SSG) and image optimization that are not natively available in CRA. It is more suitable for applications that require fast page loading and good SEO optimization.

How to manage data in Next.js?

Next.js supports various data management methods, including:

  • Server-side rendering with getServerSideProps for dynamic data retrieval.
  • Static site generation with getStaticProps for static content.
  • State management with tools such as Redux, Zustand or SWR.

Can I host a Next.js project outside of Vercel?

Yes, Next.js can be hosted on various platforms such as AWS, Netlify, Heroku or Google Cloud. However, this requires configuring the appropriate server for SSR, while on Vercel the configuration is automated.

Connected articles
See all
Discover more topics