Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(tauri): restore network requests in the native apps (#9506)

Peter Chinmanc28ea4f9f957
diff --git a/apps/web/src/index.tsx b/apps/web/src/index.tsx
index a7789d54c2..b34a5e7c12 100644
--- a/apps/web/src/index.tsx
+++ b/apps/web/src/index.tsx
@@ -8,8 +8,8 @@ import '@fontsource-variable/playfair-display';
// import 'solid-devtools';
import { initializeLexical } from '@core/component/LexicalMarkdown/init';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
-import { getPlatform, isTauri } from '@core/util/platform';
-import { platformFetch } from '@core/util/platformFetch';
+import { getPlatform } from '@core/util/platform';
+import { installPlatformFetch } from '@core/util/platformFetch';
import { recoverFromFailedLoad } from '@core/util/reloadForNewerBuild';
import { Telemetry } from '@macro-inc/observability';
import { initMonochromeIcons } from '@ui/utils/monochromeIcons';
@@ -22,37 +22,7 @@ import { Root } from './routes/Root';
perfMark('boot.entry-evaluated');
-// Third-party APIs that allow any origin and require requests from the user's
-// browser. The native HTTP client's scope doesn't list them and rejects them.
-const WEBVIEW_FETCH_HOSTS = new Set(['api.klipy.com']);
-
-// Override global fetch with platformFetch for Tauri compatibility
-// Skip localhost requests (dev server) to avoid breaking HMR
-if (isTauri()) {
- const originalFetch = window.fetch;
- window.fetch = new Proxy(originalFetch, {
- apply: (target, thisArg, args) => {
- const url = args[0];
- // Resolve bundled asset paths before checking the existing localhost bypass.
- let resolvedUrl: URL;
- try {
- resolvedUrl = new URL(
- url instanceof Request ? url.url : String(url),
- document.baseURI
- );
- } catch (error) {
- return Promise.reject(error);
- }
- if (
- resolvedUrl.href.includes('localhost') ||
- WEBVIEW_FETCH_HOSTS.has(resolvedUrl.hostname)
- ) {
- return target.apply(thisArg, args as Parameters<typeof fetch>);
- }
- return platformFetch.apply(thisArg, args as Parameters<typeof fetch>);
- },
- });
-}
+installPlatformFetch();
initializeLexical();
initMonochromeIcons();
diff --git a/apps/web/src/lib/core/util/platformFetch.test.ts b/apps/web/src/lib/core/util/platformFetch.test.ts
index 4b6c88e0d5..99a1881d7a 100644
--- a/apps/web/src/lib/core/util/platformFetch.test.ts
+++ b/apps/web/src/lib/core/util/platformFetch.test.ts
@@ -15,6 +15,7 @@ vi.mock('@tauri-apps/plugin-http', () => ({ fetch: nativeFetch }));
import {
createPlatformFetch,
initializePlatformFetch,
+ installPlatformFetch,
platformFetch,
webviewFetch,
} from './platformFetch';
@@ -123,3 +124,16 @@ it('keeps configured local and same-origin APIs on the native cookie jar', async
await request(new URL('/auth-assets/icon.png', document.baseURI));
expect(browser).toHaveBeenCalledTimes(1);
});
+
+it('routes the installed global fetch to the native transport without recursing', async () => {
+ const browser = vi
+ .fn<typeof fetch>()
+ .mockResolvedValue(new Response('browser'));
+ vi.stubGlobal('fetch', browser);
+ installPlatformFetch();
+ await fetch('https://gateway.example/auth/user/legacy_user_permissions');
+ await platformFetch('https://gateway.example/auth/jwt/macro_api_token');
+ await fetch('https://api.klipy.com/v2/search');
+ expect(nativeFetch).toHaveBeenCalledTimes(2);
+ expect(browser).toHaveBeenCalledTimes(1);
+});
diff --git a/apps/web/src/lib/core/util/platformFetch.ts b/apps/web/src/lib/core/util/platformFetch.ts
index 3bfcd7f907..ddbc40d177 100644
--- a/apps/web/src/lib/core/util/platformFetch.ts
+++ b/apps/web/src/lib/core/util/platformFetch.ts
@@ -5,7 +5,46 @@ import { isTauri } from './platform';
const nativeServices = NATIVE_SESSION_SERVICE_URLS.map((url) => new URL(url));
let originalFetch: typeof fetch | undefined;
-/** Called by the entry point before installing the desktop global override. */
+// Third-party APIs that allow any origin and require requests from the user's
+// browser. The native HTTP client's scope doesn't list them and rejects them.
+const WEBVIEW_FETCH_HOSTS = new Set(['api.klipy.com']);
+
+/**
+ * Called once by the entry point: routes Tauri's global fetch through the
+ * native transport. Localhost requests (dev server) skip it to keep HMR working.
+ */
+export function installPlatformFetch() {
+ if (!isTauri()) return;
+
+ const browserFetch = window.fetch;
+ // The global override must call the transport, never `platformFetch`: that
+ // reads the global back and would recurse until the stack overflows.
+ const nativeFetch = initializePlatformFetch(browserFetch);
+ window.fetch = new Proxy(browserFetch, {
+ apply: (target, thisArg, args: Parameters<typeof fetch>) => {
+ const url = args[0];
+ // Resolve bundled asset paths before checking the existing localhost bypass.
+ let resolvedUrl: URL;
+ try {
+ resolvedUrl = new URL(
+ url instanceof Request ? url.url : String(url),
+ document.baseURI
+ );
+ } catch (error) {
+ return Promise.reject(error);
+ }
+ if (
+ resolvedUrl.href.includes('localhost') ||
+ WEBVIEW_FETCH_HOSTS.has(resolvedUrl.hostname)
+ ) {
+ return target.apply(thisArg, args);
+ }
+ return nativeFetch.apply(thisArg, args);
+ },
+ });
+}
+
+/** Exported for tests; production calls it through `installPlatformFetch`. */
export function initializePlatformFetch(
webviewFetch: typeof fetch
): typeof fetch {