Next.js Deployment

This guide will show you how to use the Next.js Build API to deploy managed or self-hosted applications.

Next.js Build API

The next build creates a production-ready version of your application. The following items are included in the standard output:

  • HTML files for pages using getStaticProps or Automatic Static Optimization
  • CSS files for global styles or for individually scoped styles
  • JavaScript for pre-rendering dynamic content from the Next.js server
  • JavaScript for interactivity on the client-side through React

The following output is produced in the.next folder:

  • .next/static/chunks/pages – Each JavaScript file in this folder corresponds to the same-named route. When accessing the /about the route in your application, for example, the JavaScript file loaded is.next/static/chunks/pages/about.js.
  • .next/static/media – Statically imported images from next/image are hashed and copied here
  • .next/static/css – Global CSS files for all pages in your application
  • .next/server/pages – The HTML and JavaScript entry points prerendered from the server. The .nft.json files are created when Output File Tracing is enabled and contain all the file paths that depend on a given page.
  • .next/server/chunks – Shared JavaScript chunks used in multiple places throughout your application
  • .next/cache – Output for the build cache and cached images, responses, and pages from the Next.js server. Using a cache helps decrease build times and improve the performance of loading images

To help achieve the optimal speed and support all modern browsers, all JavaScript code inside.next has been compiled and browser bundles have been minified.

Managed Next.JS with Vercel

Vercel is a frontend cloud platform developed by the Next.js team. It's the quickest and easiest method to deploy your managed Next.js application with no setup required.

When you deploy to Vercel, the platform recognizes Next.js automatically, performs next build, and optimizes the build output for you, including:

  • If cached assets are not altered, they will persist across deployments.
  • Deployments that are immutable and have a unique URL for each commit
  • If feasible, pages are statically optimized automatically.
  • A Global Edge Network API compresses and serves assets (JavaScript, CSS, pictures, and fonts). As independent routes, they are automatically optimised. Functions that don't require a server and can scale indefinitely
  • Edge Functions that have zero cold starts and boot instantly are automatically optimised as middleware.

In addition, Vercel provides features like:

  • Automatic performance monitoring with Next.js Analytics
  • Automatic HTTPS and SSL certificates
  • Automatic CI/CD (through GitHub, GitLab, Bitbucket, etc.)
  • Support for Environment Variables
  • Support for Custom Domains
  • Support for Image Optimization with next/image
  • Instant global deployments via git push

Self-hosting

You can use Node.js or Docker to self-host Next.js and get support for all features. There is also a Static HTML Export option, which has some limitations.

Node.js Server

Any hosting service that supports Node.js can be used to run Next.js. AWS EC2 or a DigitalOcean Droplet, for example.

To begin, double-check your package.

The "build" and "start" scripts are in json:

{
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  }
}

Then, to build your application, run next build. Finally, start the Node.js server by running next start. This server supports all of Next.js' capabilities.

If you're using next/image, try installing sharp in your production environment for faster Image Optimization by executing npm install sharp in your project directory. Sharp may require further settings on Linux machines to prevent excessive memory utilisation.

Docker Image

Any hosting provider that supports Docker containers can run Next.js. When deploying to container orchestrators like Kubernetes or HashiCorp Nomad, or when running on a single node in any cloud provider, this strategy can be used.

  1. Install Docker on your machine
  2. Clone the with-docker example
  3. Build your container: docker build -t nextjs-docker .
  4. Run your container: docker run -p 3000:3000 nextjs-docker

Static HTML Export

If you’d like to do a static HTML export of your Next.js app, follow the directions on our Static HTML Export documentation.

Automatic Updates

You want to see the most recent version of your Next.js application without having to reload it when you deploy it.

When routing, Next.js will load the most recent version of your application in the background. Next/link will momentarily operate as a typical <a> tag for client-side navigation.

Next.js will use the old version if a new page (with an old version) has previously been prefetched by next/link. When you go to a page that hasn't been prefetched (and isn't cached at the CDN level), you'll get the most recent version.


Related Topics

Next.js MetaData

In Next.js, we can easily modify the head section of each react page by using the built-in Head> react component. Here is our base code to start with: index.JS: import Link from 'next/link' import...

3 minutes read.

Next.js Deployment

This guide will show you how to use the Next.js Build API to deploy managed or self-hosted applications. Next.js Build API The next build creates a production-ready version of your application. The...

4 minutes read.

Next.js Typescript

Next.js comes with a built-in TypeScript experience, similar to that of an IDE. You can create a TypeScript project with create-next-app using the --ts, --typescript flag like so: npx create-next-app@latest --ts # or yarn...

3 minutes read.

Next.js Global CSS Support

Here, we will create a styles directory at the root level. We will also add a file styles.css using the code given below: Let's create global styles in Next.js that will...

3 minutes read.

Next.js Envirnomental Variables

Next.js includes support for environment variables out of the box, allowing you to accomplish things like: Use .env.local to load environment variablesExpose environment variables to the browser by prefixing with NEXT_PUBLIC_ Loading...

4 minutes read.

Next.js API MiddleWares

Next.JS API Routes have built-in middlewares that aid in the parsing of incoming requests. Here are the middlewares: req.cookies - cookies object contains the cookies sent by the request. Default value...

4 minutes read.

Next.js Shallow Routing

Shallow routing in Next.js refers to returning to the same page without using the getServerSideProps, getStaticProps, or getInitialProps methods. We use a Router with the shallow flag set to true to...

2 minutes read.

Next.js API Routes

API Routes is a Next.js feature that allows you to establish a rest API. Any file in the /pages/api subdirectory will be mapped as an API endpoint by Next.js. The...

2 minutes read.

Next.js Response Helpers

The Server Response object (often abbreviated as res) contains a set of Express.js-like helper functions that improve the developer experience and speed up the creation of new API endpoints. The includes...

3 minutes read.

Next.js Command Line Interface

You can use the Next.js CLI to launch, build, and export your application. Run the following command inside your project directory to acquire a list of accessible CLI commands: npx next -h The...

3 minutes read.

Next.js CSS Support

In Next.js, we can use the built-in CSS-in-js library styled-jsx. It is possible to write CSS within a react component, and these styles are scoped to the component. In this example,...

3 minutes read.