fix: ensure rewrites are merged when config is a function (withPayload)
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 9s
Build & Deploy / 🏗️ Build (push) Successful in 14m52s
Build & Deploy / 🚀 Deploy (push) Successful in 31s
Build & Deploy / 🩺 Smoke Test (push) Failing after 3s
Build & Deploy / 🔔 Notify (push) Successful in 2s

This commit is contained in:
2026-04-12 23:20:35 +02:00
parent 0a9cb81841
commit 12848a4f23

View File

@@ -51,56 +51,52 @@ const nextConfig = {
const withMDX = createMDX({});
// Apply wrappers
const wrappedConfig = withPayload(withMintelConfig(withMDX(nextConfig)));
// Execute wrappers
let config = 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?.() || [];
/**
* Injects our critical showcase rewrites into a resolved Next.js config object.
*/
function injectShowcaseRewrites(resolvedConfig) {
const originalRewrites = resolvedConfig.rewrites;
// Normalize existing to object pattern (Next.js supports both Array and Object)
const normalized = Array.isArray(existing)
? { afterFiles: existing, beforeFiles: [], fallback: [] }
: existing;
resolvedConfig.rewrites = async () => {
const existing = await originalRewrites?.() || [];
// Normalize existing to object pattern
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*',
},
],
return {
...normalized,
beforeFiles: [
...(normalized.beforeFiles || []),
// Diagnostic rule
{ source: '/robots-test', destination: '/robots.txt' },
// Showcase Assets
{ 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*' },
{ 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*' },
],
};
};
};
return resolvedConfig;
}
export default wrappedConfig;
// Handle both standard objects and functional wrappers (like withPayload)
if (typeof config === 'function') {
const originalConfigWrapper = config;
config = async (phase, args) => {
const resolved = await originalConfigWrapper(phase, args);
return injectShowcaseRewrites(resolved);
};
} else {
config = injectShowcaseRewrites(config);
}
export default config;