Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: update base-url on remote healthy (#5787)

Myles Scolnickcac7a0898fd9
diff --git a/frontend/src/core/runtime/__tests__/runtime.test.ts b/frontend/src/core/runtime/__tests__/runtime.test.ts
index 8867fc456d..4c563ea0da 100644
--- a/frontend/src/core/runtime/__tests__/runtime.test.ts
+++ b/frontend/src/core/runtime/__tests__/runtime.test.ts
@@ -1,6 +1,6 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { SessionId } from "@/core/kernel/session";
import { Logger } from "@/utils/Logger";
import { RuntimeManager } from "../runtime";
@@ -261,6 +261,99 @@ describe("RuntimeManager", () => {
});
});
+ describe("setDOMBaseUri", () => {
+ let originalBase: HTMLBaseElement | null;
+
+ beforeEach(() => {
+ // Store and remove existing base element if any
+ originalBase = document.querySelector("base");
+ if (originalBase) {
+ originalBase.remove();
+ }
+ });
+
+ afterEach(() => {
+ // Clean up any base elements created during tests
+ const baseElements = document.querySelectorAll("base");
+ baseElements.forEach((base) => base.remove());
+
+ // Restore original base element if it existed
+ if (originalBase) {
+ document.head.append(originalBase);
+ }
+ });
+
+ it("should not set base URI when health check fails", async () => {
+ const runtime = new RuntimeManager(mockConfig);
+ // Base element should not be set
+ let baseElement = document.querySelector("base");
+ expect(baseElement).toBeNull();
+
+ // Mock failed health check
+ global.fetch = vi.fn().mockResolvedValue({ ok: false });
+
+ await runtime.isHealthy();
+
+ baseElement = document.querySelector("base");
+ expect(baseElement).toBeNull();
+ });
+
+ it("should create base element when none exists", async () => {
+ const runtime = new RuntimeManager(mockConfig);
+
+ // Mock successful health check
+ global.fetch = vi.fn().mockResolvedValue({ ok: true });
+
+ await runtime.isHealthy();
+
+ const baseElement = document.querySelector("base");
+ expect(baseElement).toBeTruthy();
+ expect(baseElement?.getAttribute("href")).toBe("https://example.com/");
+ });
+
+ it("should update existing base element href", async () => {
+ // Create existing base element
+ const existingBase = document.createElement("base");
+ existingBase.setAttribute("href", "https://old-url.com");
+ document.head.append(existingBase);
+
+ const runtime = new RuntimeManager(mockConfig);
+
+ // Mock successful health check
+ global.fetch = vi.fn().mockResolvedValue({ ok: true });
+
+ await runtime.isHealthy();
+
+ const baseElement = document.querySelector("base");
+ expect(baseElement).toBe(existingBase); // Should be the same element
+ expect(baseElement?.getAttribute("href")).toBe("https://example.com/");
+
+ // Should only have one base element
+ expect(document.querySelectorAll("base")).toHaveLength(1);
+ });
+
+ it("should remove query params from base URI", async () => {
+ const configWithQueryParams: RuntimeConfig = {
+ url: "https://example.com/foo?param1=value1&param2=value2",
+ authToken: "test-token",
+ };
+
+ const runtime = new RuntimeManager(configWithQueryParams);
+
+ // Mock successful health check
+ global.fetch = vi.fn().mockResolvedValue({ ok: true });
+
+ await runtime.isHealthy();
+
+ const baseElement = document.querySelector("base");
+ expect(baseElement).toBeTruthy();
+ // Query params should be removed from the base href
+ expect(baseElement?.getAttribute("href")).toBe(
+ "https://example.com/foo/",
+ );
+ });
+ });
+
describe("edge cases", () => {
it("should handle URLs with nested paths", () => {
const runtime = new RuntimeManager({
diff --git a/frontend/src/core/runtime/runtime.ts b/frontend/src/core/runtime/runtime.ts
index 4bdc504335..40ee2ab5ed 100644
--- a/frontend/src/core/runtime/runtime.ts
+++ b/frontend/src/core/runtime/runtime.ts
@@ -167,14 +167,53 @@ export class RuntimeManager {
// If there is a redirect, update the URL in the config
if (response.redirected) {
// strip /health from the URL
- this.config.url = response.url.replace(/\/health$/, "");
+ const baseUrl = response.url.replace(/\/health$/, "");
+ this.config.url = baseUrl;
}
- return response.ok;
+
+ const success = response.ok;
+ if (success) {
+ this.setDOMBaseUri(this.config.url);
+ }
+ return success;
} catch {
return false;
}
}
+ /**
+ * Sets the base URI for resolving relative URLs in the document.
+ *
+ * @param uri - The base URI to set. This should be a valid URL string.
+ *
+ * @remarks
+ * This method modifies the `<base>` element in the document's `<head>`.
+ * If a `<base>` element already exists, its `href` attribute is updated.
+ * Otherwise, a new `<base>` element is created and appended to the `<head>`.
+ *
+ * Side effects:
+ * - Changes how relative URLs are resolved in the document.
+ * - May affect the behavior of scripts, styles, and other resources that use relative URLs.
+ */
+ private setDOMBaseUri(uri: string) {
+ // Remove query params from the URI
+ uri = uri.split("?", 1)[0];
+
+ // Make sure there is a trailing slash
+ if (!uri.endsWith("/")) {
+ uri += "/";
+ }
+
+ let base = document.querySelector("base");
+ if (base) {
+ base.setAttribute("href", uri);
+ } else {
+ base = document.createElement("base");
+ base.setAttribute("href", uri);
+ document.head.append(base);
+ }
+ }
+
async init(options?: { disableRetryDelay?: boolean }) {
let retries = 0;
// This matches backoff logic elsewhere.