# app-update

Native update policy: responsibilities, supported exports and extension points.

## Purpose [#purpose]

The pure core compares versions and resolves disabled, up-to-date, recommended and required update states. A hook factory adapts platform-specific fetch/open behavior. Loading and network errors do not force an update wall.

## Public exports [#public-exports]

| Subpath         | Target                 |
| --------------- | ---------------------- |
| `.`             | `./src/index.ts`       |
| `./core`        | `./src/core.ts`        |
| `./create-hook` | `./src/create-hook.ts` |

These paths come from `packages/app-update/package.json`. A `.` entry is the package root. Conditional targets distinguish platform or type resolution; consumers should import the subpath, not its source file.

## The force-update gate [#the-force-update-gate]

An admin sets the gate on `/admin/flags` (card "App update gate", `admin.appUpdate.get|update`). The row is the singleton `app_update_config`; each platform is a jsonb block, so the fields below need no migration. The mobile app reads it through the public `appConfig.get`.

- **Versions.** `minVersion` blocks every install below it (`required`), `recommendedVersion` shows a dismissible banner. Both must be `x.y.z` on save. Parsing stays total on the client: a non-numeric segment reads as 0 and garbage never throws.
- **Build threshold.** `minBuild` is compared with `nativeBuildVersion` only when the installed marketing version equals `minVersion`, so two TestFlight builds of 1.4.0 can be told apart. With `appVersionSource: remote` and `autoIncrement` in `eas.json`, EAS raises the build number on every build: set `minBuild` to the build of the fix, read from the EAS build page.
- **Recommended build.** `recommendedBuild` shows the update banner for lower builds of exactly `recommendedVersion`. If the live store version raises the recommendation, the configured build threshold is dropped.
- **Live store version (iOS).** With "Require the latest App Store version" on, the server looks the app up in the App Store (`itunes.apple.com/lookup`, 3 s timeout, one hour cache per isolate, failures cached five minutes), using the app id in the iOS store URL. The id is read only from an apps.apple.com or itunes.apple.com URL whose last path segment is `id<digits>` (`/us/app/id12345-reader/id98765` names 98765); any other URL names no app and is not looked up. The isolate keeps at most 50 lookups (least recently used out, expired ones dropped). Only settled answers are shared between requests; a lookup in flight is never awaited by another request, because a canceled request would leave it unsettled. A value that is not `x.y.z` is ignored. For `liveGraceHours` after the release (default 72) the new version is only recommended, so a phased release does not block everyone; after that it is the minimum of the answer. The live value is never written to `minVersion`; the admin card shows it read-only. When the live version raises the minimum above the stored one, the stored `minBuild` is dropped from the answer: it belongs to the stored marketing version and would otherwise block the latest store build.
- **Safety rails.** The card validates the fields with the contract's own rules (`isHttpUrl` and `isBlockingStoreUrl` from the flags contract), warns when `minVersion` is above the live store version (nobody could update) and requires an acknowledgement for it, previews what a given version and build would see, and states the effect before saving ("Every install below 1.4.0 on iOS will be blocked."). The live version it compares with is looked up for the store URL being edited (`admin.appUpdate.liveVersion`), so the warning also shows on first setup and after the URL changes; while the lookup runs or when it finds nothing, the card says the version is unknown. If the server refuses a save because it knows a newer live version than the card did (`APP_UPDATE_MINIMUM_UNREACHABLE`, with `data.liveVersion`), the card takes that version, updates the warning and the preview, and keeps the dialog open with the acknowledgement checkbox.
- **Enforced on the server.** With the gate on, a platform whose block can block (a `minVersion`, or iOS live mode) needs the store listing as its `storeUrl` (`INPUT_VALIDATION`), because the Update button opens it and, on iOS, the server reads the listing id from it to compare the minimum with the live version:
  - **iOS:** an http(s) App Store listing on apps.apple.com or itunes.apple.com whose last path segment is `id<digits>`, e.g. `https://apps.apple.com/us/app/my-app/id6754909675`. A support page, a short link or an `itms-apps://` link is refused.
  - **Android:** a Google Play listing with the package in `id=`: `https://play.google.com/store/apps/details?id=com.example.app` or `market://details?id=com.example.app`.

  The contract and the admin form share the rule (`isBlockingStoreUrl`, `appStoreIdFromUrl`, `playStorePackageFromUrl` in the flags contract). While nothing can block, any link is accepted.

  A save whose iOS `minVersion` is above the live App Store version is refused with `APP_UPDATE_MINIMUM_UNREACHABLE` unless it carries `acknowledgeUnreachableMinimum: true`; the lookup runs before the save's transaction, and a failed lookup does not refuse the save. The audit row `app_update_config.updated` keeps `metadata.changes.before` and `.after` (the acknowledgement is not stored, but `metadata.acknowledgedUnreachableMinimum` records it). `before` is read under a row lock in the save's transaction, so two admins saving at once each record the row they replaced.

- **Variants.** Only the production identity is gated. The `.dev` variant from `app.config.ts` and Expo Go never block and never open the production listing. To try the gate, use a production-profile build.
- **Cold start.** The app keeps the last answer in SecureStore and reads it before the first frame: an install already told `required` is blocked at launch and offline. With nothing kept, loading and errors never block. The next answer replaces the kept one, so turning the gate off unblocks on the next foreground. The server's never-500 fallback (its config could not be read) says `update.degraded: true`: it is never kept, and while a kept answer exists it stays in force, so a server outage does not unblock an install already told `required`; the app then polls at the kept answer's interval, not the fallback's. The hook compares answers by value, so a query adapter that returns a new but equal object on every render is fine.
- **Never crash.** `AppUpdateProvider` renders inside the i18n provider and under its own boundary; if the gate throws, the app renders without it.
- **Banner.** Dismissal is stored per recommended version and survives background and relaunch; a newer recommended version shows it again.
- **"Having trouble?"** opens the admin's `supportUrl`, else `EXPO_PUBLIC_SUPPORT_URL`.
- **OTA updates.** `runtimeVersion` follows the `appVersion` policy, so an over-the-air update keeps the binary's version and can never satisfy a forced update. A forced update always needs a new store build.

## How to extend [#how-to-extend]

Keep policy testable in core and platform effects in the adapter. Distinguish an optional recommendation from a required supported-version boundary. Verify malformed and unavailable remote configuration.

Read the [native update policy guide](/docs/features/mobile) for the full flow.

## Key files [#key-files]

- [`packages/app-update/package.json`](https://github.com/CarlosZiegler/fofo/blob/main/packages/app-update/package.json)
