# Next.js

Build, serve and start every Next.js app, with the build output cached.

The Next.js plugin gives every package with a Next.js config a `build`, `dev` and `start` task, so a repository of Next.js apps builds, caches and serves them without a script or a line of `axle.toml` for each.

## Add it

```sh
axle add next
```

This writes it into the root `axle.toml`:

```toml
[plugins.next]
source = "npm:@axle-rs/plugin-next@0.1.0"
hash = "sha256-…"
```

## Tasks

A package or project with a `next.config` file, `.ts`, `.mts`, `.cts`, `.js`, `.mjs` or `.cjs`, in its folder gets:

| Task    | Command      | Settings                                                           |
| ------- | ------------ | ------------------------------------------------------------------ |
| `build` | `next build` | `outputs` the build folder, without its `cache` folder             |
| `dev`   | `next dev`   | `persistent`, ready when Next.js prints `Ready`                    |
| `start` | `next start` | `persistent`, ready when Next.js prints `Ready`, after the `build` |

Each command runs through the repository's package manager, such as `pnpm exec next build`, so it uses the version the package installs.

The build folder is read from `distDir` in the config without running it, `.next` by default. Its `cache` folder is left out of the outputs, since Next.js keeps its own cache there. When `distDir` is set to something other than a plain string, such as `process.env.DIST`, the plugin leaves `outputs` out and Axle [finds the outputs](https://axlers.dev/docs/reference/cache.md#outputs) the build writes itself, also leaving out `.next/cache`.

Axle already hashes the `NEXT_PUBLIC_` variables a Next.js app reads, so a build made for one environment isn't replayed for another.

## Scripts the repository already has

A package's own script wins. When its `package.json` has a script with a task's name that runs something else, such as `"build": "prisma generate && next build"`, the plugin leaves that task alone and Axle runs the script. A script that only runs Next.js the way the plugin would, `next build`, `next dev` or `next start`, is replaced by the plugin's task, which runs the same command and adds its settings. The repository's `axle.toml` changes a task as it would any other, and wins over the plugin.

## Upgrading to Next.js 16

[`axle migrate --upgrade`](https://axlers.dev/docs/reference/migrate.md#upgrading-tools) moves each package that asks for Next.js 15 or older to Next.js 16:

- `next`, and `eslint-config-next` when the package has it, become `^16.0.0` in `dependencies` or `devDependencies`. A version from `workspace:`, `catalog:`, a link or a tag is left alone.
- Next.js 16 no longer has `next lint`, so a script that runs it runs `eslint .` instead, keeping its other arguments, such as `"lint": "eslint . --fix"`. When the package doesn't list `eslint`, the change says to add it.

```text
Updated apps/web/package.json

plugin next: web: next ^15.3.0 is now ^16.0.0
plugin next: web: the lint script runs eslint . instead of next lint, which Next.js 16 removed
```

Install the dependencies afterwards, so the lockfile has the new versions, then build and test the apps. Read the [Next.js 16 upgrade guide](https://nextjs.org/docs/app/guides/upgrading/version-16) for the changes no codemod can make for you, such as renaming `middleware` to `proxy`.

## Settings

| Setting | Default   | Meaning                                                |
| ------- | --------- | ------------------------------------------------------ |
| `build` | `"build"` | the name of the build task, or `false` to leave it out |
| `dev`   | `"dev"`   | the name of the dev server task, or `false`            |
| `start` | `"start"` | the name of the production server task, or `false`     |

```toml
[plugins.next]
source = "npm:@axle-rs/plugin-next@0.1.0"
hash = "sha256-…"
start = false
```

The plugin's answers are kept until a `next.config.*`, a `package.json`, `pnpm-workspace.yaml` or a lockfile changes.
