Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
feat(webv2): support reverse-proxy deployment prefixes
joshistoast447659acfe97
diff --git a/invokeai/frontend/webv2/src/platform/i18n/client.ts b/invokeai/frontend/webv2/src/platform/i18n/client.tsindex 9d014a8cbb..b920586aa1 100644--- a/invokeai/frontend/webv2/src/platform/i18n/client.ts+++ b/invokeai/frontend/webv2/src/platform/i18n/client.ts@@ -1,3 +1,4 @@+import { getDeploymentBaseUrl } from '@platform/transport/deploymentBase';import i18n from 'i18next';import Backend from 'i18next-http-backend';import { initReactI18next } from 'react-i18next';@@ -7,7 +8,7 @@ void i18n.use(initReactI18next).init({backend: {- loadPath: `${import.meta.env.BASE_URL}locales/{{lng}}.json`,+ loadPath: `${getDeploymentBaseUrl()}/locales/{{lng}}.json`,},debug: false,fallbackLng: 'en',diff --git a/invokeai/frontend/webv2/src/platform/transport/deploymentBase.test.ts b/invokeai/frontend/webv2/src/platform/transport/deploymentBase.test.tsnew file mode 100644index 0000000000..525c558b6e--- /dev/null+++ b/invokeai/frontend/webv2/src/platform/transport/deploymentBase.test.ts@@ -0,0 +1,45 @@+import { describe, expect, it } from 'vitest';++import { deriveDeploymentBaseUrl } from './deploymentBase';++describe('deriveDeploymentBaseUrl', () => {+ const fallback = 'https://app.example';++ it('derives a reverse-proxy prefix from a production asset URL', () => {+ expect(+ deriveDeploymentBaseUrl('https://app.example/invoke/assets/index-abc.js', fallback, 'https://app.example')+ ).toBe('https://app.example/invoke');+ });++ it('keeps root deployments at the page origin', () => {+ expect(deriveDeploymentBaseUrl('https://app.example/assets/index-abc.js', fallback, 'https://app.example')).toBe(+ fallback+ );+ });++ it('uses the final assets segment for nested deployment prefixes', () => {+ expect(+ deriveDeploymentBaseUrl(+ 'https://app.example/team/assets/invoke/assets/index-abc.js',+ fallback,+ 'https://app.example'+ )+ ).toBe('https://app.example/team/assets/invoke');+ });++ it('keeps development modules at the page origin', () => {+ expect(deriveDeploymentBaseUrl('http://localhost:5174/src/main.tsx', 'http://localhost:5174')).toBe(+ 'http://localhost:5174'+ );+ });++ it('falls back to the app origin for foreign CDN assets', () => {+ expect(deriveDeploymentBaseUrl('https://cdn.example/invoke/assets/index.js', fallback, 'https://app.example')).toBe(+ fallback+ );+ });++ it('falls back for an invalid module URL', () => {+ expect(deriveDeploymentBaseUrl('not a url', fallback)).toBe(fallback);+ });+});diff --git a/invokeai/frontend/webv2/src/platform/transport/deploymentBase.ts b/invokeai/frontend/webv2/src/platform/transport/deploymentBase.tsnew file mode 100644index 0000000000..569a55fb5a--- /dev/null+++ b/invokeai/frontend/webv2/src/platform/transport/deploymentBase.ts@@ -0,0 +1,42 @@+/**+ * Derives the backend deployment root (origin plus an optional reverse-proxy+ * prefix) from the emitted module URL. Vite emits production chunks beneath+ * `<deployment-root>/assets/`; development modules have no `/assets/` segment+ * and therefore keep the existing origin-root behavior.+ */+export const deriveDeploymentBaseUrl = (+ moduleUrl: string,+ fallbackOrigin: string,+ appOrigin = typeof window === 'undefined' ? fallbackOrigin : window.location.origin+): string => {+ try {+ const url = new URL(moduleUrl);++ if (url.origin !== appOrigin) {+ return fallbackOrigin;+ }++ const assetsIndex = url.pathname.lastIndexOf('/assets/');+ const prefix = assetsIndex >= 0 ? url.pathname.slice(0, assetsIndex) : '';++ return `${url.origin}${prefix}`;+ } catch {+ return fallbackOrigin;+ }+};++let cachedDeploymentBaseUrl: string | undefined;++/** Deployment origin plus prefix, without a trailing slash. */+export const getDeploymentBaseUrl = (): string => {+ cachedDeploymentBaseUrl ??= deriveDeploymentBaseUrl(import.meta.url, window.location.origin);++ return cachedDeploymentBaseUrl;+};++/** Empty at the domain root, otherwise a leading-slash prefix without a trailing slash. */+export const getDeploymentBasePath = (): string => {+ const pathname = new URL(getDeploymentBaseUrl()).pathname;++ return pathname === '/' ? '' : pathname.replace(/\/$/, '');+};diff --git a/invokeai/frontend/webv2/src/platform/transport/http.test.ts b/invokeai/frontend/webv2/src/platform/transport/http.test.tsnew file mode 100644index 0000000000..8507388029--- /dev/null+++ b/invokeai/frontend/webv2/src/platform/transport/http.test.ts@@ -0,0 +1,30 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';++vi.mock('./deploymentBase', () => ({+ getDeploymentBasePath: () => '/invoke',+ getDeploymentBaseUrl: () => 'https://app.example/invoke',+}));++import { absolutizeApiUrl, buildApiUrl, getBackendSocketPath, getBackendSocketUrl } from './http';++describe('deployment-aware backend URLs', () => {+ beforeEach(() => {+ vi.stubGlobal('window', { location: { origin: 'https://app.example' } });+ });++ it('prefixes API requests with the deployment root', () => {+ expect(buildApiUrl('/api/v1/app/version')).toBe('https://app.example/invoke/api/v1/app/version');+ });++ it('prefixes backend-relative resource URLs and preserves absolute URLs', () => {+ expect(absolutizeApiUrl('/api/v1/images/i/image.png/full')).toBe(+ 'https://app.example/invoke/api/v1/images/i/image.png/full'+ );+ expect(absolutizeApiUrl('https://cdn.example/image.png')).toBe('https://cdn.example/image.png');+ });++ it('uses the page origin with a deployment-prefixed Socket.IO path', () => {+ expect(getBackendSocketUrl()).toBe('https://app.example');+ expect(getBackendSocketPath()).toBe('/invoke/ws/socket.io');+ });+});diff --git a/invokeai/frontend/webv2/src/platform/transport/http.ts b/invokeai/frontend/webv2/src/platform/transport/http.tsindex 864faf62ee..4a6d48c6a6 100644--- a/invokeai/frontend/webv2/src/platform/transport/http.ts+++ b/invokeai/frontend/webv2/src/platform/transport/http.ts@@ -5,7 +5,9 @@* request, so an identity change applies without a reload.*/-const API_BASE_URL = import.meta.env.VITE_INVOKEAI_API_BASE_URL ?? '';+import { getDeploymentBasePath, getDeploymentBaseUrl } from './deploymentBase';++const API_BASE_URL = (import.meta.env.VITE_INVOKEAI_API_BASE_URL ?? '').trim().replace(/\/$/, '');export interface HttpAuthAdapter {getToken(): string | null;onUnauthorized(): void;@@ -26,22 +28,36 @@ export const getHttpAuthToken = (): string | null => authAdapter.getToken();* stays unaware of session semantics.*/export const getBackendSocketUrl = (): string => {- if (!API_BASE_URL.trim()) {- return window.location.origin;+ const baseUrl = API_BASE_URL || getDeploymentBaseUrl();++ return new URL(baseUrl, window.location.origin).origin;+};++export const getBackendSocketPath = (): string => {+ if (!API_BASE_URL) {+ return `${getDeploymentBasePath()}/ws/socket.io`;}- return new URL(API_BASE_URL, window.location.origin).origin;+ const pathname = new URL(API_BASE_URL, window.location.origin).pathname.replace(/\/$/, '');++ return `${pathname === '/' ? '' : pathname}/ws/socket.io`;};-export const buildApiUrl = (path: string): string => `${API_BASE_URL}${path}`;+export const buildApiUrl = (path: string): string => `${API_BASE_URL || getDeploymentBaseUrl()}${path}`;/** Resolve a backend-relative resource URL (e.g. image URLs in DTOs) against the API host. */export const absolutizeApiUrl = (url: string): string => {- if (!API_BASE_URL || url.startsWith('http://') || url.startsWith('https://')) {- return url;+ try {+ const parsed = new URL(url);++ if (parsed.protocol) {+ return url;+ }+ } catch {+ // Relative URL — resolve it against the backend deployment root below.}- return new URL(url, API_BASE_URL).toString();+ return url.startsWith('/') ? buildApiUrl(url) : new URL(url, `${API_BASE_URL || getDeploymentBaseUrl()}/`).toString();};export class ApiError extends Error {diff --git a/invokeai/frontend/webv2/src/platform/transport/socketHub.ts b/invokeai/frontend/webv2/src/platform/transport/socketHub.tsindex 61ffc2ef42..c00c288d67 100644--- a/invokeai/frontend/webv2/src/platform/transport/socketHub.ts+++ b/invokeai/frontend/webv2/src/platform/transport/socketHub.ts@@ -3,9 +3,7 @@ import { io } from 'socket.io-client';import type { BackendConnectionStatus } from './types';import { setConnectionStatus } from './connectionStore';-import { getBackendSocketUrl, getHttpAuthToken } from './http';--const SOCKET_PATH = '/ws/socket.io';+import { getBackendSocketPath, getBackendSocketUrl, getHttpAuthToken } from './http';/*** The minimal Socket.IO surface the hub uses; tests substitute a fake.@@ -46,7 +44,7 @@ const createDefaultSocket = (): BackendSocket => {auth: token ? { token } : undefined,autoConnect: false,extraHeaders: token ? { Authorization: `Bearer ${token}` } : undefined,- path: SOCKET_PATH,+ path: getBackendSocketPath(),timeout: 60000,}) as unknown as BackendSocket;};