🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(web): allow serving build output from a dedicated asset host (#16546)
Steffen Schmitzdd18496d6745
diff --git a/.env.prod.example b/.env.prod.exampleindex b2da902036..7a8d181aa0 100644--- a/.env.prod.example+++ b/.env.prod.example@@ -32,6 +32,16 @@ ENCRYPTION_KEY="0000000000000000000000000000000000000000000000000000000000000000# 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"+# Serve this build's /_next/static/* output from a dedicated hostname, e.g. a+# CDN, so browser tabs that outlive a deploy can still resolve their chunks.+# Must be an origin with no path. The app keeps serving the same files itself.+# Bundled web workers stay on the app origin (next.config experimental+# turbopackWorkerAssetPrefix) because browsers reject cross-origin classic+# workers.+# Note: build-time only for the same reason as the base path above, and the+# hostname must send Access-Control-Allow-Origin for this deployment's origin.+# NEXT_PUBLIC_ASSET_PREFIX="https://static.example.com"+# Character count above which trace/observation I/O is rendered as plain text instead of markdown, optional# Works at runtime with the pre-built docker image.# LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT=150000diff --git a/.github/workflows/_deploy_ecs_service.yml b/.github/workflows/_deploy_ecs_service.ymlindex f386aa459b..c995a50d72 100644--- a/.github/workflows/_deploy_ecs_service.yml+++ b/.github/workflows/_deploy_ecs_service.yml@@ -65,6 +65,7 @@ jobs:VARS_SENTRY_ORG: ${{ vars.SENTRY_ORG }}VARS_SENTRY_PROJECT: ${{ vars.SENTRY_PROJECT }}VARS_NEXT_PUBLIC_LANGFUSE_TRACING_SAMPLE_RATE: ${{ vars.NEXT_PUBLIC_LANGFUSE_TRACING_SAMPLE_RATE }}+ VARS_NEXT_PUBLIC_ASSET_PREFIX: ${{ vars.NEXT_PUBLIC_ASSET_PREFIX }}SECRETS_SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}run: |docker build \@@ -84,6 +85,7 @@ jobs:--build-arg SENTRY_ORG=${VARS_SENTRY_ORG} \--build-arg SENTRY_PROJECT=${VARS_SENTRY_PROJECT} \--build-arg NEXT_PUBLIC_LANGFUSE_TRACING_SAMPLE_RATE=${VARS_NEXT_PUBLIC_LANGFUSE_TRACING_SAMPLE_RATE} \+ --build-arg NEXT_PUBLIC_ASSET_PREFIX=${VARS_NEXT_PUBLIC_ASSET_PREFIX} \.docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG- name: Render AWS ECS Task Definitiondiff --git a/web/Dockerfile b/web/Dockerfileindex cc0b12d628..cdff85da58 100644--- a/web/Dockerfile+++ b/web/Dockerfile@@ -98,6 +98,11 @@ ARG NEXT_PUBLIC_SENTRY_DSNENV NEXT_PUBLIC_SENTRY_DSN=$NEXT_PUBLIC_SENTRY_DSNARG NEXT_PUBLIC_BASE_PATHENV NEXT_PUBLIC_BASE_PATH=$NEXT_PUBLIC_BASE_PATH+# Origin serving this build's /_next/static/* output. Must be a build arg: the+# bundler writes it into the client runtime's public path, so it cannot be+# changed by a runtime environment variable.+ARG NEXT_PUBLIC_ASSET_PREFIX+ENV NEXT_PUBLIC_ASSET_PREFIX=$NEXT_PUBLIC_ASSET_PREFIX# Sentry already needs to be set on build time to upload sourcemaps# This must not be set for OSS releases as we would share our Sentry secret.diff --git a/web/next.config.mjs b/web/next.config.mjsindex 0785fa6f1e..92becb7f98 100644--- a/web/next.config.mjs+++ b/web/next.config.mjs@@ -31,19 +31,30 @@ const localStorageConnectSrc =env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION === undefined? "http://localhost:* ": "";+// When build output is served from a dedicated asset hostname, that origin has+// to be allowed everywhere Next.js can emit an asset URL. A host under+// langfuse.com would already be covered by the wildcards below, but the policy+// must not silently depend on where the asset host happens to live.+// Deliberately NOT added to img-src / media-src, which already allow `https:`,+// nor to worker-src: experimental.turbopackWorkerAssetPrefix keeps Worker+// entrypoints and their module chunks on the app origin, so 'self' blob:+// remains the right policy even when other /_next/static assets are cross-origin.+const assetPrefixSrc = env.NEXT_PUBLIC_ASSET_PREFIX+ ? `${new URL(env.NEXT_PUBLIC_ASSET_PREFIX).origin} `+ : "";const cspHeader = `- default-src 'self' https://*.langfuse.com https://*.langfuse.dev https://*.posthog.com https://*.sentry.io;- script-src 'self' 'unsafe-eval' 'unsafe-inline' https://*.langfuse.com https://*.langfuse.dev https://challenges.cloudflare.com https://*.sentry.io https://static.cloudflareinsights.com https://*.stripe.com https://login.microsoftonline.com https://login.microsoft.com https://*.microsoftonline.com;- style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://login.microsoftonline.com https://login.microsoft.com https://*.microsoftonline.com;+ default-src 'self' ${assetPrefixSrc}https://*.langfuse.com https://*.langfuse.dev https://*.posthog.com https://*.sentry.io;+ script-src 'self' 'unsafe-eval' 'unsafe-inline' ${assetPrefixSrc}https://*.langfuse.com https://*.langfuse.dev https://challenges.cloudflare.com https://*.sentry.io https://static.cloudflareinsights.com https://*.stripe.com https://login.microsoftonline.com https://login.microsoft.com https://*.microsoftonline.com;+ style-src 'self' 'unsafe-inline' ${assetPrefixSrc}https://fonts.googleapis.com https://login.microsoftonline.com https://login.microsoft.com https://*.microsoftonline.com;img-src 'self' https: blob: data: http://localhost:* https://prod-uk-services-workspac-workspacefilespublicbuck-vs4gjqpqjkh6.s3.amazonaws.com https://prod-uk-services-attachm-attachmentsbucket28b3ccf-uwfssb4vt2us.s3.eu-west-2.amazonaws.com https://i0.wp.com;- font-src 'self';+ font-src ${assetPrefixSrc}'self';frame-src 'self' https://challenges.cloudflare.com https://*.stripe.com https://login.microsoftonline.com https://login.microsoft.com https://*.microsoftonline.com;worker-src 'self' blob:;object-src 'none';base-uri 'self';form-action 'self' https://login.microsoftonline.com https://login.microsoft.com https://*.microsoftonline.com;frame-ancestors 'none';- connect-src 'self' ${localStorageConnectSrc}${mediaUploadConnectSrc}https://*.langfuse.com https://*.langfuse.dev https://*.ingest.us.sentry.io https://*.sentry.io https://chat.uk.plain.com https://*.amazonaws.com https://*.blob.core.windows.net https://storage.googleapis.com https://prod-uk-services-attachm-attachmentsuploadbucket2-1l2e4906o2asm.s3.eu-west-2.amazonaws.com https://login.microsoftonline.com https://login.microsoft.com https://*.microsoftonline.com https://graph.microsoft.com;+ connect-src 'self' ${localStorageConnectSrc}${mediaUploadConnectSrc}${assetPrefixSrc}https://*.langfuse.com https://*.langfuse.dev https://*.ingest.us.sentry.io https://*.sentry.io https://chat.uk.plain.com https://*.amazonaws.com https://*.blob.core.windows.net https://storage.googleapis.com https://prod-uk-services-attachm-attachmentsuploadbucket2-1l2e4906o2asm.s3.eu-west-2.amazonaws.com https://login.microsoftonline.com https://login.microsoft.com https://*.microsoftonline.com https://graph.microsoft.com;media-src 'self' https: http://localhost:*;${env.LANGFUSE_CSP_ENFORCE_HTTPS === "true" ? "upgrade-insecure-requests; block-all-mixed-content;" : ""}${env.SENTRY_CSP_REPORT_URI ? `report-uri ${env.SENTRY_CSP_REPORT_URI}; report-to csp-endpoint;` : ""}@@ -99,6 +110,26 @@ const nextConfig = {],poweredByHeader: false,basePath: env.NEXT_PUBLIC_BASE_PATH,+ // Hand the browser a dedicated hostname for this build's `/_next/static/*`+ // output so a CDN in front of it can keep serving the chunks of a build that+ // has already been replaced — a tab that outlives a deploy otherwise 404s on+ // its own chunks. The app origin keeps serving the same files either way;+ // this only changes the URLs that get emitted, which is why unsetting the+ // variable and rebuilding is a complete rollback.+ //+ // Baked in at build time, not read at runtime: the bundler writes it into the+ // client runtime's public path, and the standalone server reads the config+ // frozen into .next/required-server-files.json rather than this file. The+ // deploy workflow already builds one image per environment, so this is set+ // per environment there.+ assetPrefix: env.NEXT_PUBLIC_ASSET_PREFIX,+ // Only meaningful alongside an asset prefix, and load-bearing there: without+ // `crossorigin`, an exception thrown by a cross-origin script reaches+ // window.onerror as a bare "Script error" with no stack, which would blind+ // Sentry to exactly the failures this setup exists to observe. Requires the+ // asset host to send Access-Control-Allow-Origin, so scripts fail closed+ // rather than silently losing their stacks.+ crossOrigin: env.NEXT_PUBLIC_ASSET_PREFIX ? "anonymous" : undefined,compiler: {define: {"import.meta.vitest": "undefined",@@ -121,6 +152,14 @@ const nextConfig = {experimental: {// Use the Rust port instead of the Babel transform// turbopackRustReactCompiler: true,+ // Keep `new Worker(new URL(..., import.meta.url))` on the app origin when+ // assetPrefix points at a CDN. Browsers reject a cross-origin classic+ // worker (Turbopack always constructs one), and the worker bootstrap also+ // refuses foreign-origin module chunks, so both the entrypoint and its+ // imports have to stay same-origin. Empty string is a literal prefix, not+ // a fallback: it emits `/_next/...` on the page origin. Unset (undefined)+ // would inherit assetPrefix and break workers on Cloud.+ turbopackWorkerAssetPrefix: "",},/**diff --git a/web/src/env.mjs b/web/src/env.mjsindex 4c0ddf6642..99cef94f47 100644--- a/web/src/env.mjs+++ b/web/src/env.mjs@@ -626,6 +626,17 @@ export const env = createEnv({NEXT_PUBLIC_PLAIN_APP_ID: z.string().optional(),NEXT_PUBLIC_BUILD_ID: z.string().optional(),NEXT_PUBLIC_BASE_PATH: z.string().optional(),+ // Origin that serves this build's `/_next/static/*` output, when it is+ // fronted by a CDN on its own hostname. The app origin keeps serving the+ // same files, so this only changes the URLs the browser is handed.+ // Restricted to a bare origin: a path component would have to be mirrored+ // by whatever fronts the hostname, and nothing here checks that it is.+ NEXT_PUBLIC_ASSET_PREFIX: z+ .url()+ .refine((value) => new URL(value).pathname === "/", {+ message: "NEXT_PUBLIC_ASSET_PREFIX must be an origin with no path",+ })+ .optional(),NEXT_PUBLIC_LANGFUSE_PLAYGROUND_STREAMING_ENABLED_DEFAULT: z.enum(["true", "false"]).optional()@@ -995,6 +1006,7 @@ export const env = createEnv({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,+ NEXT_PUBLIC_ASSET_PREFIX: process.env.NEXT_PUBLIC_ASSET_PREFIX,LANGFUSE_MAX_HISTORIC_EVAL_CREATION_LIMIT:process.env.LANGFUSE_MAX_HISTORIC_EVAL_CREATION_LIMIT,SLACK_CLIENT_ID: process.env.SLACK_CLIENT_ID,