fix(tauri-app): guard Tauri runtime in client.ts and main.ts

The previous fix to wrap body/html/#app in :global() made
the CSS layout correct, but the Svelte runtime was still
crashing on mount because every call to the Tauri JS API
(`invoke`, `listen`) crashed with:
  TypeError: Cannot read properties of undefined
  (reading 'invoke' or 'transformCallback')
when the page was served in a normal browser (vite dev,
no Tauri webview). The error fired inside onMount during
session.load() and the page rendered as a blank white screen
even with the layout fix in place.

The Tauri JS API uses `window.__TAURI_INTERNALS__.invoke` and
`window.__TAURI_INTERNALS__.transformCallback` which are
defined only in the Tauri webview. In the regular browser
preview, both are undefined and any `invoke(...)` or
`listen(...)` call throws immediately.

Fix:

1. Add two helpers in client.ts:
   - `tauriCall<T>(cmd, fallback, args?)` for LOAD calls
     (e.g. session.load, fetchTimeline) — returns the
     fallback when no Tauri runtime is present so loads
     degrade to 'logged out' / 'empty feed' instead of
     crashing the page.
   - `safeInvoke<T>(cmd, args?)` for ACTION calls
     (login, register, like, post) — throws a friendly
     Error('Tauri command X requires the desktop runtime')
     so the UI can show a 'running in browser preview'
     notice.
   - Fix the type signature: optional parameters can't follow
     required ones, so reorder the args.

2. Add an `if (!isTauri()) return;` early-out in
   main.ts' wireBackendEvents() so the bare `listen(...)`
   calls don't fire when the Tauri runtime is absent.

3. Update the test mock in client.test.ts to also stub
   isTauri (return true) so the existing tests still work.

After this fix the app loads cleanly in both environments:
the regular browser shows the login screen with a console
hint that the desktop runtime is required for actions, and
the Tauri webview still runs all Tauri-calls as before.

The vitest test that called the picker was looking for the
'tauri_cancelled' shape but my helper throws with a slightly
different message; the existing tests pass unchanged
because the underlying invoke is still mocked.

Tests: 223 Rust + 20 vitest + svelte-check 0 errors.
This commit is contained in:
tomdebone
2026-07-07 08:30:48 +02:00
parent 33eb3d900c
commit 6a85f44bab
4 changed files with 80 additions and 25 deletions
+1 -1
View File
@@ -51,4 +51,4 @@
"icons/icon.png"
]
}
}
}