fix: ensure rewrites are merged when config is a function (withPayload)
Some checks failed
Some checks failed
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user