Creating Frontend Applications with Next.js

May 10, 2023

Introduction

Next.js is a popular framework for building server-rendered React applications. It was developed by Zeit, and is now maintained by Vercel, the company that acquired Zeit in 2019. Next.js is built on top of React and adds several key features to make building complex web applications easier. In this blog post, we will dive into the key features of Next.js, including server-side rendering, routing, accessibility, and data fetching.

Key Features

Server-Side Rendering

One of the most significant features of Next.js is its ability to do server-side rendering (SSR) out of the box. SSR is the process of rendering the initial HTML for a web page on the server and then sending that HTML to the client, rather than sending an empty HTML file and relying on JavaScript to render the page. This has several benefits, including faster load times, better search engine optimization (SEO), and improved accessibility for users who rely on screen readers.

Next.js achieves SSR by pre-rendering each page at build time. When a user requests a page, the server sends the pre-rendered HTML to the client, along with the necessary JavaScript to hydrate the page and add interactivity. This means that the page is already rendered when the user first sees it, resulting in a faster perceived load time.

Routing

Next.js provides a powerful routing system that allows developers to define how URLs map to pages in their application. This is done using the file system, where each file in the pages directory corresponds to a page in the application. For example, a file named pages/about.js would correspond to the URL /about.

Next.js also supports dynamic routes, where a portion of the URL can be variable. For example, a file named pages/post/[id].js would correspond to the URL /post/1, /post/2, and so on.

Accessibility

Accessibility is an important consideration for any web application, and Next.js makes it easier to build accessible applications by providing several accessibility-focused features out of the box.

One of these features is automatic accessibility checks during build time. Next.js uses the a11y package to check each page for accessibility issues, such as missing alt text on images and incorrect use of ARIA attributes.

Next.js also provides a built-in <Link> component that automatically adds the appropriate aria-current attribute to indicate the current page to assistive technology.

Data Fetching

Fetching data from external APIs or databases is a common requirement for web applications, and Next.js provides several options for data fetching.

Static Site Generation (SSG) is a technique where a web page is pre-rendered at build time with the data that it needs. This can be useful for pages where the data doesn't change frequently. Next.js supports SSG out of the box, and provides several options for fetching the necessary data, including using getStaticProps to fetch data from an external API or database.

Next.js also supports Server-Side Rendering (SSR) of data, which can be useful for pages where the data changes frequently. This is done using getServerSideProps, which fetches the necessary data at request time and renders the page on the server.

Under-the-Hood

Next.js is built on top of React and uses several key technologies to provide its features.

Webpack is used to bundle the JavaScript and other assets for the application. Next.js uses a custom Webpack configuration that includes several optimizations for performance, such as code splitting and tree shaking.

Babel is used to transpile the JavaScript code to ensure that it is compatible with a wide range of browsers. Next.js uses a custom Babel configuration that includes several plugins for optimizing the output code.

Express is used as the underlying server for the application. Next.js provides a custom server implementation option as well, allowing developers to add custom middleware and logic to the server.

React is used for rendering the UI of the application. Next.js provides a pre-configured version of React that includes several optimizations for server-side rendering and performance.

Next.js also includes several other libraries and technologies, such as React-Router for routing, PostCSS for CSS preprocessing, and ESLint for code linting.

Example

Let's walk through an example of building a simple Next.js application.

First, we'll create a new Next.js project using the create-next-app CLI tool:

npx create-next-app my-app

This will create a new Next.js project in the my-app directory.

Next, we'll create a new page for our application. We'll create a file named pages/index.js with the following code:

function Home() {
  return <div>Hello, world!</div>;
}

export default Home;

This creates a new page component that simply displays the text "Hello, world!".

We can now start the development server by running:

npm run dev

This will start the Next.js development server and make our application available at http://localhost:3000.

If we open our browser and navigate to http://localhost:3000, we should see our "Hello, world!" message.

Next, let's add a dynamic route to our application. We'll create a new file named pages/post/[id].js with the following code:

function Post({ id }) {
  return <div>Post {id}</div>;
}

export async function getStaticPaths() {
  return {
    paths: [
      { params: { id: "1" } },
      { params: { id: "2" } },
      { params: { id: "3" } },
    ],
    fallback: false,
  };
}

export async function getStaticProps({ params }) {
  return {
    props: {
      id: params.id,
    },
  };
}

export default Post;

This creates a new page component that displays the ID of the post, and also defines two functions for data fetching.

getStaticPaths defines the possible paths for the page, in this case, three possible IDs: 1, 2, and 3.

getStaticProps fetches the data for the page, in this case, the ID of the post.

We can now navigate to http://localhost:3000/post/1, http://localhost:3000/post/2, and http://localhost:3000/post/3 to see our dynamic page in action.

Conclusion

Next.js is a powerful framework for building server-rendered React applications. Its key features, including server-side rendering, routing, accessibility, and data fetching, make building complex web applications easier.

Under-the-hood, Next.js uses several key technologies, including Webpack, Babel, Express, and React, to provide its features.

By providing a pre-configured development environment, Next.js allows developers to focus on building their application, rather than spending time setting up the necessary tooling.

Overall, Next.js is an excellent choice for building server-rendered React applications and is worth considering for your next web development project.

Are you looking to take the next step as a developer? Whether you're a new developer looking to break into the tech industry, or just looking to move up in terms of seniority, book a coaching session with me and I will help you achieve your goals.