---
title: "Recipe: work that runs while the app is unseen"
description: "@exact/background (LLP 1133.017) has Background Sync's and Periodic Background Sync's names."
modificationDate: "2026-11-12"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: work that runs while the app is unseen

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/background-work: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

`@exact/background` (LLP 1133.017) has Background Sync's and Periodic
Background Sync's names. The app registers a tag; the platform later runs the
root's `sync` or `periodicsync` handler in a **wake**: a session that may have
no window, with a 30 s budget. Nothing is shown to the person while it runs.

```prompt Run work while the app is unseen
Make the Exact app in this folder run work while it is unseen: a one-time sync, a periodic sync, or both. Use https://github.com/expo/exact/blob/main/docs/recipes/background-work.md as the source for each step. The Exact checkout has the same page at docs/recipes/background-work.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me. Find the Exact checkout (the folder with scripts/exact.mjs); if you cannot find it, ask me where it is.
2. If I did not say which work to run, propose it and ask me. A `sync` runs once at the next chance, which on an iPhone can be hours later. A `periodicsync` runs no sooner than its `minInterval` (at least 15 minutes), and never on the web.
3. Install the package: `bun <exact2>/scripts/exact.mjs add background .`. In app.ts, declare the grant lines bare, with no purpose (`device.background-sync`, `device.periodic-background-sync`).
4. Register the tag from data code or an action. On the root, handle `sync` and `periodicsync` by sending to data code. The work must settle in 30 s, and it may not open system UI, ask the person or play sound. To retry, register the tag again from the handler.
5. If the app has a switch for periodic work, show it only while `background.periodic` is true.
6. Add tests in app.test.contract that run each registration with `tap @<ref>` from `state.backgroundSync` and check what the work did, and that a tap while offline (`prefer online false`) is refused. Harmony's `packages/background/test/harmony.mjs` in the Exact checkout is a whole example.
7. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json until they pass. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 8) and report it; do not keep trying.
8. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
9. Tell me each tag, when it runs on each platform, and what the tests and verify reported.
```

1. **Declare** the grant lines, bare (no purpose: no platform shows one):

   ```ts
   export const grants = 'device.background-sync\ndevice.periodic-background-sync';
   ```

2. **Register** from data code or an action. A `sync` fires once, at the next
   chance the platform gives the app to run online: on the web while the page
   is open, on a Mac while it runs, on an iPhone at the next refresh or
   processing wake or launch, which can be hours.
   A `periodicsync` fires no sooner than `minInterval` (at least 15 minutes)
   after its last run, when the platform chooses, and never on the web.

   ```ts
   import { registerSync } from '@exact/background';
   if (!round.ok) await registerSync({ tag: `outbox:${persona}` });
   ```

3. **Handle** on the root. The handler sends to data code; the wake ends when
   that work settles, or expires at 30 s, the work then settling as the app's.
   A handler that registers its own tag again retries it after a backoff (5
   minutes, doubling to an hour; `lastChance` is true on the third attempt).

```text
use registerPeriodicSync, unregisterPeriodicSync, backgroundSyncStatus, periodicSyncTags, sync, periodicsync, BackgroundSyncStatus, SyncEvent, SyncRegistration from "@exact/background"

shape Flushed
  sent: bool

component App
  resource background = backgroundSyncStatus() as shape BackgroundSyncStatus
  resource catchUps = periodicSyncTags() as shape SyncRegistration[]
  mutation flushed as shape Flushed
  action outboxDue(tag: string, e: SyncEvent)
    send flushed = flushOutbox(tag)
  action caughtUp(tag: string)
    send flushed = catchUp(tag)
  action setCatchUp(on: bool)
    if on
      registerPeriodicSync(tag="catch-up", minInterval=1800000)
    else
      unregisterPeriodicSync(tag="catch-up")
  view
    main sync=outboxDue periodicsync=caughtUp
      if background.periodic
        input type="checkbox" switch checked=(catchUps.length > 0) change=setCatchUp aria-label="Check for mentions"
```

A wake may not open system UI, ask the person or play sound (`share`,
`openURL`, the pickers, …): each is refused into the journal. No timer fires
in a wake with no window or a hidden one. A quit Mac app has no wake:
`"keepRunning": true` under `host.macos.window` (on Windows,
`host.windows.window`) keeps it running.

**Driving it:** `state.backgroundSync` lists each registration (`ref`,
`kind`, `due`, `lastRun`); `tap @<ref>` runs it as a warm wake (refused `not
due` early, `offline` while `prefer online false`); `state.wake` shows its
outcome; `--wake '<entry>'` launches the app cold for one.

Transfers the system finishes: `contract vocab background-transfers`.
