Unverified Commit 0f27700f authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

feat: Isolate dev build from prod (#83961)

### Why?

When running a dev server, it currently gets interrupted when running
`next build`.
This is very common when using AI for app development, where it runs
`next build` for whatever reason while the user is working on a dev
server.

### How?

This PR creates an isolated `dev/` directory inside the `distDir` to be
isolated from the build, and will not be cleared out during the `next
build` like the `cache/` directory. Since the cache between the dev and
prod isn't shared at the moment, it makes sense to let them have
separate `cache/` directories.


https://github.com/user-attachments/assets/2005ba70-861c-4763-b47f-cf17c62c0571
parent a355f911
...@@ -1123,7 +1123,7 @@ export default async function build( ...@@ -1123,7 +1123,7 @@ export default async function build(
} }
if (config.cleanDistDir && !isGenerateMode) { if (config.cleanDistDir && !isGenerateMode) {
await recursiveDelete(distDir, /^cache/) await recursiveDelete(distDir, /^(cache|dev)/)
} }
if (appDir && 'exportPathMap' in config) { if (appDir && 'exportPathMap' in config) {
......
...@@ -414,6 +414,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -414,6 +414,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
prerenderEarlyExit: z.boolean().optional(), prerenderEarlyExit: z.boolean().optional(),
proxyTimeout: z.number().gte(0).optional(), proxyTimeout: z.number().gte(0).optional(),
rootParams: z.boolean().optional(), rootParams: z.boolean().optional(),
isolatedDevBuild: z.boolean().optional(),
routerBFCache: z.boolean().optional(), routerBFCache: z.boolean().optional(),
removeUncaughtErrorAndRejectionListeners: z.boolean().optional(), removeUncaughtErrorAndRejectionListeners: z.boolean().optional(),
validateRSCRequestHeaders: z.boolean().optional(), validateRSCRequestHeaders: z.boolean().optional(),
......
...@@ -804,6 +804,13 @@ export interface ExperimentalConfig { ...@@ -804,6 +804,13 @@ export interface ExperimentalConfig {
* Enable accessing root params via the `next/root-params` module. * Enable accessing root params via the `next/root-params` module.
*/ */
rootParams?: boolean rootParams?: boolean
/**
* Use an isolated directory for development builds to prevent conflicts
* with production builds. Development builds will use `{distDir}/dev`
* instead of `{distDir}`.
*/
isolatedDevBuild?: boolean
} }
export type ExportPathMap = { export type ExportPathMap = {
...@@ -1496,6 +1503,7 @@ export const defaultConfig = Object.freeze({ ...@@ -1496,6 +1503,7 @@ export const defaultConfig = Object.freeze({
globalNotFound: false, globalNotFound: false,
browserDebugInfoInTerminal: false, browserDebugInfoInTerminal: false,
optimizeRouterScrolling: false, optimizeRouterScrolling: false,
isolatedDevBuild: false,
}, },
htmlLimitedBots: undefined, htmlLimitedBots: undefined,
bundlePagesRouterDependencies: false, bundlePagesRouterDependencies: false,
......
...@@ -215,7 +215,8 @@ function assignDefaultsAndValidate( ...@@ -215,7 +215,8 @@ function assignDefaultsAndValidate(
dir: string, dir: string,
userConfig: NextConfig & { configFileName: string }, userConfig: NextConfig & { configFileName: string },
silent: boolean, silent: boolean,
configuredExperimentalFeatures: ConfiguredExperimentalFeature[] configuredExperimentalFeatures: ConfiguredExperimentalFeature[],
phase: PHASE_TYPE
): NextConfigComplete { ): NextConfigComplete {
const configFileName = userConfig.configFileName const configFileName = userConfig.configFileName
if (typeof userConfig.exportTrailingSlash !== 'undefined') { if (typeof userConfig.exportTrailingSlash !== 'undefined') {
...@@ -1198,6 +1199,13 @@ function assignDefaultsAndValidate( ...@@ -1198,6 +1199,13 @@ function assignDefaultsAndValidate(
) )
} }
if (
phase === PHASE_DEVELOPMENT_SERVER &&
result.experimental?.isolatedDevBuild
) {
result.distDir = join(result.distDir, 'dev')
}
return result as NextConfigComplete return result as NextConfigComplete
} }
...@@ -1362,7 +1370,8 @@ export default async function loadConfig( ...@@ -1362,7 +1370,8 @@ export default async function loadConfig(
...customConfig, ...customConfig,
}, },
silent, silent,
configuredExperimentalFeatures configuredExperimentalFeatures,
phase
) as NextConfigComplete, ) as NextConfigComplete,
phase, phase,
silent silent
...@@ -1569,7 +1578,8 @@ export default async function loadConfig( ...@@ -1569,7 +1578,8 @@ export default async function loadConfig(
...userConfig, ...userConfig,
}, },
silent, silent,
configuredExperimentalFeatures configuredExperimentalFeatures,
phase
) as NextConfigComplete ) as NextConfigComplete
const finalConfig = await applyModifyConfig(completeConfig, phase, silent) const finalConfig = await applyModifyConfig(completeConfig, phase, silent)
...@@ -1624,7 +1634,8 @@ export default async function loadConfig( ...@@ -1624,7 +1634,8 @@ export default async function loadConfig(
dir, dir,
{ ...clonedDefaultConfig, configFileName }, { ...clonedDefaultConfig, configFileName },
silent, silent,
configuredExperimentalFeatures configuredExperimentalFeatures,
phase
) as NextConfigComplete ) as NextConfigComplete
setHttpClientAndAgentOptions(completeConfig) setHttpClientAndAgentOptions(completeConfig)
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>hello world</p>
}
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('isolated-dev-build', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should create dev artifacts in .next/dev/ directory', async () => {
expect(await next.hasFile('.next/dev')).toBe(true)
expect(await next.hasFile('.next/server')).toBe(false)
})
it('should work with HMR', async () => {
const browser = await next.browser('/')
expect(await browser.elementByCss('p').text()).toBe('hello world')
await next.patchFile('app/page.tsx', (content) => {
return content.replace('hello world', 'hello updated world')
})
await retry(async () => {
expect(await browser.elementByCss('p').text()).toBe('hello updated world')
})
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
isolatedDevBuild: true,
},
}
module.exports = nextConfig
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