From cab5fb75af75f86fcd3734ea8fea06a56e757278 Mon Sep 17 00:00:00 2001 From: mubashir1osmani Date: Fri, 18 Sep 2026 00:00:00 -0400 Subject: [PATCH] fix(ui): bake /ui basePath so sidebar navigation is client-side The dashboard is a static export served under /ui, but it was built with basePath "" and every href manually prefixed via uiHref(). To next/link those hrefs look external, so each sidebar click was a full browser navigation: providers remounted and every endpoint refetched. Set basePath to /ui (UI_BASE_PATH still overrides it, which build_ui_custom_path.sh passes for server_root_path deployments; that env var was previously ignored by the config). Link hrefs and router.push targets become app-relative, the router prepends the base itself and navigations use the exported RSC payloads instead of reloading. uiHref() remains for raw and window.location targets, and appHrefFromUiHref() strips the base before router.push for hrefs built that way. Login-page redirects that carry browser-absolute return URLs move to window.location so the basePath-aware router does not double-prefix them. No proxy changes: the /ui StaticFiles mount already serves the per-route index.html and __next.*.txt payloads. Co-Authored-By: Claude Fable 5 --- ui/litellm-dashboard/next.config.mjs | 6 ++- .../src/app/(dashboard)/layout.tsx | 5 +- .../app/(dashboard)/legacyPageRoutes.test.ts | 24 +++++----- .../src/app/(dashboard)/legacyPageRoutes.ts | 3 +- .../src/app/(dashboard)/page.test.tsx | 10 ++-- .../src/app/chat/layout.test.tsx | 7 +-- ui/litellm-dashboard/src/app/chat/layout.tsx | 3 +- .../src/app/login/LoginPage.tsx | 20 ++++---- .../src/components/DashboardHeader.test.tsx | 8 ++-- .../components/Navbar/ViewSwitcher.test.tsx | 6 +-- .../src/components/Navbar/ViewSwitcher.tsx | 3 +- .../src/components/chat/ChatShell.test.tsx | 17 ++++--- .../src/components/chat/ChatShell.tsx | 3 +- .../components/chat/ConnectFlowSurface.tsx | 3 +- .../src/components/leftnav.test.tsx | 44 ++++++++--------- .../src/components/leftnav.tsx | 6 +-- .../src/components/navbar.tsx | 3 +- .../src/components/shared/EntityLink.test.tsx | 4 +- .../src/components/shared/EntityLink.tsx | 3 +- ui/litellm-dashboard/src/utils/uiHref.test.ts | 47 ++++++++++--------- ui/litellm-dashboard/src/utils/uiHref.ts | 24 ++++++---- 21 files changed, 122 insertions(+), 127 deletions(-) diff --git a/ui/litellm-dashboard/next.config.mjs b/ui/litellm-dashboard/next.config.mjs index 128ce0a84a7..2c9e90a6ccd 100644 --- a/ui/litellm-dashboard/next.config.mjs +++ b/ui/litellm-dashboard/next.config.mjs @@ -18,7 +18,11 @@ const nextConfig = { images: { unoptimized: true, }, - basePath: "", + // The proxy mounts the static export under /ui. Baking that into basePath lets + // next/link and router.push do client-side navigation instead of full reloads. + // server_root_path deployments must rebuild with UI_BASE_PATH=/ui + // (see build_ui_custom_path.sh). + basePath: process.env.UI_BASE_PATH || "/ui", assetPrefix: "/litellm-asset-prefix", trailingSlash: true, turbopack: { diff --git a/ui/litellm-dashboard/src/app/(dashboard)/layout.tsx b/ui/litellm-dashboard/src/app/(dashboard)/layout.tsx index fa6df7f176a..fb83609623e 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/layout.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/layout.tsx @@ -13,7 +13,6 @@ import { NoRedisWarningBanner } from "@/components/NoRedisWarningBanner"; import { EnvCredentialLoginWarningBanner } from "@/components/EnvCredentialLoginWarningBanner"; import { LicenseExpiryBanner } from "@/components/LicenseExpiryBanner"; import { UserBanner } from "@/components/UserBanner"; -import { uiHref } from "@/utils/uiHref"; import { PluginModeProvider, usePluginMode } from "@/contexts/PluginModeContext"; import { createApiClient } from "@/lib/http/client"; import { getProxyBaseUrl } from "@/components/networking"; @@ -150,10 +149,10 @@ function LayoutContent({ children }: { children: React.ReactNode }) { const isInvitationFlow = Boolean(searchParams.get("invitation_id")); // Legacy invitation links point at /ui/?invitation_id=; the onboarding form now lives at its own - // /onboarding route. Redirect once ui-config has loaded so uiHref resolves the SERVER_ROOT_PATH base. + // /onboarding route. useEffect(() => { if (!authLoading && isInvitationFlow) { - router.replace(`${uiHref("onboarding")}?${searchParams.toString()}`); + router.replace(`/onboarding?${searchParams.toString()}`); } }, [authLoading, isInvitationFlow, router, searchParams]); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/legacyPageRoutes.test.ts b/ui/litellm-dashboard/src/app/(dashboard)/legacyPageRoutes.test.ts index 6b02b79b04c..85ba548e648 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/legacyPageRoutes.test.ts +++ b/ui/litellm-dashboard/src/app/(dashboard)/legacyPageRoutes.test.ts @@ -6,26 +6,26 @@ const redirect = (query: string) => legacyPageRedirectHref(new URLSearchParams(q describe("legacyPageRedirectHref", () => { it("sends an old ?page= bookmark to the path route that replaced it", () => { - expect(redirect("page=logs")).toBe("/ui/logs"); - expect(redirect("page=models")).toBe("/ui/models-and-endpoints"); - expect(redirect("page=llm-playground")).toBe("/ui/playground"); - expect(redirect("page=new_usage")).toBe("/ui/usage"); - expect(redirect("page=usage")).toBe("/ui/old-usage"); + expect(redirect("page=logs")).toBe("/logs"); + expect(redirect("page=models")).toBe("/models-and-endpoints"); + expect(redirect("page=llm-playground")).toBe("/playground"); + expect(redirect("page=new_usage")).toBe("/usage"); + expect(redirect("page=usage")).toBe("/old-usage"); }); it("keeps the older aliases for renamed pages", () => { - expect(redirect("page=api_ref")).toBe("/ui/api-reference"); - expect(redirect("page=api-reference")).toBe("/ui/api-reference"); - expect(redirect("page=claude-code-plugins")).toBe("/ui/skills"); + expect(redirect("page=api_ref")).toBe("/api-reference"); + expect(redirect("page=api-reference")).toBe("/api-reference"); + expect(redirect("page=claude-code-plugins")).toBe("/skills"); }); it("forwards the remaining query params so the MCP env-var setup link still opens its form", () => { - expect(redirect("page=mcp-servers&fill_env_vars=srv-1")).toBe("/ui/mcp-servers?fill_env_vars=srv-1"); - expect(redirect("fill_env_vars=srv-1&page=mcp-servers")).toBe("/ui/mcp-servers?fill_env_vars=srv-1"); + expect(redirect("page=mcp-servers&fill_env_vars=srv-1")).toBe("/mcp-servers?fill_env_vars=srv-1"); + expect(redirect("fill_env_vars=srv-1&page=mcp-servers")).toBe("/mcp-servers?fill_env_vars=srv-1"); }); it("keeps forwarded values encoded", () => { - expect(redirect("page=mcp-servers&fill_env_vars=a%26b%3Dc")).toBe("/ui/mcp-servers?fill_env_vars=a%26b%3Dc"); + expect(redirect("page=mcp-servers&fill_env_vars=a%26b%3Dc")).toBe("/mcp-servers?fill_env_vars=a%26b%3Dc"); }); it("returns null when there is no page param or the id is unknown", () => { @@ -41,7 +41,7 @@ describe("legacyPageRedirectHref", () => { .filter((item) => !item.external_url); expect(leaves.length).toBeGreaterThan(30); for (const leaf of leaves) { - expect(redirect(`page=${leaf.page}`), leaf.page).toBe(`/ui/${leaf.route ?? leaf.page}`); + expect(redirect(`page=${leaf.page}`), leaf.page).toBe(`/${leaf.route ?? leaf.page}`); } }); }); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/legacyPageRoutes.ts b/ui/litellm-dashboard/src/app/(dashboard)/legacyPageRoutes.ts index cf943b331b9..4403a809aab 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/legacyPageRoutes.ts +++ b/ui/litellm-dashboard/src/app/(dashboard)/legacyPageRoutes.ts @@ -1,4 +1,3 @@ -import { uiHref } from "@/utils/uiHref"; const LEGACY_PAGE_ROUTES: ReadonlyMap = new Map( Object.entries({ @@ -50,5 +49,5 @@ export function legacyPageRedirectHref(searchParams: URLSearchParams): string | const rest = new URLSearchParams(searchParams); rest.delete("page"); const query = rest.toString(); - return query ? `${uiHref(route)}?${query}` : uiHref(route); + return query ? `/${route}?${query}` : `/${route}`; } diff --git a/ui/litellm-dashboard/src/app/(dashboard)/page.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/page.test.tsx index aaad0d072e5..a2ca30e94d3 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/page.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/page.test.tsx @@ -6,7 +6,7 @@ interface KeyRow { token: string; } -const { mockReplace, mockUseKeys, mockUiHref, state } = vi.hoisted(() => { +const { mockReplace, mockUseKeys, state } = vi.hoisted(() => { const state = { search: "login=success", userRole: "Internal User", @@ -16,7 +16,6 @@ const { mockReplace, mockUseKeys, mockUiHref, state } = vi.hoisted(() => { return { state, mockReplace: vi.fn(), - mockUiHref: vi.fn((segment: string) => `/mocked-ui/${segment}`), mockUseKeys: vi.fn(() => ({ data: { keys: state.keys, total_count: state.keys.length }, isLoading: false, @@ -44,7 +43,6 @@ vi.mock("@/components/common_components/LoadingScreen", () => ({ default: () =>
, })); vi.mock("@/components/networking", () => ({ proxyBaseUrl: "" })); -vi.mock("@/utils/uiHref", () => ({ uiHref: mockUiHref })); vi.mock("@/utils/returnUrlUtils", () => ({ buildLoginUrlWithReturn: (u: string) => u, consumeReturnUrl: () => state.returnUrl, @@ -77,7 +75,6 @@ describe("dashboard landing", () => { state.returnUrl = null; mockReplace.mockClear(); mockUseKeys.mockClear(); - mockUiHref.mockClear(); mockLocationReplace.mockClear(); }); @@ -89,7 +86,6 @@ describe("dashboard landing", () => { expect(screen.getByTestId("api-keys-dashboard")).toBeInTheDocument(); expect(screen.queryByTestId("loading-screen")).not.toBeInTheDocument(); expect(mockReplace).not.toHaveBeenCalled(); - expect(mockUiHref).not.toHaveBeenCalledWith("connect"); }, ); @@ -108,7 +104,7 @@ describe("dashboard landing", () => { it("redirects an old ?page= bookmark to its path route without rendering the keys dashboard", () => { state.search = "page=logs"; render(); - expect(mockReplace).toHaveBeenCalledWith("/mocked-ui/logs"); + expect(mockReplace).toHaveBeenCalledWith("/logs"); expect(screen.getByTestId("loading-screen")).toBeInTheDocument(); expect(screen.queryByTestId("api-keys-dashboard")).not.toBeInTheDocument(); }); @@ -116,7 +112,7 @@ describe("dashboard landing", () => { it("carries the MCP env-var deep link's other params through the legacy redirect", () => { state.search = "page=mcp-servers&fill_env_vars=srv-1"; render(); - expect(mockReplace).toHaveBeenCalledWith("/mocked-ui/mcp-servers?fill_env_vars=srv-1"); + expect(mockReplace).toHaveBeenCalledWith("/mcp-servers?fill_env_vars=srv-1"); }); it("still sends the user to an explicit stored return URL", () => { diff --git a/ui/litellm-dashboard/src/app/chat/layout.test.tsx b/ui/litellm-dashboard/src/app/chat/layout.test.tsx index 6c78bca0b5a..347215e389f 100644 --- a/ui/litellm-dashboard/src/app/chat/layout.test.tsx +++ b/ui/litellm-dashboard/src/app/chat/layout.test.tsx @@ -2,7 +2,7 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import { render, screen } from "@testing-library/react"; import ChatLayout from "./layout"; -const { mockUseAuthorized, mockUseUISettings, mockReplace, mockUiHref, state } = vi.hoisted(() => { +const { mockUseAuthorized, mockUseUISettings, mockReplace, state } = vi.hoisted(() => { const state = { enableChatUI: false, isUISettingsLoading: false, @@ -10,7 +10,6 @@ const { mockUseAuthorized, mockUseUISettings, mockReplace, mockUiHref, state } = return { state, mockReplace: vi.fn(), - mockUiHref: vi.fn((segment: string) => `/mocked-ui/${segment}`), mockUseAuthorized: vi.fn(() => ({ accessToken: "token-123", userRole: "Internal User", @@ -30,7 +29,6 @@ vi.mock("next/navigation", () => ({ })); vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({ default: mockUseAuthorized })); vi.mock("@/app/(dashboard)/hooks/uiSettings/useUISettings", () => ({ useUISettings: mockUseUISettings })); -vi.mock("@/utils/uiHref", () => ({ uiHref: mockUiHref })); vi.mock("@/components/navbar", () => ({ default: () =>
})); vi.mock("@/contexts/ThemeContext", () => ({ ThemeProvider: ({ children }: { children: React.ReactNode }) => <>{children}, @@ -47,7 +45,6 @@ describe("ChatLayout", () => { state.enableChatUI = false; state.isUISettingsLoading = false; mockReplace.mockClear(); - mockUiHref.mockClear(); }); it("renders the chat shell when enable_chat_ui is on", () => { @@ -70,7 +67,7 @@ describe("ChatLayout", () => { , ); expect(screen.queryByTestId("chat-shell")).not.toBeInTheDocument(); - expect(mockReplace).toHaveBeenCalledWith("/mocked-ui/"); + expect(mockReplace).toHaveBeenCalledWith("/"); }); it("renders nothing while UI settings are still loading", () => { diff --git a/ui/litellm-dashboard/src/app/chat/layout.tsx b/ui/litellm-dashboard/src/app/chat/layout.tsx index 2e0db2c6bdc..63aa1aba7e6 100644 --- a/ui/litellm-dashboard/src/app/chat/layout.tsx +++ b/ui/litellm-dashboard/src/app/chat/layout.tsx @@ -8,7 +8,6 @@ import Navbar from "@/components/navbar"; import { ThemeProvider } from "@/contexts/ThemeContext"; import { ChatShellProvider } from "@/contexts/ChatShellContext"; import ChatShell from "@/components/chat/ChatShell"; -import { uiHref } from "@/utils/uiHref"; // ChatShellProvider uses useSearchParams(), which requires a Suspense boundary for static export. function ChatLayoutContent({ children }: { children: React.ReactNode }) { @@ -20,7 +19,7 @@ function ChatLayoutContent({ children }: { children: React.ReactNode }) { const blocked = !isUISettingsLoading && !chatEnabled; useEffect(() => { - if (blocked) router.replace(uiHref("")); + if (blocked) router.replace("/"); }, [blocked, router]); if (isUISettingsLoading || blocked) return null; diff --git a/ui/litellm-dashboard/src/app/login/LoginPage.tsx b/ui/litellm-dashboard/src/app/login/LoginPage.tsx index 11cf5ffad3d..63dc34b5d93 100644 --- a/ui/litellm-dashboard/src/app/login/LoginPage.tsx +++ b/ui/litellm-dashboard/src/app/login/LoginPage.tsx @@ -102,7 +102,7 @@ function LoginPageContent() { params.delete("code"); const cleanSearch = params.toString(); window.history.replaceState(null, "", window.location.pathname + (cleanSearch ? `?${cleanSearch}` : "")); - router.replace("/ui/?login=success"); + router.replace("/?login=success"); }); return; } @@ -120,9 +120,10 @@ function LoginPageContent() { // User already logged in - redirect to return URL or default const returnUrl = consumeReturnUrl(); if (returnUrl) { - router.replace(returnUrl); + // Browser-absolute (may be a full URL or a /ui path), so bypass the basePath-aware router + window.location.replace(returnUrl); } else { - router.replace("/ui"); + router.replace("/"); } return; } @@ -134,7 +135,7 @@ function LoginPageContent() { if (returnUrl && isValidReturnUrl(returnUrl)) { ssoUrl += `?redirect_to=${encodeURIComponent(returnUrl)}`; } - router.push(ssoUrl); + window.location.assign(ssoUrl); return; } @@ -156,15 +157,12 @@ function LoginPageContent() { if (selectedWorker) { selectWorker(selectedWorker.worker_id); // Stay on the CP's UI — proxyBaseUrl already points at the worker - router.push("/ui/?login=success"); + router.push("/?login=success"); } else { // Normal (non-control-plane) login — follow the server's redirect const returnUrl = consumeReturnUrl(); - if (returnUrl) { - router.push(returnUrl); - } else { - router.push(data.redirect_url); - } + // Both values are browser-absolute, so bypass the basePath-aware router + window.location.assign(returnUrl ?? data.redirect_url); } }, onError: () => { @@ -339,7 +337,7 @@ function LoginPageContent() { // include return_to so the callback redirects back here const ssoBase = selectedWorker?.url ?? getProxyBaseUrl(); const returnTo = encodeURIComponent(getLoginUrl(window.location.origin)); - router.push(`${ssoBase}/sso/key/generate?return_to=${returnTo}`); + window.location.assign(`${ssoBase}/sso/key/generate?return_to=${returnTo}`); }} className="w-full" > diff --git a/ui/litellm-dashboard/src/components/DashboardHeader.test.tsx b/ui/litellm-dashboard/src/components/DashboardHeader.test.tsx index b4b9950cdd0..a21dec44412 100644 --- a/ui/litellm-dashboard/src/components/DashboardHeader.test.tsx +++ b/ui/litellm-dashboard/src/components/DashboardHeader.test.tsx @@ -7,7 +7,7 @@ const { mockUsePluginMode, mockUseUISettings, state } = vi.hoisted(() => { const state = { plugins: [] as { name: string; display_name: string; url: string }[], enableChatUI: false, - pathname: "/ui/logs", + pathname: "/logs", }; return { state, @@ -32,18 +32,18 @@ describe("DashboardHeader breadcrumb", () => { afterEach(() => { state.plugins = []; state.enableChatUI = false; - state.pathname = "/ui/logs"; + state.pathname = "/logs"; }); it("titles the breadcrumb from the current route, not from a sidebar page id", () => { - state.pathname = "/ui/models-and-endpoints"; + state.pathname = "/models-and-endpoints"; render(); expect(screen.getByText("Models + Endpoints")).toBeInTheDocument(); }); it("titles the dashboard root as Virtual Keys", () => { - state.pathname = "/ui/"; + state.pathname = "/"; render(); expect(screen.getByText("Virtual Keys")).toBeInTheDocument(); diff --git a/ui/litellm-dashboard/src/components/Navbar/ViewSwitcher.test.tsx b/ui/litellm-dashboard/src/components/Navbar/ViewSwitcher.test.tsx index a32df932d05..ead8cb78f61 100644 --- a/ui/litellm-dashboard/src/components/Navbar/ViewSwitcher.test.tsx +++ b/ui/litellm-dashboard/src/components/Navbar/ViewSwitcher.test.tsx @@ -9,7 +9,7 @@ const { mockUsePluginMode, mockUseUISettings, mockUsePathname, state } = vi.hois plugins: [] as { name: string; display_name: string; url: string }[], activePlugin: null as { name: string; display_name: string; url: string } | null, enableChatUI: false, - pathname: "/ui/", + pathname: "/", }; return { state, @@ -45,7 +45,7 @@ describe("ViewSwitcher", () => { state.mode = "ai-gateway"; state.plugins = []; state.enableChatUI = false; - state.pathname = "/ui/"; + state.pathname = "/"; state.setMode.mockClear(); }); @@ -120,7 +120,7 @@ describe("ViewSwitcher", () => { it("navigates back to the dashboard when a mode entry is picked from the chat route", async () => { state.enableChatUI = true; - state.pathname = "/ui/chat"; + state.pathname = "/chat"; state.plugins = [{ name: "litellm-platform-plugin", display_name: "Chat UI", url: "http://localhost:3300" }]; render(); diff --git a/ui/litellm-dashboard/src/components/Navbar/ViewSwitcher.tsx b/ui/litellm-dashboard/src/components/Navbar/ViewSwitcher.tsx index b3aba7155d1..1dec4310991 100644 --- a/ui/litellm-dashboard/src/components/Navbar/ViewSwitcher.tsx +++ b/ui/litellm-dashboard/src/components/Navbar/ViewSwitcher.tsx @@ -28,9 +28,8 @@ export default function ViewSwitcher() { const chatEnabled = Boolean(uiSettings?.values?.enable_chat_ui); - const chatHref = uiHref(CHAT); const normalizedPathname = (pathname ?? "").replace(/\/+$/, ""); - const isChatRoute = chatEnabled && (normalizedPathname === chatHref || normalizedPathname.startsWith(`${chatHref}/`)); + const isChatRoute = chatEnabled && (normalizedPathname === `/${CHAT}` || normalizedPathname.startsWith(`/${CHAT}/`)); const activeLabel = isChatRoute ? "Chat" : plugins.find((p) => p.name === mode)?.display_name ?? "AI Gateway"; diff --git a/ui/litellm-dashboard/src/components/chat/ChatShell.test.tsx b/ui/litellm-dashboard/src/components/chat/ChatShell.test.tsx index bff6d30a5c8..819be793c5b 100644 --- a/ui/litellm-dashboard/src/components/chat/ChatShell.test.tsx +++ b/ui/litellm-dashboard/src/components/chat/ChatShell.test.tsx @@ -4,7 +4,7 @@ import ChatShell from "./ChatShell"; const { mockPush, mockUsePathname, mockUseChatShell } = vi.hoisted(() => ({ mockPush: vi.fn(), - mockUsePathname: vi.fn(() => "/ui/chat"), + mockUsePathname: vi.fn(() => "/chat"), mockUseChatShell: vi.fn(() => ({ conversations: [], activeConversationId: null, @@ -18,14 +18,13 @@ vi.mock("next/navigation", () => ({ usePathname: mockUsePathname, })); // Deterministic hrefs so navigation/active-state assertions don't depend on server_root_path. -vi.mock("@/utils/uiHref", () => ({ uiHref: (seg: string) => `/ui/${seg}`.replace(/\/$/, "") || "/ui" })); vi.mock("@/contexts/ChatShellContext", () => ({ useChatShell: mockUseChatShell })); vi.mock("./ConversationList", () => ({ default: () =>
})); describe("ChatShell", () => { afterEach(() => { mockPush.mockClear(); - mockUsePathname.mockReturnValue("/ui/chat"); + mockUsePathname.mockReturnValue("/chat"); }); it("marks Chats active and shows the conversation list on the base chat route", () => { @@ -40,7 +39,7 @@ describe("ChatShell", () => { }); it("marks API Keys active while still showing the conversation list", () => { - mockUsePathname.mockReturnValue("/ui/chat/api-keys"); + mockUsePathname.mockReturnValue("/chat/api-keys"); render(
@@ -58,17 +57,17 @@ describe("ChatShell", () => { , ); fireEvent.click(screen.getByRole("button", { name: "Integrations" })); - expect(mockPush).toHaveBeenCalledWith("/ui/chat/integrations"); + expect(mockPush).toHaveBeenCalledWith("/chat/integrations"); fireEvent.click(screen.getByRole("button", { name: "Usage" })); - expect(mockPush).toHaveBeenCalledWith("/ui/chat/usage"); + expect(mockPush).toHaveBeenCalledWith("/chat/usage"); fireEvent.click(screen.getByRole("button", { name: "Logs" })); - expect(mockPush).toHaveBeenCalledWith("/ui/chat/logs"); + expect(mockPush).toHaveBeenCalledWith("/chat/logs"); }); it("marks Logs active on the logs route", () => { - mockUsePathname.mockReturnValue("/ui/chat/logs"); + mockUsePathname.mockReturnValue("/chat/logs"); render(
@@ -79,7 +78,7 @@ describe("ChatShell", () => { }); it("tolerates a trailing slash on the current pathname when matching the active route", () => { - mockUsePathname.mockReturnValue("/ui/chat/usage/"); + mockUsePathname.mockReturnValue("/chat/usage/"); render(
diff --git a/ui/litellm-dashboard/src/components/chat/ChatShell.tsx b/ui/litellm-dashboard/src/components/chat/ChatShell.tsx index 7d144944d64..c5b31ac5e33 100644 --- a/ui/litellm-dashboard/src/components/chat/ChatShell.tsx +++ b/ui/litellm-dashboard/src/components/chat/ChatShell.tsx @@ -5,12 +5,11 @@ import { usePathname, useRouter } from "next/navigation"; import { Plus, MessageSquare, LayoutGrid, KeyRound, Lock, BarChart3, ScrollText } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Separator } from "@/components/ui/separator"; -import { uiHref } from "@/utils/uiHref"; import { useChatShell } from "@/contexts/ChatShellContext"; import ConversationList from "./ConversationList"; export function getChatRoutes() { - const base = uiHref("chat"); + const base = "/chat"; return { chats: base, integrations: `${base}/integrations`, diff --git a/ui/litellm-dashboard/src/components/chat/ConnectFlowSurface.tsx b/ui/litellm-dashboard/src/components/chat/ConnectFlowSurface.tsx index 33a61fceacf..5d00c5c3955 100644 --- a/ui/litellm-dashboard/src/components/chat/ConnectFlowSurface.tsx +++ b/ui/litellm-dashboard/src/components/chat/ConnectFlowSurface.tsx @@ -2,6 +2,7 @@ import React, { useEffect } from "react"; import { useRouter, useSearchParams } from "next/navigation"; +import { appHrefFromUiHref } from "@/utils/uiHref"; import { useQuery } from "@tanstack/react-query"; import MCPAppsPanel from "@/components/chat/MCPAppsPanel"; import ConnectFlowBanner from "@/components/chat/ConnectFlowBanner"; @@ -24,7 +25,7 @@ const ConnectFlowSurface: React.FC = ({ accessToken, selectedServers, onC if (oauthReturn) { const url = new URL(window.location.href); url.searchParams.delete("mcpOauthReturn"); - router.replace(url.pathname + url.search); + router.replace(appHrefFromUiHref(url.pathname + url.search)); } }, [oauthReturn, router]); diff --git a/ui/litellm-dashboard/src/components/leftnav.test.tsx b/ui/litellm-dashboard/src/components/leftnav.test.tsx index 6eb0218c41d..94169580f34 100644 --- a/ui/litellm-dashboard/src/components/leftnav.test.tsx +++ b/ui/litellm-dashboard/src/components/leftnav.test.tsx @@ -17,7 +17,7 @@ vi.mock("../utils/roles", async (importOriginal) => { }; }); -const navState = vi.hoisted(() => ({ pathname: "/ui/api-keys" })); +const navState = vi.hoisted(() => ({ pathname: "/api-keys" })); vi.mock("next/navigation", () => ({ usePathname: () => navState.pathname, @@ -111,13 +111,13 @@ describe("Sidebar (leftnav)", () => { mockUseAuthorized.mockReset(); mockUseOrganizations.mockReset(); mockUseThemeImpl = unbrandedTheme; - navState.pathname = "/ui/api-keys"; + navState.pathname = "/api-keys"; }); it("should link the logo to the UI home route rather than the proxy origin", () => { renderWithProviders(); - expect(screen.getByRole("link", { name: /litellm home/i })).toHaveAttribute("href", "/ui"); + expect(screen.getByRole("link", { name: /litellm home/i })).toHaveAttribute("href", "/"); }); it("pairs the logo with a dark-mode variant that swaps on the dark class", () => { @@ -515,20 +515,20 @@ describe("Sidebar (leftnav)", () => { }); it("marks the nav item for the current route active", () => { - navState.pathname = "/ui/logs"; + navState.pathname = "/logs"; renderWithProviders(); expect(screen.getByRole("link", { name: "Logs" })).toHaveAttribute("data-active", "true"); expect(screen.getByRole("link", { name: "Virtual Keys" })).not.toHaveAttribute("data-active"); }); it("marks Virtual Keys active at the dashboard root", () => { - navState.pathname = "/ui/"; + navState.pathname = "/"; renderWithProviders(); expect(screen.getByRole("link", { name: "Virtual Keys" })).toHaveAttribute("data-active", "true"); }); it("expands the parent group of the current nested route and marks the child active", () => { - navState.pathname = "/ui/search-tools"; + navState.pathname = "/search-tools"; renderWithProviders(); expect(screen.getByRole("link", { name: "Search Tools" })).toHaveAttribute("data-active", "true"); expect(screen.getByRole("button", { name: "Tools" })).toHaveAttribute("aria-expanded", "true"); @@ -542,12 +542,12 @@ describe("Sidebar (leftnav)", () => { const expectHref = (label: string, href: string) => expect(screen.getByRole("link", { name: label })).toHaveAttribute("href", href); - expectHref("Virtual Keys", "/ui/api-keys"); - expectHref("Playground", "/ui/playground"); - expectHref("Models + Endpoints", "/ui/models-and-endpoints"); - expectHref("Usage", "/ui/usage"); - expectHref("API Reference", "/ui/api-reference"); - expectHref("Old Usage", "/ui/old-usage"); + expectHref("Virtual Keys", "/api-keys"); + expectHref("Playground", "/playground"); + expectHref("Models + Endpoints", "/models-and-endpoints"); + expectHref("Usage", "/usage"); + expectHref("API Reference", "/api-reference"); + expectHref("Old Usage", "/old-usage"); }); it("never links a leaf to the legacy ?page= switch", () => { @@ -559,7 +559,7 @@ describe("Sidebar (leftnav)", () => { } const hrefs = screen.getAllByRole("link").map((link) => link.getAttribute("href") ?? ""); expect(hrefs.filter((href) => href.includes("page="))).toHaveLength(0); - expect(hrefs.filter((href) => href.startsWith("/ui/")).length).toBeGreaterThan(30); + expect(hrefs.filter((href) => href.startsWith("/")).length).toBeGreaterThan(30); }); it("hides labels but keeps items reachable (icon + link) when collapsed to the rail", () => { @@ -596,29 +596,29 @@ describe("Sidebar (leftnav)", () => { describe("getBreadcrumb", () => { it("resolves a top-level route to its section + title", () => { - expect(getBreadcrumb("/ui/api-keys")).toEqual({ section: "AI Gateway", title: "Virtual Keys" }); - expect(getBreadcrumb("/ui/logs")).toEqual({ section: "Observability", title: "Logs" }); + expect(getBreadcrumb("/api-keys")).toEqual({ section: "AI Gateway", title: "Virtual Keys" }); + expect(getBreadcrumb("/logs")).toEqual({ section: "Observability", title: "Logs" }); }); it("resolves routes whose segment differs from the sidebar page id", () => { - expect(getBreadcrumb("/ui/models-and-endpoints")).toEqual({ section: "AI Gateway", title: "Models + Endpoints" }); - expect(getBreadcrumb("/ui/usage")).toEqual({ section: "Observability", title: "Usage" }); - expect(getBreadcrumb("/ui/old-usage")).toEqual({ section: "Developer Tools", title: "Old Usage" }); + expect(getBreadcrumb("/models-and-endpoints")).toEqual({ section: "AI Gateway", title: "Models + Endpoints" }); + expect(getBreadcrumb("/usage")).toEqual({ section: "Observability", title: "Usage" }); + expect(getBreadcrumb("/old-usage")).toEqual({ section: "Developer Tools", title: "Old Usage" }); }); it("titles the dashboard root as Virtual Keys", () => { - expect(getBreadcrumb("/ui/")).toEqual({ section: "AI Gateway", title: "Virtual Keys" }); + expect(getBreadcrumb("/")).toEqual({ section: "AI Gateway", title: "Virtual Keys" }); }); it("resolves a nested child route to its parent section", () => { - expect(getBreadcrumb("/ui/search-tools/")).toEqual({ section: "AI Gateway", title: "Search Tools" }); + expect(getBreadcrumb("/search-tools/")).toEqual({ section: "AI Gateway", title: "Search Tools" }); }); it("resolves router-settings under the Settings section", () => { - expect(getBreadcrumb("/ui/router-settings")).toEqual({ section: "Settings", title: "Router Settings" }); + expect(getBreadcrumb("/router-settings")).toEqual({ section: "Settings", title: "Router Settings" }); }); it("falls back to a prettified title with no section for unknown routes", () => { - expect(getBreadcrumb("/ui/some-unknown-page")).toEqual({ section: null, title: "Some Unknown Page" }); + expect(getBreadcrumb("/some-unknown-page")).toEqual({ section: null, title: "Some Unknown Page" }); }); }); diff --git a/ui/litellm-dashboard/src/components/leftnav.tsx b/ui/litellm-dashboard/src/components/leftnav.tsx index 9d772f45153..7fdbd348470 100644 --- a/ui/litellm-dashboard/src/components/leftnav.tsx +++ b/ui/litellm-dashboard/src/components/leftnav.tsx @@ -77,7 +77,7 @@ import { import BetaBadge from "./BetaBadge"; import SidebarAccountMenu from "./SidebarAccountMenu/SidebarAccountMenu"; import SidebarUsageCard from "./SidebarUsageCard"; -import { routeSegmentForPathname, uiHref } from "@/utils/uiHref"; +import { routeSegmentForPathname } from "@/utils/uiHref"; const ICON = { strokeWidth: 1.75 } as const; @@ -547,7 +547,7 @@ const Sidebar_: React.FC = ({ return ( = ({
- + LiteLLM = ({ )}
- +
LiteLLM Brand diff --git a/ui/litellm-dashboard/src/components/shared/EntityLink.test.tsx b/ui/litellm-dashboard/src/components/shared/EntityLink.test.tsx index 3d6fc9a435b..b72e85f1eb2 100644 --- a/ui/litellm-dashboard/src/components/shared/EntityLink.test.tsx +++ b/ui/litellm-dashboard/src/components/shared/EntityLink.test.tsx @@ -18,11 +18,11 @@ describe("EntityLink", () => { expect(screen.getByRole("link", { name: "alice" })).toHaveAttribute("href", "/ui/users?user=u1"); }); - it("navigates client-side on plain click", async () => { + it("navigates client-side with the /ui base stripped so the basePath router does not double-prefix", async () => { const user = userEvent.setup(); render(alice); await user.click(screen.getByRole("link", { name: "alice" })); - expect(push).toHaveBeenCalledWith("/ui/users?user=u1"); + expect(push).toHaveBeenCalledWith("/users?user=u1"); }); it("renders the label as plain text when there is no href to point at", () => { diff --git a/ui/litellm-dashboard/src/components/shared/EntityLink.tsx b/ui/litellm-dashboard/src/components/shared/EntityLink.tsx index 4885835a42c..34edb515ac4 100644 --- a/ui/litellm-dashboard/src/components/shared/EntityLink.tsx +++ b/ui/litellm-dashboard/src/components/shared/EntityLink.tsx @@ -5,6 +5,7 @@ import { useRouter } from "next/navigation"; import * as React from "react"; import { cn } from "@/lib/cva.config"; +import { appHrefFromUiHref } from "@/utils/uiHref"; export function useEntityLinkClick(href: string): (e: React.MouseEvent) => void { const router = useRouter(); @@ -14,7 +15,7 @@ export function useEntityLinkClick(href: string): (e: React.MouseEvent) => void const isNativeNewTabClick = hasModifierKey || e.button === 1; if (isNativeNewTabClick) return; e.preventDefault(); - router.push(href); + router.push(appHrefFromUiHref(href)); }; } diff --git a/ui/litellm-dashboard/src/utils/uiHref.test.ts b/ui/litellm-dashboard/src/utils/uiHref.test.ts index dae2594929c..abadc0c3f36 100644 --- a/ui/litellm-dashboard/src/utils/uiHref.test.ts +++ b/ui/litellm-dashboard/src/utils/uiHref.test.ts @@ -1,10 +1,9 @@ -import { afterEach, describe, expect, it, vi } from "vitest"; +import { afterEach, describe, expect, it } from "vitest"; import { setServerRootPath } from "@/lib/serverRootPath"; -import { routeSegmentForPathname, uiHref } from "./uiHref"; +import { appHrefFromUiHref, routeSegmentForPathname, uiHref } from "./uiHref"; afterEach(() => { setServerRootPath("/"); - vi.unstubAllEnvs(); }); describe("uiHref", () => { @@ -20,36 +19,38 @@ describe("uiHref", () => { it("tolerates a leading slash in the route segment", () => { expect(uiHref("/api-reference")).toBe("/ui/api-reference"); }); +}); - it("stays root-relative under next dev, which serves the app at /", () => { - vi.stubEnv("NODE_ENV", "development"); - expect(uiHref("logs")).toBe("/logs"); +describe("appHrefFromUiHref", () => { + it("strips the /ui base so the basePath-aware router does not double-prefix", () => { + expect(appHrefFromUiHref("/ui/teams?team=x")).toBe("/teams?team=x"); + }); + + it("maps the bare base to the app root", () => { + expect(appHrefFromUiHref("/ui")).toBe("/"); + }); + + it("strips a non-root serverRootPath base too", () => { + setServerRootPath("/team-x/"); + expect(appHrefFromUiHref("/team-x/ui/guardrails")).toBe("/guardrails"); + }); + + it("passes through an href that is already app-relative", () => { + expect(appHrefFromUiHref("/guardrails")).toBe("/guardrails"); }); }); describe("routeSegmentForPathname", () => { - it("strips the /ui base and any trailing slash", () => { - expect(routeSegmentForPathname("/ui/api-reference")).toBe("api-reference"); - expect(routeSegmentForPathname("/ui/api-reference/")).toBe("api-reference"); + it("reads the first segment of the basePath-stripped pathname", () => { + expect(routeSegmentForPathname("/api-reference")).toBe("api-reference"); + expect(routeSegmentForPathname("/api-reference/")).toBe("api-reference"); }); it("returns an empty segment for the dashboard root", () => { - expect(routeSegmentForPathname("/ui/")).toBe(""); - expect(routeSegmentForPathname("/ui")).toBe(""); + expect(routeSegmentForPathname("/")).toBe(""); }); it("keeps only the first segment of a nested path", () => { - expect(routeSegmentForPathname("/ui/models-and-endpoints/anything")).toBe("models-and-endpoints"); - }); - - it("strips a non-root serverRootPath too", () => { - setServerRootPath("/team-x/"); - expect(routeSegmentForPathname("/team-x/ui/guardrails")).toBe("guardrails"); - }); - - it("reads the segment straight after / under next dev", () => { - vi.stubEnv("NODE_ENV", "development"); - expect(routeSegmentForPathname("/logs")).toBe("logs"); - expect(routeSegmentForPathname("/")).toBe(""); + expect(routeSegmentForPathname("/models-and-endpoints/anything")).toBe("models-and-endpoints"); }); }); diff --git a/ui/litellm-dashboard/src/utils/uiHref.ts b/ui/litellm-dashboard/src/utils/uiHref.ts index 83e65b4d5a3..64dcd8757a9 100644 --- a/ui/litellm-dashboard/src/utils/uiHref.ts +++ b/ui/litellm-dashboard/src/utils/uiHref.ts @@ -1,23 +1,27 @@ import { serverRootPath } from "@/lib/serverRootPath"; function uiBase(): string { - // next dev serves the app at the root; only the proxy mounts the static export under /ui - // (and optionally under server_root_path). Inlined at build time, so production is unaffected. - if (process.env.NODE_ENV === "development") { - return ""; - } const root = serverRootPath && serverRootPath !== "/" ? `/${serverRootPath.replace(/^\/+|\/+$/g, "")}` : ""; return `${root}/ui`; } -/** Absolute (same-origin) href for a dashboard route segment, e.g. "api-reference" -> "/ui/api-reference". */ +/** + * Browser-absolute (same-origin) href for a dashboard route segment, e.g. "api-reference" -> "/ui/api-reference". + * Use for raw tags and window.location navigations. For and router.push, pass the app-relative + * path ("/api-reference") instead: the router prepends the /ui basePath itself. + */ export function uiHref(routeSegment: string): string { return `${uiBase()}/${routeSegment.replace(/^\/+/, "")}`; } -/** First route segment under the UI base, e.g. "/ui/api-reference/" -> "api-reference" and "/ui/" -> "". */ -export function routeSegmentForPathname(pathname: string): string { +/** App-relative path for router.push, from a uiHref-built href: "/ui/teams?team=x" -> "/teams?team=x". */ +export function appHrefFromUiHref(href: string): string { const base = uiBase(); - const relative = pathname.startsWith(base) ? pathname.slice(base.length) : pathname; - return relative.replace(/^\/+/, "").split("/")[0]; + if (href === base) return "/"; + return href.startsWith(`${base}/`) || href.startsWith(`${base}?`) ? href.slice(base.length) : href; +} + +/** First route segment of an app-relative pathname (usePathname already strips the /ui basePath). */ +export function routeSegmentForPathname(pathname: string): string { + return pathname.replace(/^\/+/, "").split("/")[0]; }