> ## Documentation Index
> Fetch the complete documentation index at: https://docs.outerlayer.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Preview a work item

> Open a live copy of a work item's pull request in your browser, and declare how a repository runs it.

A preview is a live copy of a work item's pull request. OuterLayer runs the app at the pull request's head commit in a managed sandbox. A member opens it in a new browser tab and uses it before the merge.

> [!NOTE]
>
> Previews are in early access. The **Preview** row shows only for an organization that has previews turned on. To turn them on, email [hello@outerlayer.ai](mailto:hello@outerlayer.ai).

## Open a preview

1. Open a work item that has an open pull request.
2. Select **Start preview** in the **Preview** row of the item's sidebar.
3. Wait while the page shows **Preview starting**. It shows the time since the start.
4. When the page shows **Preview ready**, select **Open preview**. A new tab loads the app.

Only members of the organization reach a preview. **Open preview** makes a link that works for one minute, so a link you copy stops working.

A second member who opens the same item joins the same preview. No second sandbox starts.

## What the item page shows

| State | What it means |
| - | - |
| **Preview starting** | A runner is taking the preview, or it runs the `setup` and `seed` commands. |
| **Preview ready** | The app runs. Select **Open preview**. |
| **Preview out of date** | The pull request has a newer commit than the one the preview runs. Select **Start again** to run the new head. |
| **Preview stopped** | The preview ended. Select **Start preview** to run it again. |
| **Preview failed** | A `setup` or `seed` command failed, or the runner restarted before the preview was ready. Select **View the failure** in the Preview row to see the command and the last 50 lines of its output. |
| **Preview did not start** | The organization is read-only, has used its trial hours, or has reached its spending cap. The page shows the reason. |

The action is off, with a reason beside it, in these cases:

* The repository declares no preview. The reason links to this page.
* The pull request changes no file that the preview shows. See `paths` below.
* The pull request comes from a fork. A fork gets no preview.
* GitHub does not answer. Try again in a few minutes. A running preview still opens.
* You do not hold **Add work**. A member with the Read role can open a running preview, but cannot start or stop one. See [Roles and permissions](https://docs.outerlayer.ai/roles-and-permissions.md).

An item with no open pull request has no preview action.

## Declare a preview

Add a `preview` block to `.outerlayer-proofs.yaml` at the root of the repository. The file also needs a `runners` list, which says how to run your tests. Use `runners: []` for none.

```yaml theme={"system"}
setup:
  - yarn install
runners: []
preview:
  open: 3000
  ports: [3000, 4000]
  seed:
    - yarn db:seed
  paths:
    - "apps/web/**"
```

| Key | Meaning |
| - | - |
| `ports` | Required. The ports the app listens on. |
| `open` | Required. The port that the new tab loads. It must be one of `ports`. |
| `seed` | Commands that run in order after every `setup` command. Optional. |
| `paths` | Globs of the files that the preview shows. Optional. With no `paths`, every change gets a preview. |

The preview runs the `setup` commands of the file, then the `seed` commands, then it waits for the `open` port. Make the app listen on `0.0.0.0`, not on `localhost`.

The commands run in a container of the build image. A build of the same item uses that image. The commands find the same tools as a build, such as `yarn` and `mise`. They reach the sandbox's Docker as a build's commands do, so `docker compose up -d --wait` starts a database. A command that starts a server in the background keeps it running after the command ends. If the repository has a `.outerlayer/setup-root.sh` script, the preview runs it as a build does, and the container starts from the image it makes.

Check the file with `outerlayer work proofs --check`. See [The proofs file](https://docs.outerlayer.ai/reference/cli-work.md#the-proofs-file) and [The preview block](https://docs.outerlayer.ai/reference/cli-work.md#the-preview-block). A runner needs CLI 0.46 or newer.

## Give the app its preview address

Each port has its own address in the form `https://<preview id>-<port>.<preview domain>`. A browser keeps the cookies of one port apart from another.

Every `setup` and `seed` command gets the address of each declared port in the variable `OUTERLAYER_PREVIEW_URL_<port>`. For port 4000 the variable is `OUTERLAYER_PREVIEW_URL_4000`.

> [!WARNING]
>
> An app whose browser code calls `http://localhost:<port>` does not work in a preview. The browser is on your machine, and nothing listens there. Have the app read `OUTERLAYER_PREVIEW_URL_<port>` when it is set up, and use that address for the calls of its browser code.

An app that is split across two ports needs this. A web app on port 3000 that calls an API on port 4000 reads `OUTERLAYER_PREVIEW_URL_4000` for the API address.

A call to another port's address is a call to another origin. Send the call with credentials, so that the browser sends the preview cookie. Use `credentials: 'include'` for `fetch` and `withCredentials: true` for axios. A call without credentials gets a not-found answer.

The app needs no CORS setup for these calls. The preview answers the browser's preflight request and sets the CORS headers itself. It does this only for the other ports of the same preview.

## Limits

* **The sign-in callback.** A sign-in provider that checks the redirect address rejects the preview host. OuterLayer cannot fix this. Use a sign-in method that has no redirect, such as email and password, for the preview.
* **Idle time.** A preview stops after 30 minutes with no request.
* **Total time.** A preview stops 4 hours after it started, even when requests still arrive.
* **One preview.** An item has at most one running preview.
* **Runner restarts.** A ready preview keeps running when its runner restarts, for example to update itself. A preview that is still starting fails. Start it again.
* **Managed sandboxes.** The organization needs managed sandboxes. A preview does not run on a self-hosted runner without them.
* **No secrets.** The sandbox holds no build variable and no token that can write to the repository. The app cannot push to the repository.
* **Data.** A preview keeps no data after it stops. Each start runs `setup` and `seed` again.
* **Network.** A preview reaches GitHub and the hosts a managed build reaches: the default hosts and the factory's **Allowed hosts**. A `setup` command that downloads from another host fails. Add that host under Settings → Build settings → **Allowed hosts**. See [Managed build settings](https://docs.outerlayer.ai/build-security.md#managed-build-settings).

## Billing

A preview counts as managed build-hours of the organization, as a build does. It runs on a Small machine, so each hour of a preview uses 1 hour of the pool. The time counts from the start of the sandbox until it ends.

A preview does not start when the organization is read-only, has no trial hours left, or has reached its spending cap. See [Managed build-hours](https://docs.outerlayer.ai/billing-and-trials.md#managed-build-hours).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.