fix(web): stabilize local sign-in and page lifecycle handling

This commit is contained in:
Boki 2026-10-25 11:31:42 -04:00
parent 3a290cc8a7
commit 0874e44c12
7 changed files with 79 additions and 17 deletions

View file

@ -32,6 +32,8 @@ bun run --cwd apps/stock/orchestrator dev
Run these in separate terminals from the repository root. The frontend alone does not run backtests; the API and orchestrator handle admission and execution. Ingestion and pipeline are additional entrypoints when you need to collect source data or synchronize the catalog.
Open `http://localhost:5173` and sign in with `STOCK_BOT_ADMIN_TOKEN` from the root `.env`. Both `STOCK_BOT_ADMIN_TOKEN` and `STOCK_BOT_SERVICE_TOKEN` must be configured as distinct values of at least 32 characters before starting HTTP services. The API uses port `2003`; the orchestrator uses `2004`. The frontend has a fixed port to avoid colliding with Docker's Grafana (`3000`) and Bull Board (`3001`) or silently moving to an unapproved browser origin.
Stock configuration is loaded by [config-instance.ts](config/src/config-instance.ts), with stock-specific validation in [stock-app.schema.ts](config/src/schemas/stock-app.schema.ts). HTTP authentication/session behavior is in the shared [security module](../../libs/core/di/src/http/security.ts). Keep backend credentials out of frontend build variables.
## Supported execution

View file

@ -1,9 +1,9 @@
# API Configuration
VITE_API_BASE_URL=http://localhost:2003
VITE_DATA_SERVICE_URL=http://localhost:3001
VITE_PORTFOLIO_SERVICE_URL=http://localhost:3002
VITE_STRATEGY_SERVICE_URL=http://localhost:3003
VITE_EXECUTION_SERVICE_URL=http://localhost:3004
# Environment
# API Configuration
VITE_API_BASE_URL=
VITE_DATA_SERVICE_URL=http://localhost:3001
VITE_PORTFOLIO_SERVICE_URL=http://localhost:3002
VITE_STRATEGY_SERVICE_URL=http://localhost:3003
VITE_EXECUTION_SERVICE_URL=http://localhost:3004
# Environment
VITE_NODE_ENV=development

View file

@ -1,6 +1,7 @@
# API Configuration
# Web API service URL (default port: 2003)
VITE_API_BASE_URL=http://localhost:2003
# Leave blank to use this browser's hostname on port 2003 in development.
# Set an explicit URL only when the API is hosted elsewhere.
VITE_API_BASE_URL=
# Other services (if needed in the future)
VITE_DATA_SERVICE_URL=http://localhost:3001
@ -9,4 +10,4 @@ VITE_STRATEGY_SERVICE_URL=http://localhost:3003
VITE_EXECUTION_SERVICE_URL=http://localhost:3004
# Environment
VITE_NODE_ENV=development
VITE_NODE_ENV=development

View file

@ -43,7 +43,7 @@ bun run type-check
These start Vite development, build static assets, preview a build, and check frontend types respectively; run the command you need. React, TypeScript, Vite, Tailwind, Headless UI and Recharts are the main UI dependencies. See [package.json](package.json) for scripts and pinned ranges.
Vite is configured for `127.0.0.1:3000`. The API origin is `VITE_API_BASE_URL` when supplied; otherwise development uses the browser's hostname on port `2003`, and production uses the page's own origin. The dev server has no configured API proxy. The backend must be running and allow the frontend origin for authenticated cross-origin requests.
Vite is configured for `127.0.0.1:5173`, with a fixed port so it cannot silently move to an origin the API does not allow. Port `3000` belongs to Grafana in the Docker stack. Open `http://localhost:5173` or `http://127.0.0.1:5173`. The API origin is `VITE_API_BASE_URL` when supplied; the checked-in value is blank, so development follows the browser's hostname on port `2003`, and production uses the page's own origin. This keeps the session cookie on the same hostname: mixing `127.0.0.1` and `localhost` can prevent sign-in from persisting. Restart Vite after changing environment configuration. The dev server has no configured API proxy. The backend must be running and allow the frontend origin for authenticated cross-origin requests.
Browser authentication uses HttpOnly session cookies. `VITE_API_BASE_URL` is a public address, not a place for service credentials. Research runs also need the orchestrator and its configured infrastructure; starting Vite alone supplies the interface only.

View file

@ -2,15 +2,16 @@ import { browserLog } from '@/utils/browserLog';
// Global error handler for chart disposal errors
export function setupChartErrorHandler() {
// Only suppress chart disposal errors during page unload
// pagehide works with back/forward caching and documents that disallow unload.
// Restore normal reporting if the browser later restores this same document.
let isUnloading = false;
window.addEventListener('beforeunload', () => {
window.addEventListener('pagehide', () => {
isUnloading = true;
});
window.addEventListener('unload', () => {
isUnloading = true;
window.addEventListener('pageshow', () => {
isUnloading = false;
});
// Intercept console errors for chart disposal

View file

@ -11,7 +11,8 @@ export default defineConfig({
},
},
server: {
port: 3000,
port: 5173,
strictPort: true,
host: '127.0.0.1',
},
});

View file

@ -0,0 +1,57 @@
import { expect, test } from 'bun:test';
import { browserLog } from '../../apps/stock/web-app/src/utils/browserLog';
import { setupChartErrorHandler } from '../../apps/stock/web-app/src/utils/chartErrorHandler';
test('chart cleanup uses page lifecycle events and restores error reporting after history navigation', () => {
const previousWindow = Object.getOwnPropertyDescriptor(globalThis, 'window');
const previousError = browserLog.error;
const target = new EventTarget();
const registered: string[] = [];
const messages: unknown[][] = [];
Object.defineProperty(globalThis, 'window', {
configurable: true,
value: {
addEventListener(type: string, callback: EventListener) {
registered.push(type);
target.addEventListener(type, callback);
},
},
});
browserLog.error = (...args) => messages.push(args);
const chartError = () => {
const event = new Event('error', { cancelable: true });
Object.defineProperty(event, 'error', { value: new Error('Object is disposed') });
target.dispatchEvent(event);
return event;
};
try {
setupChartErrorHandler();
expect(registered).toContain('pagehide');
expect(registered).toContain('pageshow');
expect(registered).not.toContain('unload');
expect(registered).not.toContain('beforeunload');
browserLog.error('Object is disposed');
expect(messages).toHaveLength(1);
expect(chartError().defaultPrevented).toBe(false);
target.dispatchEvent(new Event('pagehide'));
browserLog.error('Object is disposed');
expect(messages).toHaveLength(1);
expect(chartError().defaultPrevented).toBe(true);
browserLog.error('Unrelated application failure');
expect(messages).toHaveLength(2);
target.dispatchEvent(new Event('pageshow'));
browserLog.error('Object is disposed');
expect(messages).toHaveLength(3);
expect(chartError().defaultPrevented).toBe(false);
} finally {
browserLog.error = previousError;
if (previousWindow) {
Object.defineProperty(globalThis, 'window', previousWindow);
} else {
Reflect.deleteProperty(globalThis, 'window');
}
}
});