Skip to content

Build your personal knowledge base with Trilium Notes

docs(mobile): document the iOS capacitor:// request-routing gotcha

Elian Doran7ac68d4c9d65
diff --git a/CLAUDE.md b/CLAUDE.md
index 09fceb5716..b4c2247edb 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -169,6 +169,14 @@ Fluent builder pattern: `.child()`, `.class()`, `.css()` chaining with position-
- **Barrel import caution** — `import { x } from "@triliumnext/core"` loads ALL core exports. Early-loading modules like `config.ts` should import specific subpaths (e.g. `@triliumnext/core/src/services/utils/index`) to avoid circular dependencies or initialization ordering issues
- **Electron custom protocol** — In desktop mode, the renderer loads the UI and makes API calls via the `trilium-app://` custom protocol (not HTTP). `apps/desktop/src/protocol.ts` dispatches these requests into the Express app running in the main process; the dispatcher tags them via `apps/server/src/services/electron_request.ts` so auth/CSRF middleware can distinguish them from external TCP traffic
+### Mobile (Capacitor) request routing — Android vs iOS
+
+The mobile app (`apps/mobile/`) wraps the standalone WASM stack in a Capacitor WebView. There is no network backend; the client's API/sync calls (`/api`, `/sync`, `/bootstrap`, `/search`) reach the in-process worker via **two platform-specific request paths**:
+- **Android**: `androidScheme: "https"` works → the app runs at `https://localhost` → the **service worker** (`apps/standalone/src/sw.ts`) routes those requests to the worker.
+- **iOS**: the app runs at **`capacitor://localhost`**, where service workers cannot register, so `apps/standalone/src/main.ts` installs **fetch/XHR/image interceptors** (gated on `location.protocol === "capacitor:"`) instead.
+
+**`iosScheme: "https"` is a no-op on iOS and must not be re-added.** Capacitor rejects it — `CAPInstanceDescriptor.normalize()` checks `WKWebView.handlesURLScheme(scheme) == false`, and WKWebView reserves `http`/`https`, so the scheme is reset to the default `capacitor`. The config line only misleads (it implies an https origin that never exists on iOS). **Do not delete the iOS interceptor path as "dead code"** — it is the only working request path on iOS; a code reviewer assuming `iosScheme:https` ⇒ https origin will wrongly flag it.
+
### Binary Utilities
Use utilities from `packages/trilium-core/src/services/utils/binary.ts` for string/buffer conversions instead of manual `TextEncoder`/`TextDecoder` or `Buffer.from()` calls:
diff --git a/apps/mobile/README.md b/apps/mobile/README.md
index 999dcd5436..5e77dabbcb 100644
--- a/apps/mobile/README.md
+++ b/apps/mobile/README.md
@@ -27,3 +27,14 @@ pnpm --filter @triliumnext/mobile run:android # launch on emulator/device
# or
pnpm --filter @triliumnext/mobile open:android # open Android Studio
```
+
+## How web requests reach the local server (Android vs iOS)
+
+There is **no network backend** — the whole server runs in-process as WASM in a web worker. How the client's API/sync calls (`/api`, `/sync`, `/bootstrap`, `/search`) reach that worker differs by platform, because the two WebViews resolve `*Scheme: "https"` differently:
+
+- **Android** — `androidScheme: "https"` works: the app loads at `https://localhost` (a real secure origin), so the **service worker** ([`apps/standalone/src/sw.ts`](../standalone/src/sw.ts)) intercepts those requests and forwards them to the worker.
+- **iOS** — the app loads at **`capacitor://localhost`** and uses **fetch / XHR / image interceptors** ([`apps/standalone/src/main.ts`](../standalone/src/main.ts), gated on `location.protocol === "capacitor:"`) instead, because a service worker cannot register on `capacitor://` (`navigator.serviceWorker.register()` throws — the scheme is not HTTP/HTTPS).
+
+**Why iOS is on `capacitor://` and not `https`:** Capacitor **ignores** `iosScheme: "https"`. WKWebView reserves the `http`/`https` schemes, so `CAPInstanceDescriptor.normalize()` (`WKWebView.handlesURLScheme("https") == true`) rejects it and resets the scheme to the default `capacitor`. That's why `iosScheme` is intentionally **not** set here — it would be a no-op that falsely implies iOS runs on an https origin.
+
+> ⚠️ **Do not remove the iOS interceptor path as "dead code."** It is the only working request path on iOS. And do not re-add `iosScheme: "https"` — it does nothing on iOS and is misleading.