diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index 6a27607e82909..68ba6f9d97f58 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -1351,15 +1351,14 @@ export default async function build( env: process.env as Record, defineEnv: createDefineEnv({ isTurbopack: true, - allowedRevalidateHeaderKeys: undefined, - clientRouterFilters: undefined, + clientRouterFilters: NextBuildContext.clientRouterFilters, config, dev, distDir, - fetchCacheKeyPrefix: undefined, + fetchCacheKeyPrefix: config.experimental.fetchCacheKeyPrefix, hasRewrites, + // TODO: Implement middlewareMatchers: undefined, - previewModeId: undefined, }), }) diff --git a/packages/next/src/build/swc/index.ts b/packages/next/src/build/swc/index.ts index 127af725a8a17..c332948c43935 100644 --- a/packages/next/src/build/swc/index.ts +++ b/packages/next/src/build/swc/index.ts @@ -435,7 +435,6 @@ export interface DefineEnv { export function createDefineEnv({ isTurbopack, - allowedRevalidateHeaderKeys, clientRouterFilters, config, dev, @@ -443,7 +442,6 @@ export function createDefineEnv({ fetchCacheKeyPrefix, hasRewrites, middlewareMatchers, - previewModeId, }: Omit< DefineEnvPluginOptions, 'isClient' | 'isNodeOrEdgeCompilation' | 'isEdgeServer' | 'isNodeServer' @@ -458,7 +456,6 @@ export function createDefineEnv({ defineEnv[variant] = rustifyEnv( getDefineEnv({ isTurbopack, - allowedRevalidateHeaderKeys, clientRouterFilters, config, dev, @@ -470,7 +467,6 @@ export function createDefineEnv({ isNodeOrEdgeCompilation: variant === 'nodejs' || variant === 'edge', isNodeServer: variant === 'nodejs', middlewareMatchers, - previewModeId, }) ) } diff --git a/packages/next/src/build/webpack-config.ts b/packages/next/src/build/webpack-config.ts index 9a0c6eb6d5fe3..22d2ac187e640 100644 --- a/packages/next/src/build/webpack-config.ts +++ b/packages/next/src/build/webpack-config.ts @@ -318,9 +318,7 @@ export default async function getBaseWebpackConfig( resolvedBaseUrl, supportedBrowsers, clientRouterFilters, - previewModeId, fetchCacheKeyPrefix, - allowedRevalidateHeaderKeys, }: { buildId: string config: NextConfigComplete @@ -348,9 +346,7 @@ export default async function getBaseWebpackConfig( import('../shared/lib/bloom-filter').BloomFilter['export'] > } - previewModeId?: string fetchCacheKeyPrefix?: string - allowedRevalidateHeaderKeys?: string[] } ): Promise { const isClient = compilerType === COMPILER_NAMES.client @@ -1741,7 +1737,6 @@ export default async function getBaseWebpackConfig( }), getDefineEnvPlugin({ isTurbopack: false, - allowedRevalidateHeaderKeys, clientRouterFilters, config, dev, @@ -1753,7 +1748,6 @@ export default async function getBaseWebpackConfig( isNodeOrEdgeCompilation, isNodeServer, middlewareMatchers, - previewModeId, }), isClient && new ReactLoadablePlugin({ diff --git a/packages/next/src/build/webpack/plugins/define-env-plugin.ts b/packages/next/src/build/webpack/plugins/define-env-plugin.ts index 9d34c494bdc62..ab29d920c195c 100644 --- a/packages/next/src/build/webpack/plugins/define-env-plugin.ts +++ b/packages/next/src/build/webpack/plugins/define-env-plugin.ts @@ -23,7 +23,6 @@ type BloomFilter = ReturnType< export interface DefineEnvPluginOptions { isTurbopack: boolean - allowedRevalidateHeaderKeys: string[] | undefined clientRouterFilters?: { staticFilter: BloomFilter dynamicFilter: BloomFilter @@ -38,7 +37,6 @@ export interface DefineEnvPluginOptions { isNodeOrEdgeCompilation: boolean isNodeServer: boolean middlewareMatchers: MiddlewareMatcher[] | undefined - previewModeId: string | undefined } interface DefineEnv { @@ -46,7 +44,6 @@ interface DefineEnv { | string | string[] | boolean - | undefined | MiddlewareMatcher[] | BloomFilter | Partial @@ -129,7 +126,6 @@ function getImageConfig( export function getDefineEnv({ isTurbopack, - allowedRevalidateHeaderKeys, clientRouterFilters, config, dev, @@ -141,7 +137,6 @@ export function getDefineEnv({ isNodeOrEdgeCompilation, isNodeServer, middlewareMatchers, - previewModeId, }: DefineEnvPluginOptions): SerializedDefineEnv { const defineEnv: DefineEnv = { // internal field to identify the plugin config @@ -158,7 +153,7 @@ export function getDefineEnv({ * and library authors to have different implementations based on * the runtime they are running with, if it's not using `edge-runtime` */ - process.env.NEXT_EDGE_RUNTIME_PROVIDER || 'edge-runtime', + process.env.NEXT_EDGE_RUNTIME_PROVIDER ?? 'edge-runtime', }), 'process.turbopack': isTurbopack, 'process.env.TURBOPACK': isTurbopack, @@ -172,70 +167,71 @@ export function getDefineEnv({ 'process.env.NEXT_MINIMAL': '', 'process.env.__NEXT_PPR': config.experimental.ppr === true, 'process.env.__NEXT_ACTIONS_DEPLOYMENT_ID': - config.experimental.useDeploymentIdServerActions, - 'process.env.NEXT_DEPLOYMENT_ID': config.experimental.deploymentId || false, - 'process.env.__NEXT_FETCH_CACHE_KEY_PREFIX': fetchCacheKeyPrefix, - 'process.env.__NEXT_PREVIEW_MODE_ID': previewModeId, - 'process.env.__NEXT_ALLOWED_REVALIDATE_HEADERS': - allowedRevalidateHeaderKeys, - 'process.env.__NEXT_MIDDLEWARE_MATCHERS': middlewareMatchers || [], + config.experimental.useDeploymentIdServerActions ?? false, + 'process.env.NEXT_DEPLOYMENT_ID': config.experimental.deploymentId ?? false, + 'process.env.__NEXT_FETCH_CACHE_KEY_PREFIX': fetchCacheKeyPrefix ?? '', + 'process.env.__NEXT_MIDDLEWARE_MATCHERS': middlewareMatchers ?? [], 'process.env.__NEXT_MANUAL_CLIENT_BASE_PATH': - config.experimental.manualClientBasePath, + config.experimental.manualClientBasePath ?? false, 'process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED': - config.experimental.clientRouterFilter, + config.experimental.clientRouterFilter ?? true, 'process.env.__NEXT_CLIENT_ROUTER_S_FILTER': - clientRouterFilters?.staticFilter, + clientRouterFilters?.staticFilter ?? false, 'process.env.__NEXT_CLIENT_ROUTER_D_FILTER': - clientRouterFilters?.dynamicFilter, + clientRouterFilters?.dynamicFilter ?? false, 'process.env.__NEXT_OPTIMISTIC_CLIENT_CACHE': - config.experimental.optimisticClientCache, + config.experimental.optimisticClientCache ?? true, 'process.env.__NEXT_MIDDLEWARE_PREFETCH': - config.experimental.middlewarePrefetch, + config.experimental.middlewarePrefetch ?? 'flexible', 'process.env.__NEXT_CROSS_ORIGIN': config.crossOrigin, 'process.browser': isClient, - 'process.env.__NEXT_TEST_MODE': process.env.__NEXT_TEST_MODE, + 'process.env.__NEXT_TEST_MODE': process.env.__NEXT_TEST_MODE ?? false, // This is used in client/dev-error-overlay/hot-dev-client.js to replace the dist directory - ...(dev && (isClient || isEdgeServer) + ...(dev && (isClient ?? isEdgeServer) ? { 'process.env.__NEXT_DIST_DIR': distDir, } : {}), 'process.env.__NEXT_TRAILING_SLASH': config.trailingSlash, - 'process.env.__NEXT_BUILD_INDICATOR': config.devIndicators.buildActivity, + 'process.env.__NEXT_BUILD_INDICATOR': + config.devIndicators.buildActivity ?? true, 'process.env.__NEXT_BUILD_INDICATOR_POSITION': - config.devIndicators.buildActivityPosition, + config.devIndicators.buildActivityPosition ?? 'bottom-right', 'process.env.__NEXT_STRICT_MODE': config.reactStrictMode === null ? false : config.reactStrictMode, 'process.env.__NEXT_STRICT_MODE_APP': // When next.config.js does not have reactStrictMode it's enabled by default. config.reactStrictMode === null ? true : config.reactStrictMode, 'process.env.__NEXT_OPTIMIZE_FONTS': !dev && config.optimizeFonts, - 'process.env.__NEXT_OPTIMIZE_CSS': config.experimental.optimizeCss && !dev, + 'process.env.__NEXT_OPTIMIZE_CSS': + (config.experimental.optimizeCss && !dev) ?? false, 'process.env.__NEXT_SCRIPT_WORKERS': - config.experimental.nextScriptWorkers && !dev, + (config.experimental.nextScriptWorkers && !dev) ?? false, 'process.env.__NEXT_SCROLL_RESTORATION': - config.experimental.scrollRestoration, + config.experimental.scrollRestoration ?? false, ...getImageConfig(config, dev), 'process.env.__NEXT_ROUTER_BASEPATH': config.basePath, - 'process.env.__NEXT_STRICT_NEXT_HEAD': config.experimental.strictNextHead, + 'process.env.__NEXT_STRICT_NEXT_HEAD': + config.experimental.strictNextHead ?? false, 'process.env.__NEXT_HAS_REWRITES': hasRewrites, 'process.env.__NEXT_CONFIG_OUTPUT': config.output, 'process.env.__NEXT_I18N_SUPPORT': !!config.i18n, - 'process.env.__NEXT_I18N_DOMAINS': config.i18n?.domains, + 'process.env.__NEXT_I18N_DOMAINS': config.i18n?.domains ?? false, 'process.env.__NEXT_ANALYTICS_ID': config.analyticsId, // TODO: remove in the next major version 'process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE': config.skipMiddlewareUrlNormalize, 'process.env.__NEXT_EXTERNAL_MIDDLEWARE_REWRITE_RESOLVE': - config.experimental.externalMiddlewareRewritesResolve, + config.experimental.externalMiddlewareRewritesResolve ?? false, 'process.env.__NEXT_MANUAL_TRAILING_SLASH': config.skipTrailingSlashRedirect, 'process.env.__NEXT_HAS_WEB_VITALS_ATTRIBUTION': - config.experimental.webVitalsAttribution && - config.experimental.webVitalsAttribution.length > 0, + (config.experimental.webVitalsAttribution && + config.experimental.webVitalsAttribution.length > 0) ?? + false, 'process.env.__NEXT_WEB_VITALS_ATTRIBUTION': - config.experimental.webVitalsAttribution, + config.experimental.webVitalsAttribution ?? false, 'process.env.__NEXT_LINK_NO_TOUCH_START': - config.experimental.linkNoTouchStart, + config.experimental.linkNoTouchStart ?? false, 'process.env.__NEXT_ASSET_PREFIX': config.assetPrefix, ...(isNodeOrEdgeCompilation ? { diff --git a/packages/next/src/server/dev/hot-reloader-turbopack.ts b/packages/next/src/server/dev/hot-reloader-turbopack.ts index 0f8481a5983ea..f0b7f29393054 100644 --- a/packages/next/src/server/dev/hot-reloader-turbopack.ts +++ b/packages/next/src/server/dev/hot-reloader-turbopack.ts @@ -129,15 +129,15 @@ export async function createHotReloaderTurbopack( env: process.env as Record, defineEnv: createDefineEnv({ isTurbopack: true, - allowedRevalidateHeaderKeys: undefined, + // TODO: Implement clientRouterFilters: undefined, config: nextConfig, dev: true, distDir, - fetchCacheKeyPrefix: undefined, + fetchCacheKeyPrefix: opts.nextConfig.experimental.fetchCacheKeyPrefix, hasRewrites, + // TODO: Implement middlewareMatchers: undefined, - previewModeId: undefined, }), }) const entrypointsSubscription = project.entrypointsSubscribe() diff --git a/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts b/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts index 9f5ffc4bc0b7f..49fa02208e578 100644 --- a/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts +++ b/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts @@ -553,15 +553,15 @@ async function startWatcher(opts: SetupOpts) { await hotReloader.turbopackProject.update({ defineEnv: createDefineEnv({ isTurbopack: true, - allowedRevalidateHeaderKeys: undefined, clientRouterFilters, config: nextConfig, dev: true, distDir, - fetchCacheKeyPrefix: undefined, + fetchCacheKeyPrefix: + opts.nextConfig.experimental.fetchCacheKeyPrefix, hasRewrites, + // TODO: Implement middlewareMatchers: undefined, - previewModeId: undefined, }), }) } @@ -625,19 +625,18 @@ async function startWatcher(opts: SetupOpts) { ) { const newDefine = getDefineEnv({ isTurbopack: false, - allowedRevalidateHeaderKeys: undefined, clientRouterFilters, config: nextConfig, dev: true, distDir, - fetchCacheKeyPrefix: undefined, + fetchCacheKeyPrefix: + opts.nextConfig.experimental.fetchCacheKeyPrefix, hasRewrites, isClient, isEdgeServer, isNodeOrEdgeCompilation: isNodeServer || isEdgeServer, isNodeServer, middlewareMatchers: undefined, - previewModeId: undefined, }) Object.keys(plugin.definitions).forEach((key) => { diff --git a/packages/next/src/shared/lib/router/router.ts b/packages/next/src/shared/lib/router/router.ts index 3865d9d0f3ae5..5e4ffceaee7fa 100644 --- a/packages/next/src/shared/lib/router/router.ts +++ b/packages/next/src/shared/lib/router/router.ts @@ -765,13 +765,24 @@ export default class Router implements BaseRouter { const { BloomFilter } = require('../../lib/bloom-filter') as typeof import('../../lib/bloom-filter') - const staticFilterData: - | ReturnType - | undefined = process.env.__NEXT_CLIENT_ROUTER_S_FILTER as any + type Filter = ReturnType< + import('../../lib/bloom-filter').BloomFilter['export'] + > - const dynamicFilterData: typeof staticFilterData = process.env + const routerFilterSValue: Filter | false = process.env + .__NEXT_CLIENT_ROUTER_S_FILTER as any + + const staticFilterData: Filter | undefined = routerFilterSValue + ? routerFilterSValue + : undefined + + const routerFilterDValue: Filter | false = process.env .__NEXT_CLIENT_ROUTER_D_FILTER as any + const dynamicFilterData: Filter | undefined = routerFilterDValue + ? routerFilterDValue + : undefined + if (staticFilterData?.numHashes) { this._bfl_s = new BloomFilter( staticFilterData.numItems, diff --git a/test/development/basic/next-rs-api.test.ts b/test/development/basic/next-rs-api.test.ts index 70ecaddaeca36..04cde12ede22d 100644 --- a/test/development/basic/next-rs-api.test.ts +++ b/test/development/basic/next-rs-api.test.ts @@ -205,7 +205,6 @@ describe('next.rs api', () => { dev: true, defineEnv: createDefineEnv({ isTurbopack: true, - allowedRevalidateHeaderKeys: undefined, clientRouterFilters: undefined, config: nextConfig, dev: true, @@ -218,7 +217,6 @@ describe('next.rs api', () => { fetchCacheKeyPrefix: undefined, hasRewrites: false, middlewareMatchers: undefined, - previewModeId: undefined, }), }) projectUpdateSubscription = filterMapAsyncIterator(