refactor(deep-links, quick-ask, window): replace localhost references with dynamic server origin retrieval

This commit is contained in:
Anish Sarkar 2026-05-25 17:55:03 +05:30
parent a2847664c8
commit fe797e65d6
4 changed files with 16 additions and 11 deletions

View file

@ -1,7 +1,7 @@
import { app } from 'electron';
import path from 'path';
import { getMainWindow } from './window';
import { getServerPort } from './server';
import { getServerOrigin } from './server';
import { trackEvent } from './analytics';
const PROTOCOL = 'surfsense';
@ -23,7 +23,7 @@ function handleDeepLink(url: string) {
});
if (parsed.hostname === 'auth' && parsed.pathname === '/callback') {
const params = parsed.searchParams.toString();
win.loadURL(`http://localhost:${getServerPort()}/auth/callback?${params}`);
win.loadURL(`${getServerOrigin()}/auth/callback?${params}`);
}
win.show();

View file

@ -2,7 +2,7 @@ import { BrowserWindow, clipboard, globalShortcut, ipcMain, screen, shell } from
import path from 'path';
import { IPC_CHANNELS } from '../ipc/channels';
import { checkAccessibilityPermission, getFrontmostApp, simulateCopy, simulatePaste } from './platform';
import { getServerPort } from './server';
import { getServerOrigin } from './server';
import { getShortcuts } from './shortcuts';
import { getActiveSearchSpaceId } from './active-search-space';
import { trackEvent } from './analytics';
@ -58,7 +58,7 @@ function createQuickAskWindow(x: number, y: number): BrowserWindow {
const spaceId = pendingSearchSpaceId;
const route = spaceId ? `/dashboard/${spaceId}/new-chat` : '/dashboard';
quickAskWindow.loadURL(`http://localhost:${getServerPort()}${route}?quickAssist=true`);
quickAskWindow.loadURL(`${getServerOrigin()}${route}?quickAssist=true`);
quickAskWindow.once('ready-to-show', () => {
quickAskWindow?.show();
@ -69,7 +69,7 @@ function createQuickAskWindow(x: number, y: number): BrowserWindow {
});
quickAskWindow.webContents.setWindowOpenHandler(({ url }) => {
if (url.startsWith('http://localhost')) {
if (url.startsWith(getServerOrigin())) {
return { action: 'allow' };
}
shell.openExternal(url);

View file

@ -3,6 +3,7 @@ import { app, utilityProcess } from 'electron';
import { getPort } from 'get-port-please';
const isDev = !app.isPackaged;
const SERVER_HOST = '127.0.0.1';
let serverPort = 3000;
let nextServerProcess: ReturnType<typeof utilityProcess.fork> | null = null;
@ -10,6 +11,10 @@ export function getServerPort(): number {
return serverPort;
}
export function getServerOrigin(): string {
return `http://${SERVER_HOST}:${serverPort}`;
}
function getStandalonePath(): string {
if (isDev) {
return path.join(__dirname, '..', '..', 'surfsense_web', '.next', 'standalone', 'surfsense_web');
@ -44,7 +49,7 @@ export async function startNextServer(): Promise<void> {
env: {
...process.env,
PORT: String(serverPort),
HOSTNAME: '127.0.0.1',
HOSTNAME: SERVER_HOST,
NODE_ENV: 'production',
},
serviceName: 'SurfSense Next Server',
@ -75,7 +80,7 @@ export async function startNextServer(): Promise<void> {
child.once('exit', startupExitHandler);
});
const ready = await Promise.race([waitForServer(`http://localhost:${serverPort}`), exited]);
const ready = await Promise.race([waitForServer(getServerOrigin()), exited]);
if (startupExitHandler) {
child.removeListener('exit', startupExitHandler);
}

View file

@ -2,7 +2,7 @@ import { app, BrowserWindow, shell, session } from 'electron';
import path from 'path';
import { trackEvent } from './analytics';
import { showErrorDialog } from './errors';
import { getServerPort } from './server';
import { getServerOrigin } from './server';
import { setActiveSearchSpaceId } from './active-search-space';
const isDev = !app.isPackaged;
@ -58,10 +58,10 @@ export function createMainWindow(initialPath = '/dashboard'): BrowserWindow {
mainWindow?.setTitle(WINDOW_TITLE);
});
mainWindow.loadURL(`http://localhost:${getServerPort()}${initialPath}`);
mainWindow.loadURL(`${getServerOrigin()}${initialPath}`);
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
if (url.startsWith('http://localhost')) {
if (url.startsWith(getServerOrigin())) {
return { action: 'allow' };
}
shell.openExternal(url);
@ -70,7 +70,7 @@ export function createMainWindow(initialPath = '/dashboard'): BrowserWindow {
const filter = { urls: [`${HOSTED_FRONTEND_URL}/*`] };
session.defaultSession.webRequest.onBeforeRequest(filter, (details, callback) => {
const rewritten = details.url.replace(HOSTED_FRONTEND_URL, `http://localhost:${getServerPort()}`);
const rewritten = details.url.replace(HOSTED_FRONTEND_URL, getServerOrigin());
callback({ redirectURL: rewritten });
});