Skip to content

Latest commit

 

History

History
105 lines (67 loc) · 4.29 KB

deployment.md

File metadata and controls

105 lines (67 loc) · 4.29 KB
description
Compile and deploy your Next.js app to production with ZEIT Now and other hosting alternatives.

Deployment

To go to production Next.js has a next build command. When run, it will compile your project and automatically apply numerous optimizations.

Prepare your package.json

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"
  }
}

ZEIT Now

The easiest way to deploy Next.js to production is using the ZEIT Now platform from the creators of Next.js.

Preview deployments

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.

Hybrid Next.js

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.

CDN + HTTPS by default

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.

Getting started

From a git repository

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.

Through the ZEIT Now CLI

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.

Custom domains

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.

Self hosting

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 (runs next build)
  • Potentially copy the .next, node_modules, and package.json to your server.
  • Run npm run start (runs next 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 (runs next build && next export)
  • A out directory is generated by next export
  • Copy the out directory to the server and make sure it's served by your server