Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(self-host): support for custom base path via `NEXT_PUBLIC_BASE_PATH` (#3346)

Marc Klingen96ace71f9a01
diff --git a/.env.prod.example b/.env.prod.example
index cad53cbe67..0ab38c103a 100644
--- a/.env.prod.example
+++ b/.env.prod.example
@@ -28,6 +28,9 @@ ENCRYPTION_KEY="0000000000000000000000000000000000000000000000000000000000000000
# Use CSP headers to enforce HTTPS, optional
# LANGFUSE_CSP_ENFORCE_HTTPS="true"
+# Configure base path for self-hosting, optional
+# Note: You need to build the docker image with the base path set and cannot use the pre-built docker image if you set this.
+# NEXT_PUBLIC_BASE_PATH="/app"
# Docker only, optional
# PORT=3000
diff --git a/web/Dockerfile b/web/Dockerfile
index c99d0ac07c..7d00f8abdd 100644
--- a/web/Dockerfile
+++ b/web/Dockerfile
@@ -44,6 +44,7 @@ ARG NEXT_PUBLIC_CRISP_WEBSITE_ID
ARG NEXT_LANGFUSE_TRACING_SAMPLE_RATE
ARG NEXT_PUBLIC_SENTRY_ENVIRONMENT
ARG NEXT_PUBLIC_SENTRY_DSN
+ARG NEXT_PUBLIC_BASE_PATH
# Accept build id as NEXT_PUBLIC_BUILD_ID or PORTER_NEXT_PUBLIC_BUILD_ID
ARG PORTER_NEXT_PUBLIC_BUILD_ID
diff --git a/web/next.config.mjs b/web/next.config.mjs
index b016173b9a..a7c8fcb88c 100644
--- a/web/next.config.mjs
+++ b/web/next.config.mjs
@@ -55,6 +55,7 @@ const nextConfig = {
],
},
poweredByHeader: false,
+ basePath: env.NEXT_PUBLIC_BASE_PATH,
/**
* If you have `experimental: { appDir: true }` set, then you must comment the below `i18n` config
@@ -104,22 +105,22 @@ const nextConfig = {
// Required to check authentication status from langfuse.com
...(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION !== undefined
? [
- {
- source: "/api/auth/session",
- headers: [
- {
- key: "Access-Control-Allow-Origin",
- value: "https://langfuse.com",
- },
- { key: "Access-Control-Allow-Credentials", value: "true" },
- { key: "Access-Control-Allow-Methods", value: "GET,POST" },
- {
- key: "Access-Control-Allow-Headers",
- value: "Content-Type, Authorization",
- },
- ],
- },
- ]
+ {
+ source: "/api/auth/session",
+ headers: [
+ {
+ key: "Access-Control-Allow-Origin",
+ value: "https://langfuse.com",
+ },
+ { key: "Access-Control-Allow-Credentials", value: "true" },
+ { key: "Access-Control-Allow-Methods", value: "GET,POST" },
+ {
+ key: "Access-Control-Allow-Headers",
+ value: "Content-Type, Authorization",
+ },
+ ],
+ },
+ ]
: []),
// all files in /public/generated are public and can be accessed from any origin, e.g. to render an API reference based on our openapi schema
{
diff --git a/web/src/components/LangfuseLogo.tsx b/web/src/components/LangfuseLogo.tsx
index bc14b14844..e4c6f6a2d9 100644
--- a/web/src/components/LangfuseLogo.tsx
+++ b/web/src/components/LangfuseLogo.tsx
@@ -2,6 +2,7 @@ import { cn } from "@/src/utils/tailwind";
import Link from "next/link";
import { EnvLabel } from "./EnvLabel";
import { VersionLabel } from "./VersionLabel";
+import { env } from "@/src/env.mjs";
export const LangfuseIcon = ({
size = 32,
@@ -12,7 +13,7 @@ export const LangfuseIcon = ({
}) => (
// eslint-disable-next-line @next/next/no-img-element
<img
- src="/icon.svg"
+ src={`${env.NEXT_PUBLIC_BASE_PATH}/icon.svg`}
width={size}
height={size}
alt="Langfuse Icon"
diff --git a/web/src/components/layouts/layout.tsx b/web/src/components/layouts/layout.tsx
index f4235299eb..b321647eeb 100644
--- a/web/src/components/layouts/layout.tsx
+++ b/web/src/components/layouts/layout.tsx
@@ -39,9 +39,7 @@ const signOutUser = async () => {
localStorage.clear();
sessionStorage.clear();
- await signOut({
- callbackUrl: "/auth/sign-in",
- });
+ await signOut();
};
const userNavigation = [
diff --git a/web/src/ee/features/playground/page/context/index.tsx b/web/src/ee/features/playground/page/context/index.tsx
index d350fd562d..b378c5c568 100644
--- a/web/src/ee/features/playground/page/context/index.tsx
+++ b/web/src/ee/features/playground/page/context/index.tsx
@@ -26,6 +26,7 @@ import {
import type { MessagesContext } from "@/src/components/ChatMessages/types";
import type { ModelParamsContext } from "@/src/components/ModelParameters";
+import { env } from "@/src/env.mjs";
type PlaygroundContextType = {
promptVariables: PromptVariable[];
@@ -270,11 +271,14 @@ async function* getChatCompletionStream(
messages,
modelParams: getFinalModelParams(modelParams),
});
- const result = await fetch("/api/chatCompletion", {
- method: "POST",
- headers: { "Content-Type": "application/json" },
- body,
- });
+ const result = await fetch(
+ `${env.NEXT_PUBLIC_BASE_PATH}/api/chatCompletion`,
+ {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body,
+ },
+ );
if (!result.ok) {
const errorData = await result.json();
diff --git a/web/src/env.mjs b/web/src/env.mjs
index 7e07905ac8..d0d4afdac9 100644
--- a/web/src/env.mjs
+++ b/web/src/env.mjs
@@ -204,6 +204,7 @@ export const env = createEnv({
NEXT_PUBLIC_POSTHOG_HOST: z.string().optional(),
NEXT_PUBLIC_CRISP_WEBSITE_ID: z.string().optional(),
NEXT_PUBLIC_BUILD_ID: z.string().optional(),
+ NEXT_PUBLIC_BASE_PATH: z.string().optional().default(""),
},
/**
@@ -342,6 +343,7 @@ export const env = createEnv({
LANGFUSE_INIT_USER_EMAIL: process.env.LANGFUSE_INIT_USER_EMAIL,
LANGFUSE_INIT_USER_NAME: process.env.LANGFUSE_INIT_USER_NAME,
LANGFUSE_INIT_USER_PASSWORD: process.env.LANGFUSE_INIT_USER_PASSWORD,
+ NEXT_PUBLIC_BASE_PATH: process.env.NEXT_PUBLIC_BASE_PATH,
},
// Skip validation in Docker builds
// DOCKER_BUILD is set in Dockerfile
diff --git a/web/src/features/auth-credentials/components/ResetPasswordButton.tsx b/web/src/features/auth-credentials/components/ResetPasswordButton.tsx
index 5e80eb2c7b..f85d135fe3 100644
--- a/web/src/features/auth-credentials/components/ResetPasswordButton.tsx
+++ b/web/src/features/auth-credentials/components/ResetPasswordButton.tsx
@@ -3,6 +3,7 @@ import { Button } from "@/src/components/ui/button";
import { useEffect, useState } from "react";
import { z } from "zod";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
+import { env } from "@/src/env.mjs";
export function RequestResetPasswordEmailButton({
email,
@@ -33,7 +34,7 @@ export function RequestResetPasswordEmailButton({
try {
const res = await signIn("email", {
email: email,
- callbackUrl: "/auth/reset-password",
+ callbackUrl: `${env.NEXT_PUBLIC_BASE_PATH}/auth/reset-password`,
redirect: false,
});
if (res?.error) {
diff --git a/web/src/features/projects/components/HostNameProject.tsx b/web/src/features/projects/components/HostNameProject.tsx
index 010a751b6f..ab16813791 100644
--- a/web/src/features/projects/components/HostNameProject.tsx
+++ b/web/src/features/projects/components/HostNameProject.tsx
@@ -2,6 +2,7 @@ import { Card } from "@/src/components/ui/card";
import { CodeView } from "@/src/components/ui/CodeJsonViewer";
import Header from "@/src/components/layouts/header";
import { useUiCustomization } from "@/src/ee/features/ui-customization/useUiCustomization";
+import { env } from "@/src/env.mjs";
export function HostNameProject() {
const uiCustomization = useUiCustomization();
@@ -13,7 +14,9 @@ export function HostNameProject() {
<div className="mb-2 text-sm">
When connecting to Langfuse, use this hostname / baseurl.
</div>
- <CodeView content={uiCustomization?.hostname ?? window.origin} />
+ <CodeView
+ content={`${uiCustomization?.hostname ?? window.origin}${env.NEXT_PUBLIC_BASE_PATH}`}
+ />
</div>
</Card>
</div>
diff --git a/web/src/features/public-api/components/CreateApiKeyButton.tsx b/web/src/features/public-api/components/CreateApiKeyButton.tsx
index 4480374f18..08d53d4a83 100644
--- a/web/src/features/public-api/components/CreateApiKeyButton.tsx
+++ b/web/src/features/public-api/components/CreateApiKeyButton.tsx
@@ -13,6 +13,7 @@ import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAcces
import { QuickstartExamples } from "@/src/features/public-api/components/QuickstartExamples";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import { useUiCustomization } from "@/src/ee/features/ui-customization/useUiCustomization";
+import { env } from "@/src/env.mjs";
export function CreateApiKeyButton(props: { projectId: string }) {
const utils = api.useUtils();
@@ -108,7 +109,9 @@ export const ApiKeyRender = ({
</div>
<div>
<div className="text-md mb-2 font-semibold">Host</div>
- <CodeView content={uiCustomization?.hostname ?? window.origin} />
+ <CodeView
+ content={`${uiCustomization?.hostname ?? window.origin}${env.NEXT_PUBLIC_BASE_PATH}`}
+ />
</div>
</>
);
diff --git a/web/src/features/public-api/components/QuickstartExamples.tsx b/web/src/features/public-api/components/QuickstartExamples.tsx
index 4caffb4c17..0256445095 100644
--- a/web/src/features/public-api/components/QuickstartExamples.tsx
+++ b/web/src/features/public-api/components/QuickstartExamples.tsx
@@ -6,6 +6,7 @@ import {
TabsTrigger,
} from "@/src/components/ui/tabs";
import { useUiCustomization } from "@/src/ee/features/ui-customization/useUiCustomization";
+import { env } from "@/src/env.mjs";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import Link from "next/link";
@@ -27,7 +28,7 @@ export const QuickstartExamples = ({
{ value: "llamaindex", label: "LlamaIndex" },
{ value: "other", label: "Other" },
];
- const host = uiCustomization?.hostname ?? window.origin;
+ const host = `${uiCustomization?.hostname ?? window.origin}${env.NEXT_PUBLIC_BASE_PATH}`;
// if custom docs link, do not show quickstart examples but refer to docs
if (uiCustomization?.documentationHref) {
diff --git a/web/src/pages/_app.tsx b/web/src/pages/_app.tsx
index 1ff27b03b8..1b3ae8d0f2 100644
--- a/web/src/pages/_app.tsx
+++ b/web/src/pages/_app.tsx
@@ -96,6 +96,11 @@ const MyApp: AppType<{ session: Session | null }> = ({
session={session}
refetchOnWindowFocus={true}
refetchInterval={5 * 60} // 5 minutes
+ basePath={
+ env.NEXT_PUBLIC_BASE_PATH
+ ? `${env.NEXT_PUBLIC_BASE_PATH}/api/auth`
+ : undefined
+ }
>
<DetailPageListsProvider>
<MarkdownContextProvider>
diff --git a/web/src/pages/auth/sign-in.tsx b/web/src/pages/auth/sign-in.tsx
index 266ecb838e..266b9db733 100644
--- a/web/src/pages/auth/sign-in.tsx
+++ b/web/src/pages/auth/sign-in.tsx
@@ -320,7 +320,7 @@ export default function SignIn({ authProviders, signUpDisabled }: PageProps) {
}
// current email domain
const domain = email.data.split("@")[1]?.toLowerCase();
- const res = await fetch("/api/auth/check-sso", {
+ const res = await fetch(`${env.NEXT_PUBLIC_BASE_PATH}/api/auth/check-sso`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ domain }),
diff --git a/web/src/pages/auth/sign-up.tsx b/web/src/pages/auth/sign-up.tsx
index 2dd36bdec2..249a499923 100644
--- a/web/src/pages/auth/sign-up.tsx
+++ b/web/src/pages/auth/sign-up.tsx
@@ -48,7 +48,7 @@ export default function SignIn({ authProviders }: PageProps) {
async function onSubmit(values: z.infer<typeof signupSchema>) {
try {
setFormError(null);
- const res = await fetch("/api/auth/signup", {
+ const res = await fetch(`${env.NEXT_PUBLIC_BASE_PATH}/api/auth/signup`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(values),
diff --git a/web/src/server/auth.ts b/web/src/server/auth.ts
index 96c3b204d9..95dad875f0 100644
--- a/web/src/server/auth.ts
+++ b/web/src/server/auth.ts
@@ -477,11 +477,11 @@ export async function getAuthOptions(): Promise<NextAuthOptions> {
adapter: extendedPrismaAdapter,
providers,
pages: {
- signIn: "/auth/sign-in",
- error: "/auth/error",
+ signIn: `${env.NEXT_PUBLIC_BASE_PATH}/auth/sign-in`,
+ error: `${env.NEXT_PUBLIC_BASE_PATH}/auth/error`,
...(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION
? {
- newUser: "/onboarding",
+ newUser: `${env.NEXT_PUBLIC_BASE_PATH}/onboarding`,
}
: {}),
},
diff --git a/web/src/utils/api.ts b/web/src/utils/api.ts
index f7dfd3d5d8..6563b5bc00 100644
--- a/web/src/utils/api.ts
+++ b/web/src/utils/api.ts
@@ -23,13 +23,19 @@ import { setUpSuperjson } from "@/src/utils/superjson";
import { trpcErrorToast } from "@/src/utils/trpcErrorToast";
import { showVersionUpdateToast } from "@/src/features/notifications/showVersionUpdateToast";
import { captureException } from "@sentry/nextjs";
+import { env } from "@/src/env.mjs";
setUpSuperjson();
const getBaseUrl = () => {
- if (typeof window !== "undefined") return ""; // browser should use relative url
- if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}`; // SSR should use vercel url
- return `http://localhost:${process.env.PORT ?? 3000}`; // dev SSR should use localhost
+ const hostname =
+ typeof window !== "undefined"
+ ? window.location.origin
+ : process.env.VERCEL_URL
+ ? `https://${process.env.VERCEL_URL}`
+ : `http://localhost:${process.env.PORT ?? 3000}`;
+
+ return `${hostname}${env.NEXT_PUBLIC_BASE_PATH}`;
};
// global build id used to compare versions to show refresh toast on stale cache hit serving deprecated files