fix(showcase): harden middleware rewrite logic to fix 404 assets in smoke tests
Some checks failed
Some checks failed
This commit is contained in:
@@ -15,19 +15,24 @@ export function middleware(request: NextRequest) {
|
||||
// - Global assets (/wp-content/*, /wp-includes/*, /assets/*)
|
||||
// - Showcase-specific resources (/case-studies/klz-cables/index.html, etc.)
|
||||
|
||||
// Normalize pathname to ensure we're matching correctly
|
||||
const path = pathname;
|
||||
|
||||
// Map Pattern 1: Global assets (directly under root)
|
||||
if (pathname.match(/^\/(?:wp-content|wp-includes|assets)\//)) {
|
||||
return NextResponse.rewrite(new URL(`/showcase/klz-cables.com${pathname}`, request.url));
|
||||
// These are often mirrored by wget/discovery engines into a domain-named subfolder
|
||||
if (path.startsWith('/wp-content/') || path.startsWith('/wp-includes/') || path.startsWith('/assets/')) {
|
||||
// Brute force: Re-route to the known KLZ showcase directory
|
||||
return NextResponse.rewrite(new URL(`/showcase/klz-cables.com${path}`, request.url));
|
||||
}
|
||||
|
||||
// Map Pattern 2: Showcase-prefixed assets
|
||||
const showcaseMatch = pathname.match(/^\/(?:case-studies|work|blog)\/([^\/]+)\/(.*)/);
|
||||
// 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;
|
||||
|
||||
// Safety: Only rewrite if it looks like a static asset (has extension)
|
||||
// or belongs to known legacy folders. This prevents catching Next.js routes.
|
||||
if (remainingPath.match(/\.(?:html|php|js|css|png|jpg|jpeg|svg|gif|woff2?|ttf|pdf|json|xml)$/) ||
|
||||
if (remainingPath.match(/\.(?:html|php|js|css|png|jpg|jpeg|svg|gif|webp|woff2?|ttf|pdf|json|xml)$/) ||
|
||||
remainingPath.includes('wp-content/') ||
|
||||
remainingPath.includes('wp-includes/') ||
|
||||
remainingPath.includes('assets/')) {
|
||||
@@ -35,7 +40,12 @@ export function middleware(request: NextRequest) {
|
||||
// Normalize slug: klz-cables -> klz-cables.com (literal folder name)
|
||||
const directory = slug === 'klz-cables' ? 'klz-cables.com' : slug;
|
||||
|
||||
return NextResponse.rewrite(new URL(`/showcase/${directory}/${remainingPath}`, request.url));
|
||||
// If the remaining path already starts with assets/directory, don't double it
|
||||
const targetPath = remainingPath.startsWith(`assets/${directory}/`)
|
||||
? `/${remainingPath}`
|
||||
: `/${remainingPath}`;
|
||||
|
||||
return NextResponse.rewrite(new URL(`/showcase/${directory}${targetPath}`, request.url));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user