From d5f36274aa3b12202c54bab578ecebfb9b7e5d6a Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Wed, 6 May 2026 10:21:03 +0200 Subject: [PATCH] Fix: Correct relative asset routing for case studies in middleware --- apps/web/src/middleware.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/web/src/middleware.ts b/apps/web/src/middleware.ts index a47d19a..ed87ba4 100644 --- a/apps/web/src/middleware.ts +++ b/apps/web/src/middleware.ts @@ -21,7 +21,7 @@ export function middleware(request: NextRequest) { // Map Pattern 2: Showcase-prefixed assets (used for relative links within the showcase) const showcaseMatch = path.match(/^\/(?:case-studies|work|blog)\/([^\/]+)\/(.*)/); if (showcaseMatch) { - const [, slug, remainingPath] = showcaseMatch; + let [, slug, remainingPath] = showcaseMatch; // Safety: Only rewrite if it looks like a static asset (has extension) // or belongs to known legacy folders. This prevents catching Next.js routes. @@ -30,13 +30,23 @@ export function middleware(request: NextRequest) { remainingPath.includes('wp-includes/') || remainingPath.includes('assets/')) { + // If the URL resolved to /case-studies/assets/..., 'assets' is captured as the slug + if (slug === 'assets') { + const nextSlash = remainingPath.indexOf('/'); + if (nextSlash !== -1) { + const actualDir = remainingPath.substring(0, nextSlash); + slug = actualDir === 'klz-cables.com' ? 'klz-cables' : actualDir; // Normalize back to expected slug + remainingPath = remainingPath.substring(nextSlash + 1); + } + } + // Normalize slug: klz-cables -> klz-cables.com (literal folder name) const directory = slug === 'klz-cables' ? 'klz-cables.com' : slug; // If the remaining path already starts with assets/directory, don't double it const targetPath = remainingPath.startsWith(`assets/${directory}/`) ? `/${remainingPath}` - : `/${remainingPath}`; + : `/assets/${directory}/${remainingPath}`; return NextResponse.rewrite(new URL(`/showcase/${directory}${targetPath}`, request.url)); }