# Startup

> instrumentation.ts runs once, before anything else.

Some setup belongs to the process, not to a page: validating the environment,
configuring a shared package, opening a connection. Put it in
`src/instrumentation.ts`, and it runs once, before any page module evaluates
and before the first request:

```ts title="src/instrumentation.ts"
// Runs at import, before any page: a bad variable stops the server here.
import './env'

// Called once per process, at startup. The first render waits for it.
export async function register() {
  await db.connect()
}
```

The name is Next's. Both halves are optional: a file with only imports is a
bootstrap, and a file with only `register()` is a hook.

## Validating the environment

Import the module that checks your variables. The scaffold writes this line
when the project validates its environment:

```ts title="src/instrumentation.ts"
import './env'
```

`src/env.ts` refuses at import, so a missing variable is the server's failure
at startup rather than a visitor's, three calls later.

The build prerenders pages, so it runs this file too. A build machine without
the production variables sets `SKIP_ENV_VALIDATION=1`, which the generated
schema honours. The server that runs the build still validates at startup.

## Configuring a shared package

Configure a logger, an ORM or a metrics client at the top of the file. It runs
before the first page module evaluates, so no page finds it unconfigured:

```ts title="src/instrumentation.ts"
import { configure } from '@/lib/logger'

configure({ level: process.env.LOG_LEVEL ?? 'info' })
```

## Connecting before the first request

Do anything asynchronous in `register()`. Every way a render can arrive waits
on that one call, and after startup it has long since finished:

```ts title="src/instrumentation.ts"
export async function register() {
  await db.connect()
  await cache.warm()
}
```

A `register()` that resolved isn't called again. One that rejected is: the
next request calls it again, so a database that wasn't up yet is asked again
rather than leaving the process refusing for good.

## Closing connections on shutdown

Export `shutdown()` beside `register()` to close what `register()` opened. A
built server calls it when it is stopped, after in-flight requests have
drained, and then exits:

```ts title="src/instrumentation.ts"
export async function register() {
  await db.connect()
}

export async function shutdown() {
  await db.end()
}
```

- It is bounded by `RSC_SHUTDOWN_TIMEOUT` (10 seconds).
- One that throws is reported rather than holding the process.
- The server exits whether or not you export it; see
  [Stopping gracefully](/hosts/deployment/#stopping-gracefully).
- It isn't called on a Worker, which has no process to stop, or by the dev
  server.

## Logging requests

Export `onRequest()` to hear about every request the app answers (a page, a
navigation's payload, an action, a `route.ts`) once its response is over:

```ts title="src/instrumentation.ts"
import type { ServedRequest } from '@rsc-kit/core/request'

export const onRequestHeaders = ['cf-ray']

export function onRequest({ method, pathname, status, durationMs, aborted, headers }: ServedRequest) {
  logger.info({ method, pathname, status, durationMs, aborted, ray: headers['cf-ray'] })
}
```

To report failed renders, read `error`:

```ts title="src/instrumentation.ts"
export function onRequest({ pathname, status, error }: ServedRequest) {
  if (error) reportError(error, { pathname, status })
}
```

What it is told:

- **When the response is over.** A page streams, so the last byte leaves long
  after anything in front of the render has returned, which middleware can't
  see. `durationMs` runs up to that byte.
- **`aborted: true`** when a visitor closes the tab or navigates away
  mid-stream.
- **`error`** when a render fails, before its first byte or partway through.
- **`pathname` without the query string**, because a query carries what
  people typed.
- **Only the headers `onRequestHeaders` lists**, by lowercase name, and only
  those the request had. A cookie or an `Authorization` header reaches it
  only if you name it.

It runs beside the response, never in front of it. The body is passed through
as it streams, `onRequest()` isn't awaited, and one that throws or rejects is
reported rather than failing the page. Static files are served before the app
and aren't reported. It is called in development too.

## Using it in a command

A [command](/guides/commands/) doesn't import this file unless it exports
`register = true`:

```ts title="src/commands/queue-drain.ts"
export const register = true
```

Without it, the command runs from a bundle that leaves this file out, so
neither its environment checks nor anything else at its top level run. A queue
drain shouldn't fail on the web's secrets; it validates what it imports itself.
With it, the file is imported, `register()` runs before the command, and
`shutdown()` after it returns.

## On a Worker

Read bindings inside `register()`, not at the top of the module:

```ts title="src/instrumentation.ts"
export async function register() {
  const url = process.env.DATABASE_URL   // readable here on every host
  await db.connect(url)
}
```

A Worker evaluates the module before any request exists, and `process.env` is
empty until Nitro maps the first request's bindings onto it. A top-level
`process.env.DATABASE_URL` is `undefined` at exactly the moment it looks like
it shouldn't be. Inside `register()`, which runs at the first request, it is
there. The same file works unchanged on a server, where both moments have the
environment.

## When it runs

| Where | When |
| --- | --- |
| A server (`bun`, `node`, any long-lived preset) | At startup, before the server reports itself up |
| A Worker | At the isolate's first request. There is no startup, and a binding is only readable once a request has arrived. |
| `vite dev` | When the dev server first evaluates the app, and again when the file or anything it imports changes |
| `vite build` | Before the first page is prerendered |

It runs **once per process, not once per request**. Measured on a built server
and on a compiled binary, with a `register()` that logs: one call before the
first request, and still one after a stream of them.

On a server, a failure at startup (the file throwing at import, or
`register()` rejecting) is reported and the process exits. A server that
couldn't bootstrap has nothing correct to serve, and a health check that
passed on a server about to fail its first visitor is worse than one that
never passed. The dev server stays up and reports the error on the page.

## Why it is a framework file

Two things make this a file the framework has to know about, rather than a
module the app imports.

**Import order.** A page that configures a shared package at import time runs
before any module the app could put in front of it, because the generated
entry imports the pages. The entry imports `instrumentation.ts` *first*.
Importing a bootstrap module from every file that needs it works until someone
forgets, and the failure is a page that throws "not configured" for the one
visitor who reached it first.

**Before the first request.** `register()` is **called once**, and every entry
point a render can come through (the built server, the dev server, the
prerender, a route's middleware check, a server action, an API route) waits on
that one call rather than making its own. After startup, a request checks a
promise that has already settled. That is how "before the first render" holds
without the app knowing the list of entry points, and without a request ever
paying for it.
