Skip to content

recipes/nextjs

View source on GitHub

typescript
import '@catapultjs/deploy/recipes/nextjs'

This recipe runs the Next.js build on the server. It does not override deploy:update_code, so combine it with a transfer recipe such as git or rsync.

It is designed for Next.js standalone output. After next build, it symlinks public and .next/static into .next/standalone/ when that directory exists.

See the example Next.js project for a complete setup, or go directly to deploy.ts.

Tasks

TaskInsertedDescription
deploy:buildafter deploy:sharedOverrides the built-in build task, runs next build, and prepares standalone

The recipe runs the build command from /.

Configuration

KeyTypeDefaultDescription
shared_filesstring[]['.env']Files symlinked from shared/ into each release
nextjs_pathstring''Sub-path to the Next.js app within the repository
nextjs_out_pathstring'.next/standalone/'Standalone output path receiving the symlinks
source_pathstring''Used as the default value for nextjs_path
typescript
import { set } from '@catapultjs/deploy'

set('nextjs_path', 'apps/web')
import '@catapultjs/deploy/recipes/nextjs'

For standalone output, make sure your Next.js config enables it:

typescript
const nextConfig = {
  output: 'standalone',
}

export default nextConfig

Next.js creates .next/standalone/server.js for standalone deployments. It does not copy public or .next/static into the standalone directory by default; this recipe links them into the standalone output after the build so the generated server can serve them.

For PM2, use the standalone server entry from the active release:

javascript
const path = require('path')
const deployPath = '/home/deploy/myapp'

module.exports = {
  apps: [
    {
      name: 'next',
      cwd: path.join(deployPath, 'current'),
      script: '.next/standalone/server.js',
    },
  ],
}

See Next.js' output: 'standalone' documentation for the upstream standalone output details.