fix: separate origin from base path so remote backend WS URL is valid

Previously, when a remote backend URL was set via the connection panel,
api_base contained the full origin and path, causing the WebSocket URL
to be malformed: wss://127.0.0.1:8188http://127.0.0.1:8188/ws.

Now we keep api_host/api_base as just host/path and use a separate
remoteOrigin field that gets prepended in apiURL/fileURL/internalURL.
The WebSocket protocol is also now derived from the remote backend URL
when set, instead of always copying from the page protocol.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
snomiao
2026-04-11 23:36:03 +09:00
parent b4f8605ab2
commit 24b9f171a4

View File

@@ -367,6 +367,12 @@ export class ComfyApi extends EventTarget {
*/
apiKey?: string
/**
* The origin (protocol + host) for the backend, when overridden via the
* preview connection panel. Empty string means use same-origin.
*/
private remoteOrigin = ''
constructor() {
super()
this.user = ''
@@ -374,8 +380,9 @@ export class ComfyApi extends EventTarget {
const remoteBackend = localStorage.getItem('comfyui-preview-backend-url')
if (remoteBackend) {
const url = new URL(remoteBackend)
this.remoteOrigin = url.origin
this.api_host = url.host
this.api_base = url.origin + url.pathname.replace(/\/+$/, '')
this.api_base = url.pathname.replace(/\/+$/, '')
} else {
this.api_host = location.host
this.api_base = isCloud
@@ -387,16 +394,17 @@ export class ComfyApi extends EventTarget {
}
internalURL(route: string): string {
return this.api_base + '/internal' + route
return this.remoteOrigin + this.api_base + '/internal' + route
}
apiURL(route: string): string {
if (route.startsWith('/api')) return this.api_base + route
return this.api_base + '/api' + route
if (route.startsWith('/api'))
return this.remoteOrigin + this.api_base + route
return this.remoteOrigin + this.api_base + '/api' + route
}
fileURL(route: string): string {
return this.api_base + route
return this.remoteOrigin + this.api_base + route
}
/**
@@ -587,8 +595,14 @@ export class ComfyApi extends EventTarget {
}
}
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'
const baseUrl = `${protocol}://${this.api_host}${this.api_base}/ws`
// Derive WebSocket protocol from remote backend if set, else from page
let wsProtocol: string
if (this.remoteOrigin) {
wsProtocol = this.remoteOrigin.startsWith('https:') ? 'wss' : 'ws'
} else {
wsProtocol = window.location.protocol === 'https:' ? 'wss' : 'ws'
}
const baseUrl = `${wsProtocol}://${this.api_host}${this.api_base}/ws`
const query = params.toString()
const wsUrl = query ? `${baseUrl}?${query}` : baseUrl