OrchardCMS/OrchardCore · error · Error

The HubConnection url must be a valid url.

Error message

The HubConnection url must be a valid url.

What it means

The baseUrl setter validates that the supplied url is truthy before applying it; an empty string, null, or undefined throws 'The HubConnection url must be a valid url.' This guards against silently configuring a connection with no endpoint, which would fail later at negotiate time with a much less obvious error.

Solutions

  1. Ensure the value passed to baseUrl is a non-empty, absolute URL before assignment.
  2. Validate the configuration source (env var, app setting, DOM attribute) and fail fast with a clear message if it is missing.
  3. Provide a sensible default URL (e.g. window.location.origin + '/hubs') when the configured value is empty.
  4. Use withUrl(url) at construction time so a missing URL surfaces during initial configuration.

Example fix

// before
connection.baseUrl = getConfig("hubUrl"); // may be ''
// after
const hubUrl = getConfig("hubUrl");
if (!hubUrl) {
  throw new Error("hubUrl configuration value is missing");
}
connection.baseUrl = hubUrl;
Defensive patterns

Strategy: validation

Validate before calling

function isValidHubUrl(url) {
  if (!url || typeof url !== "string") return false;
  try { new URL(url, window.location.origin); return true; } catch { return false; }
}
if (!isValidHubUrl(hubUrl)) throw new Error("hubUrl missing or invalid");

Type guard

function isNonEmptyString(v) {
  return typeof v === "string" && v.length > 0;
}

Try / catch

try {
  connection.baseUrl = hubUrl;
} catch (err) {
  if (err.message.includes("must be a valid url")) {
    console.error("Hub URL is empty — check configuration");
    hubUrl = defaultHubUrl;
    connection.baseUrl = hubUrl;
  } else { throw err; }
}

Prevention

When it happens

Trigger: Setting hubConnection.baseUrl = '' , null, or undefined; building the URL from a config value or environment variable that is empty at runtime and passing it straight to the setter.

Common situations: Missing appsettings/environment config in deployment; concatenating a base path that resolves to empty string; reading a DOM attribute like data-hub-url that is absent, yielding ''.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/f9f5f1d3ee4009d2. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore.Modules/OrchardCore.SignalR/wwwroot/Scripts/signalr.js:1254

     */
    get connectionId() {
        return this.connection ? (this.connection.connectionId || null) : null;
    }
    /** Indicates the url of the {@link HubConnection} to the server. */
    get baseUrl() {
        return this.connection.baseUrl || "";
    }
    /**
     * Sets a new url for the HubConnection. Note that the url can only be changed when the connection is in either the Disconnected or
     * Reconnecting states.
     * @param {string} url The url to connect to.
     */
    set baseUrl(url) {
        if (this._connectionState !== HubConnectionState.Disconnected && this._connectionState !== HubConnectionState.Reconnecting) {
            throw new Error("The HubConnection must be in the Disconnected or Reconnecting state to change the url.");
        }
        if (!url) {
            throw new Error("The HubConnection url must be a valid url.");
        }
        this.connection.baseUrl = url;
    }
    /** Starts the connection.
     *
     * @returns {Promise<void>} A Promise that resolves when the connection has been successfully established, or rejects with an error.
     */
    start() {
        this._startPromise = this._startWithStateTransitions();
        return this._startPromise;
    }
    async _startWithStateTransitions() {
        if (this._connectionState !== HubConnectionState.Disconnected) {
            return Promise.reject(new Error("Cannot start a HubConnection that is not in the 'Disconnected' state."));
        }
        this._connectionState = HubConnectionState.Connecting;
        this._logger.log(LogLevel.Debug, "Starting HubConnection.");
        try {

View on GitHub (pinned to 4306c0717f)