# Maintenance mode

Maintenance mode makes the app answer every visitor with a 503 while you migrate a database, restore a backup or swap a provider, and lets you through to check the result before you open it again. You turn it on and off with a command, with no deploy.

## Turn it on

Set `server.maintenance` once and deploy. Off, which is the default, the server reads nothing per request and `hamur down` refuses to run: `hamur down needs server.maintenance in hamur.config.ts; the built server does not read the flag otherwise`.

````ts [data/maintenance-config.ts]
import { defineConfig, env } from "hamur"
import { cloudflareKV } from "hamur/storage"

export default defineConfig({
  framework: "react",
  preset: "cloudflare",
  storage: { state: cloudflareKV({ binding: "STATE" }) },
  server: { maintenance: { storage: "state" } },
  tasks: { http: { token: env("TASKS_TOKEN") } },
})
````

`server.maintenance` is `true` or `{ storage }`; `true` is `{ storage: "data" }`. `storage` names the [storage](/storage-and-cache) mount that holds the state, the key `hamur:maintenance`. Every instance reads it at most every 5 seconds and keeps it in memory, so a request costs one comparison, and an instance that cannot read the mount keeps the last state it had and logs `maintenance: reading the flag failed`.

Then:

````sh [data/maintenance.sh]
hamur down --secret "k2Hq9-preview-gate" --retry-after 600 --allow 203.0.113.0/24 --message "Back at 14:00 UTC"
hamur up
hamur down --url https://acme.dev
hamur up --url https://acme.dev
````

`hamur down` takes these flags, all optional:

| Flag                      | Does                                                                             |
| ------------------------- | -------------------------------------------------------------------------------- |
| `--secret <text>`         | 16 to 128 characters of `A-Z a-z 0-9 - _`: the path that lets **you** in (below) |
| `--retry-after <seconds>` | a positive whole number; sent as `Retry-After`, which crawlers and clients read  |
| `--allow <ip or CIDR>`    | an address that is never refused; repeat the flag for more                       |
| `--message <text>`        | the message of the error page; `Service Unavailable` without it                  |
| `--url <origin>`          | runs the command on a deployed app instead of in this process                    |

It prints `Maintenance mode is on`; `hamur up` prints `Maintenance mode is off`. A bad flag stops it before anything is written, for example `hamur down --retry-after must be a positive whole number of seconds`. The [CLI reference](/cli#hamur-down) lists every message.

## What visitors get

While the state is set, the server answers `503` with `Cache-Control: no-store` and, when you gave one, `Retry-After`. A page request gets the app's [error page](/errors) with the status 503 and your message, a request for JSON gets the error body, and a [handler](/handlers) call gets `x-hamur-fn: error`. The answer comes right after static files and the health endpoint and before the origin check, the rate limit and your `middleware`, so none of them runs. The health endpoint keeps answering `200`, so a load balancer does not take the instance out. The 503 does not reach the `error` hook or the log as an error.

`<baseURL>_hamur/tasks/*` is never refused, so `hamur up --url` works while the app is down.

## Get in while it is down

Two ways pass the 503:

- **The secret.** Open `<baseURL><secret>` once, for example `https://acme.dev/k2Hq9-preview-gate`. A `GET` sets the cookie `__Host-hamur-bypass` (sealed, 12 hours, `HttpOnly`, `SameSite=Lax`) and redirects to `<baseURL>`; from then on your browser sees the app. A later `hamur down` ends the cookies of the one before. Treat the secret as a password: only its SHA-256 is stored, and it is the only way in for someone without an allowed address.
- **An address.** A request from an address given with `--allow` passes. The address is `server.requestIP`'s, so behind a proxy set `server.requestIP` (for example `{ xForwardedFor: true, trustedHops: 1 }`) or every request seems to come from the proxy.

## Per preset

| Preset                     | The mount                                                                                           | Run `hamur down`                                              |
| -------------------------- | --------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- |
| `bun`, `node`, `hamur dev` | any; the default `data` is a file store, so the command must run where the server reads it          | on the server's machine, or with `--url`                      |
| `vercel`, `netlify`        | a shared mount; the in-memory default fails the build                                               | in process with that mount's credentials, or with `--url`     |
| `cloudflare`               | a shared mount such as KV or `redis`; KV takes up to 60 seconds to reach every location             | with `--url`: a KV binding exists only in the deployed Worker |
| `static`                   | none: the build fails with `server.maintenance needs a server; the static preset serves files only` |                                                               |

On `vercel`, `netlify` and `cloudflare` the build fails when the mount is in memory: `server.maintenance needs a shared storage mount on the vercel preset; storage "data" is in memory there; set storage.data or server.maintenance.storage`. Without `--url`, `hamur down` and `hamur up` also refuse a mount that is in memory, since the process would write where the server does not read ([HAMUR_E0227](/errors/HAMUR_E0227)).

**Files a platform serves from its CDN before your server runs are not refused.** Prerendered pages on `vercel`, `netlify` and `cloudflare` keep answering `200` while the app is down; only what reaches your function gets the 503.

### With `--url`

`hamur down --url https://acme.dev` runs the task `hamur:down` on the deployed app with `POST <url><baseURL>_hamur/tasks/hamur:down`. It needs [`tasks.http`](/tasks#over-http), as in the config sample, and the token in your environment: `hamur down --url needs the token: tasks.http.token reads env "TASKS_TOKEN", which is not set here`. Without `tasks.http` in the config it stops with `hamur down --url needs tasks.http in hamur.config.ts; the deployed server serves the task endpoint only with it`.

`hamur:down` and `hamur:up` are tasks of Hamur's own and `hamur task list` shows them. Task names that start with `hamur:` are reserved: a file in `server/tasks/hamur/` fails the build.

The option table is in the [server API reference](/api-reference/server#servermaintenance).
