Fix: Correct relative asset routing for case studies in middleware
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 10s
Build & Deploy / 🏗️ Build (push) Successful in 6m37s
Build & Deploy / 🚀 Deploy (push) Successful in 32s
Build & Deploy / 🩺 Smoke Test (push) Failing after 4s
Build & Deploy / 🔔 Notify (push) Successful in 3s

This commit is contained in:
2026-05-06 10:21:03 +02:00
parent ce7d485b0c
commit d5f36274aa

View File

@@ -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));
}