# Host a Next.js app

Host a Next.js app serverless, from Create to a live URL; push your repository as it is, it is built on every push, scales to zero, every branch gets a preview URL, and idle costs nothing.

A Next.js app here is **serverless**: you never pick a server, a machine size, an image or a
region. You push your repository as it is; the platform builds it on every push, answers every page
that `next build` wrote as a file straight away, and starts the app's own server only for requests
that need it (a server action, an API route, a dynamic page). The server stops after five idle
minutes and starts again in under a second; idle costs nothing.

## Prerequisites

- Permission to see and create apps and repositories: `deploy:DescribeApp`, `deploy:CreateApp`,
  `deploy:DeployApp` and `git:CreateRepository` (all in `AdministratorAccess`). A database also needs
  `deploy:ManageHostDatabase`.
- Next.js 15 or newer, with the App Router or the Pages Router, in a repository of your organization's
  Git ([import it](/docs/git) from GitHub or GitLab first), or the starter.

## Step 1: Create the app

**Console**
1. Open **Apps** and choose **Host a Next.js app** (or **Create app**, then **Next.js,
   serverless: scales to zero**).
2. Under **Code**, choose **My Next.js repository** and pick the repository and its production
   branch. The form reads the branch's `package.json` and says what it found: "Next.js 16.5.0
   detected; installs with npm, builds with next build". A repository that is not a Next.js app is
   offered the container choices instead. For a monorepo, set the app's folder under **Advanced**.
   No repository yet? Keep **Start from the starter**: a new repository with a plain Next.js app.
3. Enter a **Name**. The app's URL follows from it: `https://<name>-<organization>.run.apps.…`.
4. Optionally enter a **Domain**, change **Stop after** (minutes without a request before the server
   stops) and choose a **Database** (step 5 and step 6 explain both).
5. Choose **Create app**. The first build starts at once, and the app's page opens.

**CLI**
```sh
dcloud deploy hosting detect-nextjs --repository-id repository-id --branch main
dcloud deploy apps create --name shop --hosting pooled --git-repository-id repository-id
```

**API**
```http
GET /deploy/api/nextjs/detect?repositoryId=3f1c2b4a-5d6e-4f70-8a91-b2c3d4e5f601&branch=main
```

```http
POST /deploy/api/apps
{ "name": "shop", "hosting": "pooled", "gitRepositoryId": "3f1c2b4a-5d6e-4f70-8a91-b2c3d4e5f601" }
```

Nothing in the repository changes, and nothing else is set up: no workflow, no key, no build
settings. `pooledBuild` is `platform` whenever a repository is given.

## Step 2: The build, and the app is live

The app's page shows the deployment under **Deployments and previews**; its build log follows as
the build runs. A builder of the platform:

1. fetches the pushed commit;
2. installs with the package manager your lockfile names (npm, pnpm, Yarn or Bun) and the Node.js
   version in `.nvmrc` or `.node-version` (22 when there is none);
3. runs your `build` script (or `next build`), with the platform's adapter switched on through
   Next's own Adapters API: it sets `output: 'standalone'` and writes Next's description of every
   route, so your `next.config` stays as it is;
4. hands the build over and is deleted.

A small app is live within two to three minutes of the push. Every file `next build` wrote is
served from the platform's file cache; only routes Next marks as dynamic start the server. The
app's page lists them under **Routes that need the server**.

Variables the browser needs at build time (`NEXT_PUBLIC_…`) are taken from the app's environment;
every other variable reaches only the server, when it starts. A secret is never given to a build.

## Step 3: Push a change

```sh
git clone https://git.{{root}}/{{org}}/shop.git
cd shop
# edit app/page.tsx
git commit -am "My first change" && git push
```

Every push to the production branch builds and replaces production. A newer push of a branch
replaces a build of it that is still running.

## Step 4: Preview a branch

Push any other branch:

```sh
git switch -c new-checkout && git push -u origin new-checkout
```

| URL                                      | Answers with                                   |
| ---------------------------------------- | ---------------------------------------------- |
| `https://<name>-<org>.run.apps.…`        | the production branch                          |
| `https://new-checkout--<name>-<org>.…`   | the newest commit of the branch `new-checkout` |
| `https://<first 7 of the sha>--<name>-…` | exactly that commit, for good                  |

A branch URL follows the branch, so it can go into a review. Pull requests from forks never
deploy: only pushes to a branch of the repository do.

## Step 5: Add a database (optional)

Without one, the starter keeps its data in PGlite, a file database in the app's data directory.
For Postgres, choose **Postgres on the pool host** under **Database** when you create the app, or add
it later on the app's page. The app gets `DATABASE_URL`; the database's write-ahead log is archived
every minute, so it can be restored to any point of the last seven days. Details, backups and the
move to a managed database: [Use a database on the pool host](use-a-pool-host-database.md). To bind
a managed PostgreSQL instead: [Bind a database](bind-a-database.md).

## Step 6: Add your domain (optional)

Enter the domain when you create the app, or add it later under **Domains** on the app's page.
Create the DNS record the console shows; the certificate follows on its own, and the domain
answers with the production deployment. Details: [Add a custom domain](add-a-custom-domain.md).

## Logs

**Logs** on the app's page shows what the app's server prints (`console.log`, errors) within a few
seconds, production as `server/stdout` and `server/stderr`, a preview under its branch's name. The
same lines are in **Monitoring > Logs**. Each deployment keeps its build log.

## What it costs

| What                                    | Price                                                                  |
| --------------------------------------- | ---------------------------------------------------------------------- |
| An idle app, and every page from a file | nothing                                                                |
| The app's server, while it runs         | 0,008 € per GB-hour of reserved memory (768 MB: about 0,006 € an hour) |
| Requests                                | counted and shown on the bill, not charged                             |
| Builds                                  | nothing                                                                |
| Postgres on the pool host               | included                                                               |

A site with little traffic, whose server runs a few hours a day, comes to well under 2 € a month;
the same app on a machine of its own starts at the price of the smallest machine. Pooled hosting is
metered, not charged yet: the usage shows on the bill without a price, and you are told before
charging starts. Memory and the idle time before the server stops can be changed in the app's
settings.

## Building it yourself

To build somewhere else (your own CI, a laptop), create the app with `--pooled-build ci` and hand
each build over with [Deploy a bundle from anywhere](deploy-a-bundle-from-anywhere.md); a push to
the repository then deploys nothing by itself.

## Clean up

Delete the app on its page under **Danger zone**, and the repository under **Git** if you made it
for this.

## Next steps

- [Use a database on the pool host](use-a-pool-host-database.md)
- [Control who can frame an app](control-who-can-frame-an-app.md)
- [Under the hood](under-the-hood.md): how a request is answered, and the limits
