---
# System prepended metadata

title: Next.js custom server revamp

---

# Next.js custom server revamp

The current custom server setup isn't complete because the user needs to set up compilation of the custom server themselves (See: https://github.com/nrwl/nx/issues/3199).

For example, if user generates a new app like so:

```bash
nx g @nrwl/next:app demo --server=server.ts
```

Then the user can run the dev server with:

```bash
nx serve demo
```
However, the production build will not compile `server.ts`, thus it is not a complete build.

The user needs to compile `server.ts` themselves with tsc/swc/whatever in order to deploy the app.

## Proposals

There are two ways to solve this issue.

1. Add step in the build to use tsc/swc to compile the `server.ts` file.
2. Leverage existing `@nrwl/express` package to generate the server app, then import the Next.js request handler from the `demo` app in order to serve the pages.

**Pros for (1):**

- Keeps everything in a single app (e.g. `demo`), so there isn't an extra project to think about / maintain.

**Pros for (2):**

- Avoids extra responsibility for `@nrwl/next:build`.
- Using what we already have in `@nrwl/express` avoids adding yet another way to compile code.
- Consistent with what is planned for `@nrwl/react`, where server app is a separate project generated with `@nrwl/express` or `@nrwl/nest`, or a custom setup.

I think (2) is the better option, but it deviates from existing set up, and requires some extra work to import the request handler of the Next.js app from the Express app. The mechanism for importing the request handler will likely mean that the Next.js app needs to be in the `paths` entry of `tsconfig.base.json` so we can do something like `import { getRequestHandler } from '@acme/demo/server'`.