mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
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 <a> 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 <noreply@anthropic.com>
This commit is contained in:
parent
db37977307
commit
cab5fb75af
21 changed files with 122 additions and 127 deletions
|
|
@ -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=<root>/ui
|
||||
// (see build_ui_custom_path.sh).
|
||||
basePath: process.env.UI_BASE_PATH || "/ui",
|
||||
assetPrefix: "/litellm-asset-prefix",
|
||||
trailingSlash: true,
|
||||
turbopack: {
|
||||
|
|
|
|||
|
|
@ -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]);
|
||||
|
||||
|
|
|
|||
|
|
@ -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}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import { uiHref } from "@/utils/uiHref";
|
||||
|
||||
const LEGACY_PAGE_ROUTES: ReadonlyMap<string, string> = 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}`;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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: () => <div data-testid="loading-screen" />,
|
||||
}));
|
||||
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(<CreateKeyPage />);
|
||||
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(<CreateKeyPage />);
|
||||
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", () => {
|
||||
|
|
|
|||
|
|
@ -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: () => <div data-testid="navbar" /> }));
|
||||
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", () => {
|
|||
</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", () => {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
>
|
||||
|
|
|
|||
|
|
@ -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(<DashboardHeader />);
|
||||
|
||||
expect(screen.getByText("Models + Endpoints")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("titles the dashboard root as Virtual Keys", () => {
|
||||
state.pathname = "/ui/";
|
||||
state.pathname = "/";
|
||||
render(<DashboardHeader />);
|
||||
|
||||
expect(screen.getByText("Virtual Keys")).toBeInTheDocument();
|
||||
|
|
|
|||
|
|
@ -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(<ViewSwitcher />);
|
||||
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
||||
|
|
|
|||
|
|
@ -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: () => <div data-testid="conversation-list" /> }));
|
||||
|
||||
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(
|
||||
<ChatShell>
|
||||
<div />
|
||||
|
|
@ -58,17 +57,17 @@ describe("ChatShell", () => {
|
|||
</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(
|
||||
<ChatShell>
|
||||
<div />
|
||||
|
|
@ -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(
|
||||
<ChatShell>
|
||||
<div />
|
||||
|
|
|
|||
|
|
@ -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`,
|
||||
|
|
|
|||
|
|
@ -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<Props> = ({ 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]);
|
||||
|
||||
|
|
|
|||
|
|
@ -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(<Sidebar {...defaultProps} />);
|
||||
|
||||
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(<Sidebar {...defaultProps} />);
|
||||
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(<Sidebar {...defaultProps} />);
|
||||
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(<Sidebar {...defaultProps} />);
|
||||
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" });
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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<SidebarProps> = ({
|
|||
return (
|
||||
<Link
|
||||
key={item.key}
|
||||
href={uiHref(routeOf(item))}
|
||||
href={`/${routeOf(item)}`}
|
||||
title={collapsed ? labelText(item) : undefined}
|
||||
data-active={active || undefined}
|
||||
className={cn(sidebarMenuButtonVariants({ isActive: active, size }))}
|
||||
|
|
@ -603,7 +603,7 @@ const Sidebar_: React.FC<SidebarProps> = ({
|
|||
<SidebarHeader className="h-14 border-b border-border group-data-[collapsed=true]/sidebar:h-auto">
|
||||
<div className="flex items-center justify-between gap-2 group-data-[collapsed=true]/sidebar:flex-col">
|
||||
<div className="flex min-w-0 items-center gap-2">
|
||||
<Link href={uiHref("")} className="flex min-w-0 items-center" aria-label="LiteLLM home">
|
||||
<Link href="/" className="flex min-w-0 items-center" aria-label="LiteLLM home">
|
||||
<img src={logoSrc} alt="LiteLLM" className={cn(LOGO_CLASS_NAME, "dark:hidden")} />
|
||||
<img
|
||||
src={darkLogoSrc}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ import { useDisableBouncingIcon } from "@/app/(dashboard)/hooks/useDisableBounci
|
|||
import { useDisableShowPrompts } from "@/app/(dashboard)/hooks/useDisableShowPrompts";
|
||||
import { useWorker } from "@/hooks/useWorker";
|
||||
import { getProxyBaseUrl } from "@/components/networking";
|
||||
import { uiHref } from "@/utils/uiHref";
|
||||
import { useTheme } from "@/contexts/ThemeContext";
|
||||
import { clearTokenCookies } from "@/utils/cookieUtils";
|
||||
import { clearStoredReturnUrl, getLoginUrl } from "@/utils/returnUrlUtils";
|
||||
|
|
@ -87,7 +86,7 @@ const Navbar: React.FC<NavbarProps> = ({
|
|||
)}
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<Link href={uiHref("")} className="flex items-center">
|
||||
<Link href="/" className="flex items-center">
|
||||
<div className="relative">
|
||||
<div className="flex h-10 max-w-48 items-center justify-center overflow-hidden">
|
||||
<img src={imageUrl} alt="LiteLLM Brand" className={cn(NAV_LOGO_CLASS_NAME, "dark:hidden")} />
|
||||
|
|
|
|||
|
|
@ -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(<EntityLink href="/ui/users?user=u1">alice</EntityLink>);
|
||||
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", () => {
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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 <a> tags and window.location navigations. For <Link> 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];
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue