NextJS Custom Server

We use a custom NextJS server to standardise logging, tracing and metrics in production. This uses NX's official support for custom servers in NextJSarrow-up-right.

Setup in new project

Follow these steps to configure our custom NextJS server in your NextJS project:

  1. Add a server.ts file to the root folder of your NextJS project with the following content. Be sure to replace the {{variables}} with correct values. See apps/web/server.ts for example setup.

import { bootstrap } from '@island.is/infra-next-server'

bootstrap({
  name: '{{projectName}}',
  appDir: 'apps/{{pathToAppDir}}',
})
  1. Then create a new tsconfig file called tsconfig.server.json with the following contents. Be sure to replace the {{variables}} with correct values.

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "commonjs",
    "noEmit": false,
    "incremental": true,
    "tsBuildInfoFile": "../../{{extraRelativePathToRoot}}tmp/buildcache/apps/{{pathToAppDir}}/server",
    "types": [
      "node"
    ]
  },
  "include": ["server.ts"]
}
  1. Finally, open the project.json file in your project folder and replace the "build" and "serve" target keys with the following content. Remember to replace the {{variables}} with correct values, and edit the build configurations as needed.

Last updated

Was this helpful?