Final deployment
Final Deployment
Deploying your Next.js application to a live server is the final step in our project development process. In this tutorial, we will discuss how to prepare your Next.js application for production and deploy it using Vercel, which is the platform built by the creators of Next.js.
Preparing for Deployment
Before deploying your Next.js application, it's essential to prepare it for production. This is done by building the application for production using the next build
command.
To run this command, you need to open your terminal, navigate to the directory of your Next.js application, and then run the following command:
npm run build
This command will create an optimized production build of your application. The output displays information about each rendered page.
Deploying with Vercel
Vercel is a cloud platform for static sites and Serverless Functions that fits perfectly with Next.js.
To deploy your Next.js app with Vercel, you need to follow these steps:
Sign Up and Log In: First, you need to sign up for a free account on Vercel. After signing up, log into your account.
Install Vercel CLI: After logging in, install Vercel CLI by running the following command in your terminal:
npm i -g vercel
Deploy: To deploy your application, navigate to the application's directory in your terminal and run the following command:
vercel
You will be prompted to set up and deploy your project. During this setup, make sure to link your project to a Git repository for continuous deployment.
After deploying, you will get a live URL of your deployed application.
Continuous Deployment with Vercel and Git
When you deploy with Vercel and Git, every push to your Git repository will automatically trigger a deployment. This is known as 'Continuous Deployment'.
To enable continuous deployment for your Next.js app, you need to import your Git repository to Vercel. Here are the steps:
Import Project: After logging into Vercel, click on 'Import Project'.
Enter Git URL: Enter the URL of your Git repository and click 'Continue'.
Configure Project: On the 'Configure Project' page, you can choose the settings that suit your application best. Once done, click on 'Deploy'.
Now, every push to the selected branch will trigger a new deployment.
Conclusion
Congratulations! You've successfully built and deployed your Next.js application. While we've used Vercel in this tutorial, there are many other platforms you could use to deploy your Next.js application. Always choose the one that best fits your specific needs. Happy coding!