Fix: Correct relative asset routing for case studies in middleware
Some checks failed
Some checks failed
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user