From c70288bba7c2b2bffef4ef02bb1c8d995bbcf0e1 Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Fri, 6 Feb 2026 11:29:10 +0100 Subject: [PATCH] feat: Enhance Directus URL resolution for internal and proxy paths, and adjust Traefik host variable interpolation. --- .gitea/workflows/deploy.yml | 12 ++++++------ docker-compose.yml | 8 ++++---- lib/directus.ts | 8 +++++++- middleware.ts | 6 +++--- next.config.mjs | 2 +- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 7334ff00..1d95fd1d 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -89,10 +89,10 @@ jobs: TARGET="testing" IMAGE_TAG="main-${SHORT_SHA}" ENV_FILE=".env.testing" - TRAEFIK_HOST='`testing.klz-cables.com`' + TRAEFIK_HOST="testing.klz-cables.com" NEXT_PUBLIC_BASE_URL="https://testing.klz-cables.com" DIRECTUS_URL="https://cms.testing.klz-cables.com" - DIRECTUS_HOST='`cms.testing.klz-cables.com`' + DIRECTUS_HOST="cms.testing.klz-cables.com" PROJECT_NAME="klz-cables-testing" IS_PROD="false" GOTIFY_TITLE="๐Ÿงช Testing-Deploy" @@ -103,10 +103,10 @@ jobs: TARGET="production" IMAGE_TAG="$TAG" ENV_FILE=".env.prod" - TRAEFIK_HOST='`klz-cables.com`, `www.klz-cables.com`' + TRAEFIK_HOST="klz-cables.com, www.klz-cables.com" NEXT_PUBLIC_BASE_URL="https://klz-cables.com" DIRECTUS_URL="https://cms.klz-cables.com" - DIRECTUS_HOST='`cms.klz-cables.com`' + DIRECTUS_HOST="cms.klz-cables.com" PROJECT_NAME="klz-cables-prod" IS_PROD="true" GOTIFY_TITLE="๐Ÿš€ Production-Release" @@ -115,10 +115,10 @@ jobs: TARGET="staging" IMAGE_TAG="$TAG" ENV_FILE=".env.staging" - TRAEFIK_HOST='`staging.klz-cables.com`' + TRAEFIK_HOST="staging.klz-cables.com" NEXT_PUBLIC_BASE_URL="https://staging.klz-cables.com" DIRECTUS_URL="https://cms.staging.klz-cables.com" - DIRECTUS_HOST='`cms.staging.klz-cables.com`' + DIRECTUS_HOST="cms.staging.klz-cables.com" PROJECT_NAME="klz-cables-staging" IS_PROD="false" GOTIFY_TITLE="๐Ÿงช Staging-Deploy (Pre-Release)" diff --git a/docker-compose.yml b/docker-compose.yml index d4c69c32..52fb5c00 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,11 +25,11 @@ services: labels: - "traefik.enable=true" # HTTP โ‡’ HTTPS redirect - - "traefik.http.routers.${PROJECT_NAME:-klz-cables}-web.rule=Host(${TRAEFIK_HOST}) && !PathPrefix(`/.well-known/acme-challenge/`)" + - "traefik.http.routers.${PROJECT_NAME:-klz-cables}-web.rule=Host(`${TRAEFIK_HOST}`) && !PathPrefix(`/.well-known/acme-challenge/`)" - "traefik.http.routers.${PROJECT_NAME:-klz-cables}-web.entrypoints=web" - "traefik.http.routers.${PROJECT_NAME:-klz-cables}-web.middlewares=redirect-https" # HTTPS router - - "traefik.http.routers.${PROJECT_NAME:-klz-cables}.rule=Host(${TRAEFIK_HOST})" + - "traefik.http.routers.${PROJECT_NAME:-klz-cables}.rule=Host(`${TRAEFIK_HOST}`)" - "traefik.http.routers.${PROJECT_NAME:-klz-cables}.entrypoints=websecure" - "traefik.http.routers.${PROJECT_NAME:-klz-cables}.tls.certresolver=le" - "traefik.http.routers.${PROJECT_NAME:-klz-cables}.tls=true" @@ -43,7 +43,7 @@ services: - "traefik.http.routers.${PROJECT_NAME:-klz-cables}.middlewares=${PROJECT_NAME:-klz-cables}-ratelimit,${PROJECT_NAME:-klz-cables}-forward,${AUTH_MIDDLEWARE:-compress}" # Gatekeeper Router (to show the login page) - - "traefik.http.routers.${PROJECT_NAME:-klz-cables}-gatekeeper.rule=Host(${TRAEFIK_HOST}) && PathPrefix(`/gatekeeper`)" + - "traefik.http.routers.${PROJECT_NAME:-klz-cables}-gatekeeper.rule=Host(`${TRAEFIK_HOST}`) && PathPrefix(`/gatekeeper`)" - "traefik.http.routers.${PROJECT_NAME:-klz-cables}-gatekeeper.entrypoints=websecure" - "traefik.http.routers.${PROJECT_NAME:-klz-cables}-gatekeeper.tls.certresolver=le" - "traefik.http.routers.${PROJECT_NAME:-klz-cables}-gatekeeper.tls=true" @@ -102,7 +102,7 @@ services: - ./directus/extensions:/directus/extensions labels: - "traefik.enable=true" - - "traefik.http.routers.${PROJECT_NAME:-klz-cables}-directus.rule=Host(${DIRECTUS_HOST})" + - "traefik.http.routers.${PROJECT_NAME:-klz-cables}-directus.rule=Host(`${DIRECTUS_HOST}`)" - "traefik.http.routers.${PROJECT_NAME:-klz-cables}-directus.entrypoints=websecure" - "traefik.http.routers.${PROJECT_NAME:-klz-cables}-directus.tls.certresolver=le" - "traefik.http.routers.${PROJECT_NAME:-klz-cables}-directus.tls=true" diff --git a/lib/directus.ts b/lib/directus.ts index 5f8b4704..ea7d9bbb 100644 --- a/lib/directus.ts +++ b/lib/directus.ts @@ -5,7 +5,13 @@ import { getServerAppServices } from './services/create-services.server'; const { url, adminEmail, password, token, proxyPath, internalUrl } = config.directus; // Use internal URL if on server to bypass Gatekeeper/Auth -const effectiveUrl = typeof window === 'undefined' && internalUrl ? internalUrl : url; +// Use proxy path in browser to stay on the same origin +const effectiveUrl = + typeof window === 'undefined' + ? internalUrl || url + : typeof window !== 'undefined' + ? `${window.location.origin}${proxyPath}` + : proxyPath; const client = createDirectus(effectiveUrl).with(rest()).with(authentication()); diff --git a/middleware.ts b/middleware.ts index fe3ad65e..b7f6450c 100644 --- a/middleware.ts +++ b/middleware.ts @@ -8,7 +8,7 @@ const intlMiddleware = createMiddleware({ locales: ['en', 'de'], // Used when no locale matches - defaultLocale: 'en' + defaultLocale: 'en', }); // Main middleware that logs all requests @@ -20,7 +20,7 @@ export default function middleware(request: NextRequest) { const ip = headers.get('x-forwarded-for') || headers.get('x-real-ip') || 'unknown'; // Log incoming request - console.log(`Incoming request: method=${method} url=${url}`); + console.log(`Incoming request: method=${method} host=${headers.get('host')} url=${url}`); try { // Apply internationalization middleware @@ -34,5 +34,5 @@ export default function middleware(request: NextRequest) { export const config = { // Match only internationalized pathnames - matcher: ['/((?!api|_next|_vercel|health|.*\\..*).*)', '/', '/(de|en)/:path*'] + matcher: ['/((?!api|_next|_vercel|health|.*\\..*).*)', '/', '/(de|en)/:path*'], }; diff --git a/next.config.mjs b/next.config.mjs index 9ed1b8da..ad31d2e5 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -327,7 +327,7 @@ const nextConfig = { ? new URL(process.env.SENTRY_DSN).origin : 'https://errors.infra.mintel.me'; - const directusUrl = process.env.DIRECTUS_URL || 'https://cms.klz-cables.com'; + const directusUrl = process.env.INTERNAL_DIRECTUS_URL || process.env.DIRECTUS_URL || 'https://cms.klz-cables.com'; return [ {