Unverified Commit 7a4884e6 authored by David Alexandru Ilie's avatar David Alexandru Ilie Committed by GitHub

Fail on Cache Components config mismatches

parent c0dfeffa
...@@ -9,6 +9,7 @@ import '../require-hook' ...@@ -9,6 +9,7 @@ import '../require-hook'
import url from 'url' import url from 'url'
import path from 'path' import path from 'path'
import { readFileSync } from 'fs'
import loadConfig, { type ConfiguredExperimentalFeature } from '../config' import loadConfig, { type ConfiguredExperimentalFeature } from '../config'
import { finalizeBundlerFromConfig, getBundlerFromEnv } from '../../lib/bundler' import { finalizeBundlerFromConfig, getBundlerFromEnv } from '../../lib/bundler'
import { serveStatic } from '../serve-static' import { serveStatic } from '../serve-static'
...@@ -38,6 +39,7 @@ import { ...@@ -38,6 +39,7 @@ import {
PHASE_PRODUCTION_SERVER, PHASE_PRODUCTION_SERVER,
PHASE_DEVELOPMENT_SERVER, PHASE_DEVELOPMENT_SERVER,
REQUEST_INSIGHTS_DEV_ENDPOINT, REQUEST_INSIGHTS_DEV_ENDPOINT,
SERVER_FILES_MANIFEST,
UNDERSCORE_NOT_FOUND_ROUTE, UNDERSCORE_NOT_FOUND_ROUTE,
} from '../../shared/lib/constants' } from '../../shared/lib/constants'
import { RedirectStatusCode } from '../../client/components/redirect-status-code' import { RedirectStatusCode } from '../../client/components/redirect-status-code'
...@@ -113,6 +115,33 @@ function getErrorMessage(error: unknown): string { ...@@ -113,6 +115,33 @@ function getErrorMessage(error: unknown): string {
return deobfuscateText(String(error)) return deobfuscateText(String(error))
} }
function validateCacheComponentsConfig(
dir: string,
config: Pick<NextConfigComplete, 'cacheComponents' | 'distDir'>
): void {
let buildConfig: { cacheComponents?: boolean }
try {
buildConfig = JSON.parse(
readFileSync(
path.join(dir, config.distDir, SERVER_FILES_MANIFEST + '.json'),
'utf8'
)
).config
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
return
}
throw error
}
if (!!buildConfig.cacheComponents !== config.cacheComponents) {
throw new Error(
`The production build was created with \`cacheComponents: ${!!buildConfig.cacheComponents}\`, but the runtime configuration has \`cacheComponents: ${config.cacheComponents}\`. \`next start\` must use the same Cache Components setting as \`next build\`. Ensure the same Next.js configuration is available at build time and runtime, or rebuild the application after changing it.\nLearn more: https://nextjs.org/docs/app/guides/self-hosting`
)
}
}
export type RenderServer = Pick< export type RenderServer = Pick<
typeof import('./render-server'), typeof import('./render-server'),
| 'initialize' | 'initialize'
...@@ -163,6 +192,9 @@ export async function initialize(opts: { ...@@ -163,6 +192,9 @@ export async function initialize(opts: {
}, },
} }
) )
if (!opts.dev) {
validateCacheComponentsConfig(opts.dir, config)
}
if (bundlerBeforeConfig !== undefined) { if (bundlerBeforeConfig !== undefined) {
finalizeBundlerFromConfig(bundlerBeforeConfig) finalizeBundlerFromConfig(bundlerBeforeConfig)
} }
......
export default function RootLayout({
children,
}: Readonly<{ children: React.ReactNode }>) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>hello</p>
}
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
jest.setTimeout(120_000)
describe('Cache Components config parity', () => {
const { next } = nextTestSetup({
files: __dirname,
skipDeployment: true,
skipStart: true,
})
it('rejects a production server when its build config is omitted', async () => {
await next.build()
await next.deleteFile('next.config.js')
await next.start({ skipBuild: true })
await retry(async () => {
expect(next.cliOutput).toContain(
'The production build was created with `cacheComponents: true`, but the runtime configuration has `cacheComponents: false`.'
)
await expect(next.fetch('/')).rejects.toThrow()
})
})
})
module.exports = {
cacheComponents: true,
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment