# Dev mode and production

> Edit local files in dev, and edit on the live site through GitHub branches and pull requests.

The editor at `?editlet` works in dev and on the live site. Use this page to set up the live site.

| | Dev (`next dev`, `astro dev`) | Production (the live site) |
|---|---|---|
| Where edits go | Files in your working tree | Commits on `editlet/<name>` branches on GitHub |
| Sign-in | None | GitHub |
| Go live | You commit and push | A developer merges the pull request |

Edits you have not saved yet stay in the browser when you leave an entry, reload or close the editor, and you save them
together as one commit from *Your changes* (on the live site, to the same branch and pull request).

The editor picks the mode itself: if `GET /__editlet/files` answers `200`, it is in dev mode.

## Edit in dev

The editor writes files through the dev API at `/__editlet`. Only `next dev` and `astro dev` serve it; `next build`
fails if dev API code ends up in the output. The editor also shows your agent's changes within seconds.

## Set up the live site

The editor talks to GitHub from the browser with the editor's own token. Your server runs only the
[auth handler](https://editlet.gunkin.dev/docs/auth-handler.md) for the sign-in.

1. Set the admin options. They go into the public file `/editlet/config.json`, so never put a secret there. Astro
   takes the same options in `editlet({ admin: { … } })`.

   ```ts
   // next.config.ts
   export default withEditlet(nextConfig, {
     admin: { repo: 'acme/site', authUrl: 'https://example.com/api/editlet/oauth' },
   });
   ```

   | Option | Default | Use |
   |---|---|---|
   | `repo` | Vercel's git env, else `GITHUB_REPOSITORY` | The repository, `owner/name` |
   | `authUrl` | none | The auth handler's public URL. Without it, only token sign-in works |
   | `sessionMinutes` | `CMS_SESSION_MINUTES`, else 60 | Sign out after this many idle minutes (1 to 1440) |
   | `param` | `EDITLET_PARAM`, else `editlet` | The query parameter that opens the editor |

2. Set `git.baseBranch` in `cms/schema.json`. Without it, or without a repository, the editor says "Editing on the
   live site is not set up".
3. Protect the base branch with a ruleset: require a pull request with one approval, and allow no bypass. An editor's
   token can merge through the API, so only GitHub can enforce review. See [Roles for admins and editors](https://editlet.gunkin.dev/docs/roles.md).
4. Do not send `Cross-Origin-Opener-Policy: same-origin`: it breaks the sign-in popup.

## Edit and publish on the live site

1. **Sign in** with GitHub, or with a fine-grained token: **Contents** and **Pull requests** read and write,
   **Deployments** read. Each browser tab signs in on its own.
2. **Save.** The base branch is read-only. The first save creates a branch such as `editlet/post-hello-world`. Each
   later save is one more commit there.
3. **Review changes** opens `?editlet=branch:<name>`: the steps to publish (saved, the branch preview if your host
   reports deployments to GitHub, the review, live), and the changes with a diff per field. Vercel reports
   deployments; Netlify and Cloudflare Pages do not.
4. **Create pull request.** A developer merges it on GitHub, and the host redeploys. The editor never merges.

If someone changed the same file on the branch since you opened it, the save stops and your edits stay. **Compare
changes** shows each field they changed next to yours; pick one per field, apply, and save again on top of their
version.

## Names of branches, commits and pull requests

Branches start with `git.branchPrefix` (default `editlet/`). A commit reads `Update post/hello-world`, and a pull
request `Update editlet/<name>` with the label `editlet`. Change them in the `git` section of the schema
([Content model](https://editlet.gunkin.dev/docs/content-model.md)). Each commit ends with `Editlet-Source: admin` or `Editlet-Source: agent`.
In *Your changes* the editor can add a **Note**: it becomes the commit message's body, above that line. Each changed
field's diff there has **Revert this change**, which puts that field back before the save.
