fix(tauri): redirect relative fetch and websocket urls to production backend for desktop app
This commit is contained in:
@@ -3,6 +3,35 @@ import { createRoot } from 'react-dom/client';
|
||||
import './styles/index.css';
|
||||
import App from './App.tsx';
|
||||
|
||||
const isTauri = 'window' in globalThis && '__TAURI_INTERNALS__' in window;
|
||||
if (isTauri) {
|
||||
const TARGET = 'https://dumpster.dustin.coffee';
|
||||
const WS_TARGET = 'wss://dumpster.dustin.coffee';
|
||||
|
||||
const originalFetch = window.fetch;
|
||||
window.fetch = async (input, init) => {
|
||||
if (typeof input === 'string' && input.startsWith('/')) {
|
||||
input = TARGET + input;
|
||||
}
|
||||
return originalFetch(input, init);
|
||||
};
|
||||
|
||||
const OriginalWebSocket = window.WebSocket;
|
||||
window.WebSocket = function(url: string | URL, protocols?: string | string[]) {
|
||||
let urlStr = typeof url === 'string' ? url : url.toString();
|
||||
if (urlStr.includes('tauri.localhost') || urlStr.includes('tauri://localhost')) {
|
||||
urlStr = urlStr.replace(/^(wss?|https?|tauri):\/\/tauri\.localhost/, WS_TARGET);
|
||||
urlStr = urlStr.replace(/^tauri:\/\/localhost/, WS_TARGET);
|
||||
} else if (urlStr.startsWith('ws://localhost') && isTauri) {
|
||||
urlStr = urlStr.replace('ws://localhost', WS_TARGET);
|
||||
} else if (urlStr.startsWith('/')) {
|
||||
urlStr = WS_TARGET + urlStr;
|
||||
}
|
||||
return new OriginalWebSocket(urlStr, protocols);
|
||||
} as unknown as typeof WebSocket;
|
||||
window.WebSocket.prototype = OriginalWebSocket.prototype;
|
||||
}
|
||||
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
|
||||
Reference in New Issue
Block a user