Deploy a Next.js application on Netlify

Learn how to prepare and deploy a Next.js application on Netlify.

In the last document, we learned how to create a simple Next.js application. In this document, you will learn how to deploy a Next.js application on Netlify.

Configuring the project for deployment and build

Before deploying the application, we must ensure that the Next.js application is ready for production. Let's start with optimizing the images for production. First, install the following packages:

yarn install next-compose-plugins next-optimized-images

The next-compose-plugins provides a simple API and modules to make Next.js plugin configurations easier. The next-optimized-images package will automatically optimize images used in this project.

In the next.config.js file, make sure to have the following configurations.

const withPlugins = require('next-compose-plugins');
const optimizedImages = require('next-optimized-images');

const nextConfiguration = {
  target: 'serverless',
  images: {
    disableStaticImages: true,
    optimizeImages: true,
  }
};


module.exports = withPlugins([optimizedImages], nextConfiguration);

After the configuration of the Next.js plugins, let's write the netlify.toml configuration file.

# netlify.toml
[build]
  command = "yarn build"
  publish = ".next"

In the code above, we specify the required commands and configuration for Netlify to build the project and where to publish the built project. To ensure the build will pass on Netlify, you can run the build command.

yarn build

if there is no error, the project is ready for deployment on Netlify.

Deploying on Netlify

We assume here that you have a Netlify account and that the project source code is hosted on a service like Github, Gitlab, etc.

First, add a new site. If your project is already available on an online code hosting service, choose the import an existing project.

Conclusion

In this documentation, we quickly learned how to deploy a Next.js application on Netlify. In the next document, we will discuss best practices to follow when working with Next.js.

Last updated