Refactor for i18n, CMS integration, and project slugs; enhance admin & analytics

Co-authored-by: dennis <dennis@konkol.net>
This commit is contained in:
Cursor Agent
2026-01-12 14:36:10 +00:00
parent 0349c686fa
commit 12245eec8e
55 changed files with 4573 additions and 753 deletions

View File

@@ -1,21 +1,82 @@
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
const SUPPORTED_LOCALES = ["en", "de"] as const;
type SupportedLocale = (typeof SUPPORTED_LOCALES)[number];
function pickLocaleFromHeader(acceptLanguage: string | null): SupportedLocale {
if (!acceptLanguage) return "en";
const lower = acceptLanguage.toLowerCase();
// Very small parser: prefer de, then en
if (lower.includes("de")) return "de";
if (lower.includes("en")) return "en";
return "en";
}
function hasLocalePrefix(pathname: string): boolean {
return SUPPORTED_LOCALES.some((l) => pathname === `/${l}` || pathname.startsWith(`/${l}/`));
}
export function middleware(request: NextRequest) {
// For /manage and /editor routes, the pages handle their own authentication
// No middleware redirect needed - let the pages show login forms
const { pathname, search } = request.nextUrl;
// Keep admin + APIs unlocalized for simplicity
const isAdminOrApi =
pathname.startsWith("/api/") ||
pathname === "/api" ||
pathname.startsWith("/manage") ||
pathname.startsWith("/editor");
// Locale routing for public site pages
const responseUrl = request.nextUrl.clone();
if (!isAdminOrApi) {
if (hasLocalePrefix(pathname)) {
// Persist locale preference
const locale = pathname.split("/")[1] as SupportedLocale;
const res = NextResponse.next();
res.cookies.set("NEXT_LOCALE", locale, { path: "/" });
// Continue below to add security headers
// eslint-disable-next-line no-use-before-define
return addHeaders(request, res);
}
// Redirect bare routes to locale-prefixed ones
const preferred = pickLocaleFromHeader(request.headers.get("accept-language"));
const redirectTarget =
pathname === "/" ? `/${preferred}` : `/${preferred}${pathname}${search || ""}`;
responseUrl.pathname = redirectTarget;
const res = NextResponse.redirect(responseUrl);
res.cookies.set("NEXT_LOCALE", preferred, { path: "/" });
// eslint-disable-next-line no-use-before-define
return addHeaders(request, res);
}
// Fix for 421 Misdirected Request with Nginx Proxy Manager
// Ensure proper host header handling for reverse proxy
const hostname = request.headers.get('host') || request.headers.get('x-forwarded-host') || '';
const hostname = request.headers.get("host") || request.headers.get("x-forwarded-host") || "";
// Add security headers to all responses
const response = NextResponse.next();
return addHeaders(request, response, hostname);
}
function addHeaders(request: NextRequest, response: NextResponse, hostnameOverride?: string) {
const hostname =
hostnameOverride ??
request.headers.get("host") ??
request.headers.get("x-forwarded-host") ??
"";
// Set proper headers for Nginx Proxy Manager
if (hostname) {
response.headers.set('X-Forwarded-Host', hostname);
response.headers.set('X-Real-IP', request.headers.get('x-real-ip') || request.headers.get('x-forwarded-for') || '');
response.headers.set("X-Forwarded-Host", hostname);
response.headers.set(
"X-Real-IP",
request.headers.get("x-real-ip") || request.headers.get("x-forwarded-for") || "",
);
}
// Security headers (complementing next.config.ts headers)
@@ -42,13 +103,11 @@ export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api/email (email API routes)
* - api/health (health check)
* - api (all API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - api/auth (auth API routes - need to be processed)
*/
"/((?!api/email|api/health|_next/static|_next/image|favicon.ico|api/auth).*)",
"/((?!api|_next/static|_next/image|favicon.ico).*)",
],
};