---
name: self-hosted-expo-ota
description: Set up and publish self-hosted Expo Updates on a Cloudflare Worker with expo-worker-ota (R2, laptop signing, Workers Free). Use when the user mentions OTA, expo-updates, EAS Update alternative, Cloudflare Worker updates, publish JS to a phone, runtimeVersion, rollback-to-embedded, or npx expo-worker-ota. Never invent the Expo Updates protocol.
---

# Self-hosted Expo OTA

This skill is a babysitter. The protocol lives in this repo's scripts. Copy them. Do not generate Expo Updates v1 from docs, memory, or "a simple Worker that returns JSON."

## Install

If the app has no `ota-worker/` folder:

```bash
npx github:ral7/expo-worker-ota init
```

If they already have Cursor and want this skill in the project:

```bash
npx skills add ral7/expo-worker-ota
```

Init copies the Worker, `scripts/publish-ota.mjs`, `scripts/verify-ota.mjs`, `scripts/deploy-ota.mjs`, gitignores `keys/`, and generates the signing cert. It does not upload the private key.

## Five steps (the whole UX)

1. In their Expo app: `npx github:ral7/expo-worker-ota init`
2. Init writes `ota-worker/`, `scripts/`, gitignores `keys/`, generates the cert, and prints what goes in `app.json`. Do not edit `wrangler.toml` by hand unless they asked for a custom name (`--name`).
3. Cloudflare once: `npx wrangler login` then `npm run ota:deploy` (creates the R2 bucket and deploys). Deploy writes `updates.url`.
4. Ship **one** Release binary with that URL baked in. TestFlight is enough. Metro / Debug will not download OTA.
5. Forever: `npx expo export --platform ios --platform android` then `npm run ota:publish`

Default `runtimeVersion` is `"1"`. That is the native shell, not the store version (`1.0.4` is unrelated).

## Decision tree

Before any publish:

- Native plugin, permission, Expo SDK, signing cert, or `updates.url` changed → **stop**. That needs a new store binary. If the native OTA contract changed, bump `runtimeVersion` (`"1"` → `"2"`) so old binaries keep their slot.
- JS / copy / API client only, and a Release build already points at their Worker → export, publish, verify.
- They said a TestFlight fence / second slot → publish with `--runtime 2` only if they asked. Never publish runtime 2 onto the store slot.

## Hard rules

1. **Do not rewrite** `ota-worker/src/ota.ts`, `scripts/publish-ota.mjs`, `scripts/verify-ota.mjs`, or `scripts/deploy-ota.mjs`. If something is wrong, copy again from `ral7/expo-worker-ota`. If the files are missing, run init, do not invent replacements.
2. **Never upload** `keys/private-key.pem`. Not to the Worker, R2, GitHub, CI, or wrangler secrets. Signing happens on the laptop. The Worker only serves R2 bytes.
3. **Never commit** `keys/`. `certs/certificate.pem` belongs in git. It is baked into the binary.
4. **Do not publish every commit.** Workers Free is 100k requests/day account-wide. Each update makes every device download the bundle plus assets through the Worker. This is a hotfix hose.
5. **Do not expect Metro to show the update.** Use a Release install. Force-quit, open, force-quit, open. First process downloads. The next process start runs the new JS.
6. **Verify before telling them it worked.** `npm run ota:verify` (or `node scripts/verify-ota.mjs --url <origin>`) must be **204** (nothing published yet) or **200 multipart/mixed**. Never 404.
7. Rollback is a HEAD pointer, not a delete: `npm run ota:rollback`. Old R2 objects stay.

## Commands

```bash
npx github:ral7/expo-worker-ota init                 # from the folder with app.json
npx github:ral7/expo-worker-ota init --name my-ota   # optional worker / bucket name
npx wrangler login
npm run ota:deploy
npm run ota:verify               # 204 until first publish
npx expo export --platform ios --platform android
npm run ota:publish
npm run ota:rollback
```

`publish` and `verify` read `ota.config.json` (written by init/deploy). `--origin`, `--runtime`, `--channel`, `--private-key` override.

## app.json (native contract)

After deploy, `expo.updates.url` must be `https://<worker>.<subdomain>.workers.dev/ota/manifest`. Changing it requires a new binary.

```json
{
  "expo": {
    "runtimeVersion": "1",
    "updates": {
      "enabled": true,
      "url": "https://YOUR-WORKER.workers.dev/ota/manifest",
      "checkAutomatically": "ON_LOAD",
      "fallbackToCacheTimeout": 0,
      "requestHeaders": { "expo-channel-name": "production" },
      "codeSigningCertificate": "./certs/certificate.pem",
      "codeSigningMetadata": { "keyid": "main", "alg": "rsa-v1_5-sha256" }
    }
  }
}
```

If `app.config.js` / `app.config.ts` is in use, do not smash it. Print the snippet and let the user (or a careful edit) add the fields.

## What you must not do

- Implement "Updates v1" from scratch.
- Teach EAS Update as a fallback in the same flow. One path.
- Put the private key in a Worker secret.
- Point `updates.url` at `r2.dev` or a guessed subdomain.
- Bump `runtimeVersion` for a JS-only hotfix.
- Tell them Debug / Expo Go will pick up the publish.
