Build your personal knowledge base with Trilium Notes
name: developing-capacitor-mobile
description: Use when working on the Trilium mobile app (apps/mobile, Capacitor for Android/iOS) or on the standalone code paths that only run inside it — request routing on capacitor:// vs https://localhost, the native request bridges on both platforms (TriliumRequestBridge in Java and Swift, native-request-bridge.ts, which replace the service worker there; the iOS one re-types Capacitor's capacitor:// scheme handler), the Android native streaming HTTP proxy (TriliumWebViewClient), the NativeHttpHandler sync transport, downloads and exports (the WebView saves none itself — capacitor_download.ts writes them and opens the share sheet), MainActivity/ViewController WebView tweaks (edge-to-edge, keyboard), isMobileApp() gating in the client, adding a Capacitor plugin, or the mobile CI/nightly builds. Also load it when reviewing a diff that touches these files, so working iOS-only code is not flagged as dead.
Developing the Capacitor mobile app
apps/mobile is a Capacitor shell around the standalone build: it ships no web assets of its own — webDir in capacitor.config.json is ../standalone/dist, and the entire Trilium server runs in-process as WASM in a web worker (apps/standalone). There is no network backend inside the app; "the server" is apps/standalone/src/local-server-worker.ts. Most mobile work therefore lands in apps/standalone/src/, with the native projects (apps/mobile/android/, apps/mobile/ios/) touched only for WebView behaviour.
Key files:
apps/mobile/capacitor.config.json # appId org.triliumnotes.trilium, androidScheme https, hostname localhost, plugins
apps/mobile/android/app/src/main/java/org/triliumnotes/trilium/
MainActivity.java # installs TriliumRequestBridge, TriliumWebViewClient, TriliumFileSink; edge-to-edge + insets, system bars
TriliumRequestBridge.java # Android adapter for LocalRequestBroker: message listener, evaluateJavascript, page reloads
LocalRequestBroker.java # local API request logic in place of a service worker; no Android classes, unit-tested
NativeHttpProxy.java # the sync worker's streaming HTTP proxy; no Android classes, unit-tested
InterceptedResponse.java # Android-free form of WebResourceResponse, returned by both
TriliumWebViewClient.java # routes requests to TriliumRequestBridge and NativeHttpProxy, builds the WebResourceResponse
TriliumFileSink.java # binary file-write channel (WebMessageListener + ArrayBuffer, Android only)
apps/mobile/ios/App/App/ViewController.swift # re-types the capacitor:// scheme handler; keyboard handling, --tn-keyboard-gap
apps/mobile/ios/App/App/TriliumRequestBridge.swift # WebKit adapter: TriliumAssetHandler, script message handler, WebKitSchemeTask
apps/mobile/ios/RequestBroker/ # Swift package: RequestBroker.swift (request logic, no WebKit; the App target compiles it) + its tests
apps/standalone/src/main.ts # boot: native HTTP handler and native request bridge if `Capacitor` in window, else the SW
apps/standalone/src/native-request-bridge.ts # page side of both TriliumRequestBridges: localFetch() → head + chunks
apps/standalone/src/services/capacitor_http_handler.ts # NativeHttpHandler: Android proxy probe + CapacitorHttp fallback
apps/standalone/src/services/capacitor_download.ts # saveUrlToDevice(): fetch → Filesystem cache → share sheet
apps/standalone/src/local-bridge.ts # LOCAL_API_PREFIXES, localFetch(), registerNativeHttpHandler()
apps/standalone/src/sw.ts # service worker routing (web; Android only on a WebView without message listeners)
apps/client/src/services/utils.ts # isMobileApp() — running inside the native wrapper
.github/workflows/mobile.yml, .github/actions/build-mobile # APK + iOS simulator builds, nightly signing
Inbound: how the client's API calls reach the in-app worker
The client's /api, /sync, /bootstrap, /search requests (LOCAL_API_PREFIXES in local-bridge.ts) must be answered by the worker.
Most of them never leave the page. The shell is a single WebView, so its one tab always wins the database lock, and a tab that owns the worker publishes localFetch on window.standaloneApi; the client's ajax() and setupGlob()'s /bootstrap call it directly rather than issuing a request (see the standalone skill, "Request flow" step 3). That covers everything routed through apps/client/src/services/server.ts, on both platforms alike.
What still leaves the page — engine-initiated loads (<img src="api/images/…">, @font-face, themes, media, iframes such as the PDF viewer) — goes to a native request bridge on both platforms; neither registers a service worker. Uploads stay in the page too: uploadForm() and the editor's image upload adapter (packages/ckeditor5/src/plugins/uploadimage.ts, via glob.getLocalUpload()) hand the File to standaloneApi.localUpload. So does AI chat: on standalone streamChatCompletion() posts llm-chat/stream-start through server.post and receives the reply over the WebSocket channel, never the SSE route. The native side sees every request the engine issues, whichever subsystem issued it, and hands local API paths to the page: it calls window.__triliumNativeRequest(), native-request-bridge.ts answers from localFetch(), and the reply comes back as JSON strings: a head, the body in chunks, then end/error. Where the platforms differ is how the request is caught and how the body travels:
- Android (
https://localhost) —TriliumWebViewClient.shouldInterceptRequesthands local paths toTriliumRequestBridge.java(logic inLocalRequestBroker). Replies come over the injectedwindow.triliumRequestBridgeWebMessageListener, the body as ArrayBuffer frames (4-byte request id + bytes; base64chunkmessages on a WebView without ArrayBuffer messages). The IO thread blocks until the head arrives (270 s cap, as insw.ts), then the body streams through a queue-backedInputStream. A WebView withoutWEB_MESSAGE_LISTENERnever gets the object, soinstallNativeRequestBridge()returns false and the page falls back to the service worker. - iOS (
capacitor://localhost, where WebKit refuses a service worker) —ViewController.webView(with:configuration:)re-types Capacitor's registeredWebViewAssetHandlertoTriliumAssetHandlerwithobject_setClass(WebKit raises on registering a second handler for a scheme, and Capacitor'sloadView()is not overridable), which hands local paths toTriliumRequestBridge.swift. Replies come over thewindow.webkit.messageHandlers.triliumRequestBridgescript message handler, the body always in base64 chunks (script messages cannot carry ArrayBuffers). Nothing blocks: theWKURLSchemeTaskis completed bydidReceive(response),didReceive(data)per chunk anddidFinish(). A main-document request through the handler counts as a page start and fails whatever the previous page left unanswered.
Consequences that keep tripping people up:
iosScheme: "https"is a no-op — do not re-add it. Capacitor rejects it:CAPInstanceDescriptor.normalize()checksWKWebView.handlesURLScheme(scheme) == false, and WKWebView reserveshttp/https, so the scheme resets tocapacitor. The config line only implies an https origin that never exists on iOS.TriliumAssetHandlermust not declare stored properties. The instance it takes over was allocated as aWebViewAssetHandler, so subclass storage would be uninitialized; state lives inTriliumRequestBridge.shared. A stoppedWKURLSchemeTaskmust never be completed (it raises), which is whystopremoves the task from the pending map at once.- Both bridges answer GET and HEAD only. Android's
shouldInterceptRequestexposes no request body, and iOS follows suit (its scheme task could readhttpBody) so the platforms stay alike; any other method gets a 405 withx-trilium-bridge-error. A request with a body has to go throughstandaloneApi.localFetch(asserver.ts'sajax()does) or, for a multipart upload,standaloneApi.localUpload(asuploadForm()does forupload()and imports); a rawfetch/$.ajaxPOST toapi/fails on both platforms. - Neither native response carries a 3xx (
WebResourceResponserejects one, a scheme task cannot redirect), sonative-request-bridge.tsturns a redirect (the share pages issue them) into a 200 page with a<meta http-equiv="refresh">. It also setscontent-lengthto the exact body length (except for HEAD), which WebKit's media loader needs. - Android WebView applies a request's
Rangeto the intercepted stream itself. Chromium'sAndroidStreamReaderURLLoadertreats theInputStreamas the whole resource and skipsfirst_byte_positionbytes before reading, while keeping our status and headers. The worker has already cut the206slice, soLocalRequestBrokerreads the start from itsContent-RangeandChunkStream.skip()passes over that many bytes without reading. Without it every range past byte 0 lands in the wrong place or runs off the end (ERR_REQUEST_RANGE_NOT_SATISFIABLE), which breaks seeking and any MP4 whosemoovindex sits at the end. Keepavailable()at 0: that is what makes Chromium skip its own bounds check. - Standalone caps
bytes=N-media responses at 8 MiB (maxOpenRangeLengthinserveContentWithRanges(), set byopenPartialInt()whengetPlatform().isStandalone), because each response is buffered whole on the page and copied through the bridge. The element asks for the next range on its own; ranges with an explicit end are served as asked. - Three prefix lists must agree:
isLocalApiRequest()inlocal-bridge.ts,LocalRequestBroker.isLocalPath()in Java andisLocalApiPath()in Swift, including the/sharerule. - A service worker registered by an older Android build still controls the page. Its fetches never reach
shouldInterceptRequest, and with noattachServiceWorkerBridge()nothing answers them — a custom theme silently stops applying (#12170), images hang.removeStaleServiceWorkers()inmain.tsunregisters it once the native bridge is installed and reloads if it was controlling. Never advise clearing the app's data: that deletes the OPFS database. sw.tshas a defensiveself.location.protocol === "capacitor:"guard and must let/_trilium_native_http/requests fall through to the WebView (see below); keep both when editing its fetch handler.
Outbound: how the worker syncs with a remote server
A fetch from the app origin to a sync server is cross-origin, so CORS and cookie rules apply and large bodies cost bridge copies; instead local-bridge.ts exposes registerNativeHttpHandler(): when a handler is registered (only inside Capacitor — main.ts checks "Capacitor" in window), the worker's BridgedRequestProvider posts HTTP_REQUEST messages to the page and the handler does the real HTTP call. capacitor_http_handler.ts is that handler:
- Android — probes
GET /_trilium_native_http/pingonce; ifTriliumWebViewClientanswers with thex-trilium-native-httpmarker, GET/HEAD requests go through the streaming same-origin proxy (/_trilium_native_http/fetch?url=…, request headers tunnelled asx-trilium-h-<name>, upstreamSet-Cookiere-exposed asx-trilium-set-cookie, proxy failures → 502 +x-trilium-proxy-error). Answered fromWebViewClient.shouldInterceptRequest, so the body streams into the page with no bridge envelope, no full-body Java string, no base64 — the plugin transport measured ~60 % of a core and ~2 MB/s during an initial sync. A failed probe is retried after 15 s because an old service worker can still own fetches right after an update. - Everything else (POSTs, binary responses, and all of iOS) uses the stock
CapacitorHttpplugin, reached via the globalwindow.Capacitor.Plugins— notimport "@capacitor/core", since bare specifiers don't resolve in the browser's native module loader. - Responses hand parsed JSON through
dataand only non-JSON throughbody; the handler must notJSON.stringify— the extra string copy OOM-ed the iOS worker on large blobs. Preserve that contract when touching either side. - iOS has no
shouldInterceptRequestequivalent for https, so it stays on the plugin transport; the geo map's tile referer workaround (apps/client/src/widgets/collections/geomap/map.tsx) has the same limitation.
Downloads: the WebView has no download manager
A navigation whose response carries Content-Disposition: attachment is handed to
WebView.setDownloadListener on Android, and nothing registers one — not Capacitor, not
MainActivity. The response is dropped with no console output and no error, so an export "succeeds"
(the task's websocket taskSucceeded still fires the toast) while no file ever appears. On iOS the
same window.location.href is worse: it is a top-level navigation, so it takes the app out of the
SPA.
Registering a native DownloadListener does not fix it. The listener receives only a URL, and a
native re-request of https://localhost/api/… goes to the real network stack, which
shouldInterceptRequest never sees and where no server exists.
So the page does the saving: open.download() (apps/client/src/services/open.ts) routes through
window.standaloneApi.save.saveUrl() when that exists, which main.ts defines only inside the shell.
capacitor_download.ts fetches the URL — still routed to the worker by the native request bridge —
writes it into Directory.Cache and hands the file to the system share sheet.
- On Android the bytes ride
TriliumFileSink, not the plugin bridge. Every plugin call crosses as base64 inside a JSON string thatMessageHandlerre-parses whole withorg.json— ~13 MB/s no matter the chunk size. The sink is aWebViewCompat.addWebMessageListenerobject (window.triliumFileSink) carrying rawArrayBuffers: the page resolves the absolute path via the Filesystem plugin's own mapping (writeFile("")+getUri), then streamsopen/chunks/close, each step acknowledged. The base64 plugin path stays as the fallback — iOS (messageHandlerscannot carry ArrayBuffers), WebViews withoutWEB_MESSAGE_ARRAY_BUFFER, and the sink-busy case. - Fallback chunks must be a multiple of 3 bytes. The Filesystem plugin takes base64, and base64
pads any group narrower than three bytes; a padded group mid-file decodes to the wrong bytes.
rechunk()incapacitor_download.tsowns that alignment (and bounds sink messages); the encode uses nativeUint8Array.prototype.toBase64when the runtime has it. - Plugins come from
Capacitor.registerPlugin(name), not a@capacitor/*import.Capacitor.Pluginsholds only what the injected runtime registered (CapacitorHttpand the rest of core); the@capacitor/*packages exist socap syncwires the native code in. - A shared file cannot be deleted after
share()resolves — the receiving app reads the URI on its own schedule (a Drive upload can outlive the sheet by minutes). Each download therefore lands in a run folder of its own, and a save prunes all previous runs except the newest, giving the last share one save's grace. - Every save writes
<name>.partand renames into place on success. A stream can die mid-way —DatabaseChangedErrorfires if any write lands during a backup — and truncating the final name first would turn the previous good backup into a partial file. - Android needs no manifest change:
file_paths.xmlalready exposes<cache-path path="."/>to the${applicationId}.fileproviderthe Share plugin looks up, and the cache directory is not external storage, so no permission prompt. - Adding a plugin means three edits:
apps/mobile/package.json,includePluginsincapacitor.config.json(iOS only builds what is listed), andcap update androidto regenerate the trackedcapacitor.settings.gradle/app/capacitor.build.gradle. iOS'sCapApp-SPM/Package.swiftis gitignored and regenerated by CI. - The known cost profile (1.8 GB backup on a mid-range phone, Sept 2026): ~16s, split evenly
between SQLite page reads and the container's WASM SHA-256 (
hash-wasminpackages/trilium-backup-container/src/backend-web.ts); the sink's writes are not a factor. The base64 plugin bridge caps around 13 MB/s (MessageHandlerre-parses each call's JSON whole withorg.json), which is why the sink exists. - The database backup takes the same last step, from a different source.
saveDatabase()inlocal-bridge.tsconsumes the worker'sBACKUP_STREAMchannel in the page rather than relaying the port to the service worker, so no SW is involved (which is also why it works on iOS), andsetBackupPingingstays off — that keepalive exists only for a stream the SW is holding open. Two things differ from a download: it goes toDirectory.DocumentsunderTrilium/, not the cache, and nothing there is ever pruned — a repeated name replaces the file only via the.partswap. A dismissed share sheet isdone, notcancelled: the file is complete before the sheet opens. rechunk()is what makes any source safe to write. A producer picks its chunk sizes for its own reasons — a response body by packet, the backup by database page — and neither is 3-aligned.
Native shells
- Android
MainActivity: installsTriliumRequestBridgeand setsTriliumWebViewClient, draws edge-to-edge with transparent system bars, forwards window insets to the WebView (so the client can pad for the status/navigation bars) and re-applies system bar appearance on configuration change. Nightly and debug builds get a distinctapplicationIdsuffix (.nightly,.debug) and launcher icon so they install side by side (android/app/build.gradle). - iOS
ViewController: the layout isbody { position: fixed; height: 100vh }with an inner scrolling container, so WKWebView's reflexive scroll-to-focused-element would drag the toolbar off-screen; the controller pins the outer scroll offset while the keyboard animates and samples the keyboard's top edge every frame into the--tn-keyboard-gapCSS variable so the editor toolbar follows an interactive swipe-dismiss.Keyboard.resize: "native"in the config is part of the same contract. Becausenativeshrinks the WebView (the controller's wholeview) to the keyboard's top, the area behind the keyboard is theUIWindow, whose default background is black and shows through the keyboard's rounded corners;Keyboard.autoBackdropColor: "dom"makes the plugin repaint the window withbody's computed background on everykeyboardWillShow. Change the keyboard/toolbar CSS on the client and this controller together. limitsNavigationsToAppBoundDomains: trueon iOS.
Safe-area insets: never write a bare env(safe-area-inset-*)
Android's WebView does not populate env(safe-area-inset-*) — it resolves to 0, silently, on every
Android version. MainActivity.forwardInsetsToWebView() works around it by setting
--safe-area-inset-top/-bottom/-left/-right (plus --keyboard-height) on
document.documentElement from the real WindowInsetsCompat values, in CSS pixels, on every inset
change.
So client CSS must read the variable with env() only as the fallback:
padding-bottom: var(--safe-area-inset-bottom, env(safe-area-inset-bottom));
The fallback is what keeps iOS and desktop browsers right, where the variable is never injected and
env() is authoritative. Get the two axes matching — var(--safe-area-inset-left, env(safe-area-inset-left)),
never a -left var falling back to env(…-right).
- Bare
env()is a bug on Android, not a style nit. It had gone unnoticed at 27 call sites (fixed ina8f4e9f405), including--mobile-bottom-offset, which puts the mobile launcher bar under the gesture pill. - The one deliberate exception is the
body.ios--mobile-bottom-offsetoverride inapps/client/src/stylesheets/style.css— platform-scoped to whereenv()is the real source. body.desktoprules are in scope too. An Android tablet WebView has noMobiin its UA, soisMobile()is false andindex.tspicks the desktop layout for it.--keyboard-heightis injected but read by nothing.MainActivityresizes the WebView throughbottomMargininstead, so the CSS viewport shrinks on its own. iOS drives--tn-keyboard-gapfromViewControllerfor a different purpose; the two are not a pair.
Audit with grep -rn "env(safe-area" --include=*.css apps/client/src apps/standalone/src — every hit
should be wrapped in a matching var().
Client-side gating
isMobileApp()(apps/client/src/services/utils.ts) —window.Capacitor?.isNativePlatform?.(): true only inside the native wrapper. Distinct fromisMobile(), which is the layout choice and is also true for a phone browser. Use the former for "there is a native shell" behaviour (e.g. setup flow), the latter for responsive UI.window.Capacitoris typed inapps/client/src/types.d.ts.- There is no Node, no server process and no
apps/servercode at runtime — anything the mobile app needs from "the backend" is core (packages/trilium-core), which is why core carries the no-Node-built-ins rules.
Building and running
pnpm --filter @triliumnext/mobile build # = standalone build → apps/standalone/dist
pnpm --filter @triliumnext/mobile sync # build + `cap sync` (copies dist into android/ and ios/)
pnpm --filter @triliumnext/mobile run:android # emulator/device (needs ANDROID_HOME, JDK 17+)
pnpm --filter @triliumnext/mobile open:android # Android Studio
pnpm --filter @triliumnext/mobile run:ios | open:ios # Xcode (macOS)
CI: mobile.yml (pull requests) builds a debug APK via .github/actions/build-mobile and an unsigned iOS Simulator .app on macOS; nightly.yml calls the same action with nightly: "true" for the signed assembleRelease build under the .nightly app id. The action runs the Android JVM unit tests before assembling, and mobile.yml uploads their JaCoCo report to Codecov under the mobile flag; a separate Ubuntu job runs swift test on apps/mobile/ios/RequestBroker; the JS unit tests live in the standalone suite.
Debugging on a device: a release-type build (what nightly.yml produces) suppresses WebView
console→logcat forwarding, so anything logged from JS is invisible to adb logcat and log-based
detection of what the app is doing goes blind. android.util.Log calls from the native side still
come through, so instrument the Java layer — or install a debug build — when you need to see what
is happening.
Testing
Everything JS-side is under the standalone Vitest suite (happy-dom + real sqlite-wasm):
pnpm --filter standalone test capacitor_http_handler # Android proxy probe / plugin fallback
pnpm --filter standalone test capacitor_download # chunked base64 write, filename parsing, share sheet
pnpm --filter standalone test sw # service-worker routing incl. capacitor:// guard
pnpm --filter standalone test native-request-bridge # both platforms' request bridge: channels, framing, base64, redirects
pnpm --filter standalone test main # boot wiring (native HTTP handler, native request bridge or the SW)
Simulate the platform in a spec by stubbing location.protocol / window.Capacitor (getPlatform, isNativePlatform, Plugins.CapacitorHttp) — see the existing specs for the fixtures.
The Android Java has JVM unit tests (JUnit 4) under apps/mobile/android/app/src/test/:
cd apps/mobile/android
./gradlew testDebugUnitTest # needs a JDK 21 and ANDROID_HOME
./gradlew testDebugUnitTest createDebugUnitTestCoverageReport # + app/build/reports/coverage/test/debug/
The project needs cap sync android (or cap update android) once first, for the generated capacitor-cordova-android-plugins/.
- Only Android-free classes are testable. Unit tests compile against
android.jar, whose classes (Uri,Handler,Log,Base64,WebResourceResponse) are stubs that throw "Method not mocked". Logic therefore lives in classes that import none of them (LocalRequestBroker,NativeHttpProxy,InterceptedResponse), with the WebView, the main thread, base64 and logging passed in; theTrilium*classes andMainActivityare thin adapters with no tests, and the request-path ones (TriliumRequestBridge,TriliumWebViewClient,MainActivity) are listed underignore:incodecov.ymlso the patch check does not count them. Keep new native logic on the same side of that line, and a new adapter goes on that list. org.jsoncomes from atestImplementationdependency, sinceandroid.jar's copy is a stub too.com.sun.net.httpserveris not on the test classpath, soNativeHttpProxyTestserves canned responses from aServerSocket(StubServer).minSdkis 24:String.joinandjava.util.Base64need API 26, so production code avoids them; tests can use them.
Client behavior that differs on the device itself (the keyboard, the visual viewport, touch scrolling) can't be reproduced by these suites or by Playwright. When the device's browser shows it against pnpm server:start, log readings into the dev server's log and replay them: the building-client-ui skill, references/measuring-on-a-device.md.
The iOS request logic has XCTest tests in a Swift package, apps/mobile/ios/RequestBroker/:
cd apps/mobile/ios/RequestBroker
swift test # macOS, or Linux with the official swift.org toolchain
- The App target compiles the package's source.
RequestBroker.swiftis referenced fromproject.pbxprojby relative path (../../RequestBroker/Sources/RequestBroker/RequestBroker.swift), not imported as a package, so it lands in the App module next toTriliumRequestBridge.swift. The package sits outsideApp/so Xcode andcap syncnever see itsPackage.swift. - Same rule as Android: logic stays out of the WebKit side.
RequestBrokerimports only Foundation (plusFoundationNetworkingon Linux, forHTTPURLResponse) and talks to aSchemeTaskprotocol; script evaluation, scheduling and logging are passed in.TriliumRequestBridge.swiftkeeps only what needs WebKit and has no tests. - nixpkgs' Swift cannot run
swift testas is. It lackslibIndexStore.so, which Linux test discovery reads, and its manifest runner does not findlibdispatch.so. Copy the package elsewhere, add aTests/LinuxMain.swiftcallingXCTMainwith a hand-writtenallTestslist, and runswift test --disable-index-storeinnix-shell -p swift swiftpm swiftPackages.{Foundation,XCTest,Dispatch}withLD_LIBRARY_PATHpointing at those packages' libraries. Do not commit aLinuxMain.swift: CI's toolchain discovers tests itself.