description |
---|
Compile and deploy your Next.js app to production with ZEIT Now and other hosting alternatives. |
To go to production Next.js has a next build
command. When run, it will compile your project and automatically apply numerous optimizations.
Ensure your package.json
has the "build"
and "start"
scripts:
{
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
}
}
In the case that you'd want to do a full static export of the Next.js application add next export
to the "build"
script:
{
"scripts": {
"dev": "next",
"build": "next build && next export",
"start": "next start"
}
}
The easiest way to deploy Next.js to production is using the ZEIT Now platform from the creators of Next.js.
ZEIT Now integrates directly with GitHub, GitLab, and Bitbucket to give you a unique shareable url for every commit and every pull request. This url can be shared with customers and can be used to run integration tests against.
The hybrid pages approach is fully supported out of the box. Meaning that every page can either use Static Generation or Server-Side Rendering.
In case of Static Generation the page will automatically be served from the ZEIT Now Smart CDN.
When the page is using Server-Side Rendering it will become an isolated serverless function automatically. This allows the page rendering to scale automatically and be independent—errors on one page won't affect another.
API routes will also become separate serverless functions that execute and scale separately from each other.
Assets (JavaScript, CSS, images, fonts etc) and Statically Generated pages are automatically served through the ZEIT Now Smart CDN, ensuring these are always served close to your users.
HTTPS is enabled by default and doesn't require extra configuration.
You can link your project in GitHub, GitLab, or Bitbucket through the web interface. This will automatically set up deployment previews for pull requests and commits. To learn more about ZEIT Now’s Git integration, take a look at our documentation here.
You can install Now CLI from either npm or Yarn. Using npm, run the following command from your terminal:
npm install -g now
You can deploy your application by running the following command in the root of the project directory:
now
You will receive a unique link similar to the following: https://your-project.username.now.sh.
Once deployed on ZEIT Now, your projects can be assigned to a custom domain of your choice. To learn more, take a look at our documentation here.
Next.js can be deployed to any hosting provider that supports Node.js. In order to self-host there are two commands: next build
and next start
.
next build
builds the production application in the .next
folder.
next start
starts a Node.js server that supports hybrid pages, serving both statically generated and server-side rendered pages.
Generally you'll have to follow these steps to deploy to production:
- Run
npm install
- Run
npm run build
(runsnext build
) - Potentially copy the
.next
,node_modules
, andpackage.json
to your server. - Run
npm run start
(runsnext start
) on the server
In case you're doing a full static export using next export
the steps are slightly different and don't involve using next start
:
- Run
npm install
- Run
npm run build
(runsnext build && next export
) - A
out
directory is generated bynext export
- Copy the
out
directory to the server and make sure it's served by your server