# Making a game playable offline

This guide shows how to make your game eligible for **Save for offline** and how to design it for play with no connection. Players can save a game to their device and play it with no internet connection: on a train, on a plane, or on a flaky phone signal. The option appears in the **More** menu of the game bar as **Save for offline**, and saved games are listed on the player's **Offline games** page. Unfamiliar terms (build, active build, origin) are in the [glossary](https://docs.zokogames.com/getting-started/glossary).

Nothing is installed. The browser stores your build's files for the game's own origin, and our player starts the game from that copy when the network is not there.

## Turning it on

Open your game in the developer console and, under **Video and multiplayer extras**, tick **Allow save for offline**. The option only reaches players when the **active build** also passes every check below. Admins see the opt-in, and anything blocking it, in the review view.

| Check | Requirement |
| --- | --- |
| Hosting | The game is hosted by ZokoGames (an uploaded build, not your own server) |
| Build size | 60 MB or less, as stored (pre-compressed `.br` / `.gz` files count at their compressed size) |
| Multiplayer | The game is not listed as multiplayer |
| External hosts | The build validator found no references to hosts outside the platform |
| Upload date | The build was uploaded after offline support shipped (older builds have no file list: upload the build again) |

When you publish a new build, players who saved the game get it automatically the next time they open the game page online. The old copy is deleted only after the new one has downloaded completely.

## What gets saved

Every file of the build, exactly as uploaded, plus the platform files your build loads from outside its folder:

- the SDK script from `https://sdk.zokogames.com/...`
- per-game helpers under `/sdk/` and `/_shared/`

Files are checked against the size and SHA-256 recorded when you uploaded the build. Anything the build fetches from elsewhere at runtime is **not** saved, which is why external hosts make a build ineligible.

Keep every asset inside the zip and load it with relative URLs (`assets/level1.json`, not `/assets/level1.json` or a CDN URL). Files the game builds at runtime, such as `blob:` URLs, are fine.

## How the SDK behaves offline

| Feature | Offline behaviour |
| --- | --- |
| `init` / handshake | Answers at once from local state; it never waits for the network |
| Player | The last player signed in on this device, or a guest |
| Data module (cloud saves) | Reads and writes the device copy; changes upload when the connection returns, with the usual conflict check against saves made on another device |
| Video ads (`requestAd`) | Fail straight away with `unfilled`: carry on as you would with no fill |
| Banners | Not shown |
| Leaderboard scores (`submitScore`) | Resolves without recording. Offline, the score is not lost if you also pass `delayed`: a copy sealed at the end of the run with `earnedAt = Date.now()`, additional data `zoko-score-v3|<userId>|<gameId>|<earnedAt>` and plaintext `<score>|<earnedAt>`. The portal keeps it on the device for a signed-in player (keyed boards only, never in kids mode) and sends it when they are back online, within 72 hours. It counts for the all-time board only, not the weekly board. Without `delayed`, or on a portal older than SDK 1.6.4, an offline score is not queued. On a game with a leaderboard (never in kids mode), the offline notice over the game tells the player: **Scores are not saved while you're offline.** See [Offline scores](https://docs.zokogames.com/sdk/leaderboards#offline-scores). |
| Play events (`gameplayStart`, `trackOrder`) | Not recorded; nothing is queued for later |
| Friends, invites, purchases | Unavailable; calls fail with their normal network errors |

To react to the connection in your own code, use the SDK (1.2.0 and later). The portal keeps the flag current when the browser goes offline or back online, and sets it from the start when the game is launched from the **Offline games** library:

```js
const SDK = window.ZokoGames.SDK;
await SDK.init();

function onNetwork(offline) {
  // hide your "global leaderboard" button, show a small offline badge, ...
}
onNetwork(SDK.game.isOffline);
SDK.game.addOfflineListener(onNetwork); // receives the new boolean on every change
// SDK.game.removeOfflineListener(onNetwork);
```

In local mode (a game opened outside the portal) `isOffline` mirrors `navigator.onLine`, and `?offline=true` on the page URL pins it to `true` so you can test the offline paths without touching your network. On a portal that does not send the flag, `isOffline` stays `false`.

**Older SDK versions** (before 1.2.0) have no such members. Fall back to the browser's own state, which is accurate inside the game frame:

```js
function onNetwork() {
  const offline = !navigator.onLine;
}
window.addEventListener("online", onNetwork);
window.addEventListener("offline", onNetwork);
onNetwork();
```

To support both, use `SDK.game.addOfflineListener ? SDK.game.isOffline : !navigator.onLine`, and register the `online` / `offline` listeners only in the second case.

## Designing for offline play

- **Treat ads as optional.** A rewarded ad that fails with `unfilled` should not block progress. Offer the reward another way, or skip it.
- **Save small and often.** The data module keeps the device copy, and the upload queue survives a closed tab. See [Cloud saves in practice](https://docs.zokogames.com/guides/cloud-saves).
- **Do not require a fresh download on start.** Version checks, remote config and analytics beacons must time out quietly.
- **Test it.** Once the game is live (basic launch or later), open its page on the portal, choose **Save for offline** in the **More** menu and wait for it to finish. Then open your browser's developer tools, switch the network to *Offline* and reload the page. The game must reach its first frame, and you should see it listed on your **Offline games** page. To test your offline code paths without the portal, run the game on `localhost` with `?offline=true` (see above); that pins `SDK.game.isOffline` to `true` but does not test saving.
- **Ad-blocked and guest players** behave as online ones do: guests get the guest save on the device, and a blocker changes nothing here because offline ads fail with `unfilled` anyway.

## Storage and removal

Players see the total space used on the **Offline games** page, and can remove one game or all of them. Browsers may also clear saved games when the device runs low on storage. The game then plays online again until the player saves it once more. In kids mode only games made for kids can be saved.

## Next steps

- [Cloud saves in practice](https://docs.zokogames.com/guides/cloud-saves)
- [Getting to the first frame fast](https://docs.zokogames.com/guides/first-frame)
