Unverified Commit 422c93ef authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Add `serverComponentsHmrCancellation` experimental flag (#95462)

This is the first change in a series that will let `next dev` cancel a
Server Components HMR refresh once a newer refresh supersedes it. Here
we only introduce the `experimental.serverComponentsHmrCancellation`
flag and plumb it to every place later changes will read it, without
adding any behavior yet, so on its own this change is an inert no-op.

The flag is declared in `ExperimentalConfig`, validated in the Zod
schema, and defaults to `false` in `defaultConfig`. We thread it into
the runtime config via the `NextConfigRuntime` pick list and
`getNextConfigRuntime`, expose it to user-bundled client code through
`define-env` as `process.env.__NEXT_SERVER_COMPONENTS_HMR_CANCELLATION`,
and pass it into the app render options in `base-server.ts` and the
`app-page` build template. The edge SSR template hardcodes it to
`false`, because edge rendering does not expose the Node response-close
signal the cancellation relies on.

So that the following changes are exercised as soon as their behavior
lands, `config.ts` honors a
`__NEXT_EXPERIMENTAL_SERVER_COMPONENTS_HMR_CANCELLATION` environment
variable that forces the flag on while still respecting an explicit user
config value, mirroring how `cachedNavigations` is rolled out. Both the
`test-cache-components-dev` and `test-cache-components-prod` CI shards
export that variable: the development shard so the feature is exercised
once its behavior lands, and the production shard as a guard that this
development-only feature stays inert in production even with the flag
enabled. The default stays `false`; a later change flips it once the
implementation is complete and drops the shard overrides.
parent 65dbff0a
......@@ -991,6 +991,7 @@ jobs:
export __NEXT_CACHE_COMPONENTS=true
export __NEXT_EXPERIMENTAL_CACHED_NAVIGATIONS=true
export __NEXT_EXPERIMENTAL_APP_NEW_SCROLL_HANDLER=true
export __NEXT_EXPERIMENTAL_SERVER_COMPONENTS_HMR_CANCELLATION=true
export NEXT_EXTERNAL_TESTS_FILTERS="test/cache-components-tests-manifest.json"
export NEXT_TEST_MODE=dev
export IS_TURBOPACK_TEST=1
......@@ -1027,6 +1028,7 @@ jobs:
export __NEXT_CACHE_COMPONENTS=true
export __NEXT_EXPERIMENTAL_CACHED_NAVIGATIONS=true
export __NEXT_EXPERIMENTAL_APP_NEW_SCROLL_HANDLER=true
export __NEXT_EXPERIMENTAL_SERVER_COMPONENTS_HMR_CANCELLATION=true
export NEXT_EXTERNAL_TESTS_FILTERS="test/cache-components-tests-manifest.json"
export NEXT_TEST_MODE=start
export IS_TURBOPACK_TEST=1
......
......@@ -245,6 +245,9 @@ export function getDefineEnv({
'process.env.__NEXT_CLIENT_VALIDATE_RSC_REQUEST_HEADERS': Boolean(
config.experimental.validateRSCRequestHeaders
),
'process.env.__NEXT_SERVER_COMPONENTS_HMR_CANCELLATION': Boolean(
config.experimental.serverComponentsHmrCancellation
),
'process.env.__NEXT_DYNAMIC_ON_HOVER': Boolean(
config.experimental.dynamicOnHover
),
......
......@@ -922,6 +922,9 @@ export async function handler(
inlineCss: Boolean(nextConfig.experimental.inlineCss),
prefetchInlining: nextConfig.experimental.prefetchInlining ?? false,
authInterrupts: Boolean(nextConfig.experimental.authInterrupts),
serverComponentsHmrCancellation: Boolean(
nextConfig.experimental.serverComponentsHmrCancellation
),
useCacheTimeout: nextConfig.experimental.useCacheTimeout,
cachedNavigations:
nextConfig.experimental.cachedNavigations ?? false,
......
......@@ -174,6 +174,9 @@ async function requestHandler(
inlineCss: Boolean(nextConfig.experimental.inlineCss),
prefetchInlining: nextConfig.experimental.prefetchInlining ?? false,
authInterrupts: Boolean(nextConfig.experimental.authInterrupts),
// Edge has no Node response-close signal, so HMR cancellation is a
// no-op.
serverComponentsHmrCancellation: false,
useCacheTimeout: nextConfig.experimental.useCacheTimeout,
cachedNavigations: nextConfig.experimental.cachedNavigations ?? false,
appShells: nextConfig.experimental.appShells,
......
......@@ -167,6 +167,7 @@ export interface RenderOptsPartial {
inlineCss: boolean
prefetchInlining: PrefetchInliningConfig
authInterrupts: boolean
serverComponentsHmrCancellation?: boolean
useCacheTimeout: number
cachedNavigations: boolean | 'allow-runtime'
appShells: ExperimentalConfig['appShells']
......
......@@ -585,6 +585,8 @@ export default abstract class Server<
prefetchInlining:
this.nextConfig.experimental.prefetchInlining ?? false,
authInterrupts: !!this.nextConfig.experimental.authInterrupts,
serverComponentsHmrCancellation:
this.nextConfig.experimental.serverComponentsHmrCancellation,
useCacheTimeout: this.nextConfig.experimental.useCacheTimeout,
cachedNavigations:
this.nextConfig.experimental.cachedNavigations ?? false,
......
......@@ -440,6 +440,7 @@ export const experimentalSchema = {
staticGenerationMinPagesPerWorker: z.number().int().optional(),
typedEnv: z.boolean().optional(),
serverComponentsHmrCache: z.boolean().optional(),
serverComponentsHmrCancellation: z.boolean().optional(),
authInterrupts: z.boolean().optional(),
useCache: z.boolean().optional(),
durableUseCacheEntries: z.boolean().optional(),
......
......@@ -1195,6 +1195,12 @@ export interface ExperimentalConfig {
*/
serverComponentsHmrCache?: boolean
/**
* Cancels the render and validation work for a Server Component HMR refresh
* once a newer refresh supersedes it. Development only.
*/
serverComponentsHmrCancellation?: boolean
/**
* Render <style> tags inline in the HTML for imported CSS assets.
* Supports app-router in production mode only.
......@@ -2157,6 +2163,7 @@ export const defaultConfig = Object.freeze({
reactDebugChannel: true,
staticGenerationRetryCount: undefined,
serverComponentsHmrCache: true,
serverComponentsHmrCancellation: false,
staticGenerationMaxConcurrency: 8,
staticGenerationMinPagesPerWorker: 25,
transitionIndicator: false,
......@@ -2270,6 +2277,7 @@ export interface NextConfigRuntime {
| 'disableOptimizedLoading'
| 'largePageDataBytes'
| 'serverComponentsHmrCache'
| 'serverComponentsHmrCancellation'
| 'caseSensitiveRoutes'
| 'validateRSCRequestHeaders'
| 'sri'
......@@ -2338,6 +2346,7 @@ export function getNextConfigRuntime(
disableOptimizedLoading: ex.disableOptimizedLoading,
largePageDataBytes: ex.largePageDataBytes,
serverComponentsHmrCache: ex.serverComponentsHmrCache,
serverComponentsHmrCancellation: ex.serverComponentsHmrCancellation,
caseSensitiveRoutes: ex.caseSensitiveRoutes,
validateRSCRequestHeaders: ex.validateRSCRequestHeaders,
sri: ex.sri,
......
......@@ -2223,6 +2223,26 @@ function enforceExperimentalFeatures(
}
}
// TODO: Remove this once serverComponentsHmrCancellation is the default.
if (
process.env.__NEXT_EXPERIMENTAL_SERVER_COMPONENTS_HMR_CANCELLATION ===
'true' &&
// We do respect an explicit value in the user config.
(config.experimental.serverComponentsHmrCancellation === undefined ||
(isDefaultConfig && !config.experimental.serverComponentsHmrCancellation))
) {
config.experimental.serverComponentsHmrCancellation = true
if (configuredExperimentalFeatures) {
addConfiguredExperimentalFeature(
configuredExperimentalFeatures,
'serverComponentsHmrCancellation',
true,
'enabled by `__NEXT_EXPERIMENTAL_SERVER_COMPONENTS_HMR_CANCELLATION`'
)
}
}
// Enable cachedNavigations by default when cacheComponents is enabled.
// cachedNavigations relies on Cache Components rendering to do anything
// useful, so the two features are tied together: we only flip the default
......
......@@ -24,6 +24,7 @@ describe('build-output-prerender', () => {
- Experiments (use with caution):
✓ appNewScrollHandler (enabled by \`__NEXT_EXPERIMENTAL_APP_NEW_SCROLL_HANDLER\`)
✓ cachedNavigations (enabled by \`__NEXT_EXPERIMENTAL_CACHED_NAVIGATIONS\`)
✓ serverComponentsHmrCancellation (enabled by \`__NEXT_EXPERIMENTAL_SERVER_COMPONENTS_HMR_CANCELLATION\`)
· staticGenerationMaxConcurrency: 1"
`)
} else if (isRspack) {
......@@ -153,6 +154,7 @@ describe('build-output-prerender', () => {
✓ appNewScrollHandler (enabled by \`__NEXT_EXPERIMENTAL_APP_NEW_SCROLL_HANDLER\`)
✓ cachedNavigations (enabled by \`__NEXT_EXPERIMENTAL_CACHED_NAVIGATIONS\`)
⨯ prerenderEarlyExit (disabled by \`--debug-prerender\`)
✓ serverComponentsHmrCancellation (enabled by \`__NEXT_EXPERIMENTAL_SERVER_COMPONENTS_HMR_CANCELLATION\`)
✓ serverSourceMaps (enabled by \`--debug-prerender\`)
· staticGenerationMaxConcurrency: 1
⨯ turbopackMinify (disabled by \`--debug-prerender\`)"
......@@ -357,7 +359,8 @@ describe('build-output-prerender', () => {
- Cache Components enabled
- Experiments (use with caution):
✓ appNewScrollHandler (enabled by \`__NEXT_EXPERIMENTAL_APP_NEW_SCROLL_HANDLER\`)
✓ cachedNavigations (enabled by \`__NEXT_EXPERIMENTAL_CACHED_NAVIGATIONS\`)"
✓ cachedNavigations (enabled by \`__NEXT_EXPERIMENTAL_CACHED_NAVIGATIONS\`)
✓ serverComponentsHmrCancellation (enabled by \`__NEXT_EXPERIMENTAL_SERVER_COMPONENTS_HMR_CANCELLATION\`)"
`)
} else if (isRspack) {
expect(getPreambleOutput(next.cliOutput)).toMatchInlineSnapshot(`
......@@ -427,6 +430,7 @@ describe('build-output-prerender', () => {
✓ appNewScrollHandler (enabled by \`__NEXT_EXPERIMENTAL_APP_NEW_SCROLL_HANDLER\`)
✓ cachedNavigations (enabled by \`__NEXT_EXPERIMENTAL_CACHED_NAVIGATIONS\`)
⨯ prerenderEarlyExit (disabled by \`--debug-prerender\`)
✓ serverComponentsHmrCancellation (enabled by \`__NEXT_EXPERIMENTAL_SERVER_COMPONENTS_HMR_CANCELLATION\`)
✓ serverSourceMaps (enabled by \`--debug-prerender\`)
⨯ turbopackMinify (disabled by \`--debug-prerender\`)"
`)
......
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