From 0a9cb81841f170d81e468f30b2d42f6ddff84197 Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Sun, 12 Apr 2026 22:38:46 +0200 Subject: [PATCH] fix: harden next.config rewrites against wrapper overwrites --- apps/web/next.config.mjs | 98 +++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 42 deletions(-) diff --git a/apps/web/next.config.mjs b/apps/web/next.config.mjs index 723e2b0..ff7c2ea 100644 --- a/apps/web/next.config.mjs +++ b/apps/web/next.config.mjs @@ -14,7 +14,7 @@ const nextConfig = { 'sharp', 'puppeteer', 'require-in-the-middle', - 'import-in-the-middle' // Sentry 10+ instrumentation dependencies + 'import-in-the-middle' ], transpilePackages: [ '@mintel/content-engine', @@ -37,43 +37,6 @@ const nextConfig = { }, ], }, - async rewrites() { - return { - beforeFiles: [ - // Assets requested from root - { - source: '/assets/:path*', - destination: '/showcase/klz-cables.com/assets/:path*', - }, - { - source: '/wp-content/:path*', - destination: '/showcase/klz-cables.com/assets/klz-cables.com/wp-content/:path*', - }, - { - source: '/wp-includes/:path*', - destination: '/showcase/klz-cables.com/assets/klz-cables.com/wp-includes/:path*', - }, - // Assets requested relatively from case-studies/work/blog subpages - { - source: '/:folder(case-studies|work|blog)/assets/:path*', - destination: '/showcase/klz-cables.com/assets/:path*', - }, - { - source: '/:folder(case-studies|work|blog)/wp-content/:path*', - destination: '/showcase/klz-cables.com/assets/klz-cables.com/wp-content/:path*', - }, - { - source: '/:folder(case-studies|work|blog)/wp-includes/:path*', - destination: '/showcase/klz-cables.com/assets/klz-cables.com/wp-includes/:path*', - }, - // Diagnostic rule - { - source: '/robots-test', - destination: '/robots.txt', - }, - ], - }; - }, async redirects() { return [ { @@ -86,7 +49,58 @@ const nextConfig = { outputFileTracingRoot: path.join(dirname, '../../'), }; -const withMDX = createMDX({ - // Add markdown plugins here, as desired -}); -export default withPayload(withMintelConfig(withMDX(nextConfig))); +const withMDX = createMDX({}); + +// Apply wrappers +const wrappedConfig = withPayload(withMintelConfig(withMDX(nextConfig))); + +// HARDEN REWRITES: Manually merge showcase rules to prevent wrapper overwrites +const originalRewrites = wrappedConfig.rewrites; +wrappedConfig.rewrites = async () => { + const existing = await originalRewrites?.() || []; + + // Normalize existing to object pattern (Next.js supports both Array and Object) + const normalized = Array.isArray(existing) + ? { afterFiles: existing, beforeFiles: [], fallback: [] } + : existing; + + return { + ...normalized, + beforeFiles: [ + ...(normalized.beforeFiles || []), + // Diagnostic rule + { + source: '/robots-test', + destination: '/robots.txt', + }, + // Assets requested from root + { + source: '/assets/:path*', + destination: '/showcase/klz-cables.com/assets/:path*', + }, + { + source: '/wp-content/:path*', + destination: '/showcase/klz-cables.com/assets/klz-cables.com/wp-content/:path*', + }, + { + source: '/wp-includes/:path*', + destination: '/showcase/klz-cables.com/assets/klz-cables.com/wp-includes/:path*', + }, + // Assets requested relatively from case-studies/work/blog subpages + { + source: '/:folder(case-studies|work|blog)/assets/:path*', + destination: '/showcase/klz-cables.com/assets/:path*', + }, + { + source: '/:folder(case-studies|work|blog)/wp-content/:path*', + destination: '/showcase/klz-cables.com/assets/klz-cables.com/wp-content/:path*', + }, + { + source: '/:folder(case-studies|work|blog)/wp-includes/:path*', + destination: '/showcase/klz-cables.com/assets/klz-cables.com/wp-includes/:path*', + }, + ], + }; +}; + +export default wrappedConfig;