Unverified Commit a3d7ab97 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Fork navigation-testing-lock module (#95611)

Ensures the module graph is not bundled. Right now this has no effect
since all the modules imported by `navigation-test-lock` have legit uses
in prod builds.

Also nice to not have to fork every single implementation inline.
parent c3d1e893
...@@ -160,11 +160,19 @@ pub async fn get_client_resolve_options_context( ...@@ -160,11 +160,19 @@ pub async fn get_client_resolve_options_context(
let next_client_fallback_import_map = get_next_client_fallback_import_map(ty.clone()) let next_client_fallback_import_map = get_next_client_fallback_import_map(ty.clone())
.to_resolved() .to_resolved()
.await?; .await?;
let next_client_resolved_map = let expose_testing_api = mode.await?.is_development()
get_next_client_resolved_map(project_path.clone(), project_path.clone(), *mode.await?) || *next_config
.await? .enable_expose_testing_api_in_production_build()
.to_resolved()
.await?; .await?;
let next_client_resolved_map = get_next_client_resolved_map(
project_path.clone(),
project_path.clone(),
*mode.await?,
expose_testing_api,
)
.await?
.to_resolved()
.await?;
let mut custom_conditions: Vec<_> = mode.await?.custom_resolve_conditions().collect(); let mut custom_conditions: Vec<_> = mode.await?.custom_resolve_conditions().collect();
if *next_config.enable_cache_components().await? { if *next_config.enable_cache_components().await? {
......
...@@ -1292,6 +1292,7 @@ pub struct ExperimentalConfig { ...@@ -1292,6 +1292,7 @@ pub struct ExperimentalConfig {
durable_use_cache_entries: Option<bool>, durable_use_cache_entries: Option<bool>,
runtime_server_deployment_id: Option<bool>, runtime_server_deployment_id: Option<bool>,
supports_immutable_assets: Option<bool>, supports_immutable_assets: Option<bool>,
expose_testing_api_in_production_build: Option<bool>,
/// A salt to mix into chunk and asset content hashes. Empty string means /// A salt to mix into chunk and asset content hashes. Empty string means
/// no salt. /// no salt.
...@@ -2332,6 +2333,15 @@ impl NextConfig { ...@@ -2332,6 +2333,15 @@ impl NextConfig {
Vc::cell(self.experimental.blocking_ssr.unwrap_or(false)) Vc::cell(self.experimental.blocking_ssr.unwrap_or(false))
} }
#[turbo_tasks::function]
pub fn enable_expose_testing_api_in_production_build(&self) -> Vc<bool> {
Vc::cell(
self.experimental
.expose_testing_api_in_production_build
.unwrap_or(false),
)
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn enable_cache_components(&self) -> Vc<bool> { pub fn enable_cache_components(&self) -> Vc<bool> {
Vc::cell(self.cache_components.unwrap_or(false)) Vc::cell(self.cache_components.unwrap_or(false))
......
...@@ -568,6 +568,7 @@ pub async fn get_next_client_resolved_map( ...@@ -568,6 +568,7 @@ pub async fn get_next_client_resolved_map(
context_path: FileSystemPath, context_path: FileSystemPath,
root: FileSystemPath, root: FileSystemPath,
_mode: NextMode, _mode: NextMode,
expose_testing_api: bool,
) -> Result<Vc<ResolvedMap>> { ) -> Result<Vc<ResolvedMap>> {
// In the browser bundle, swap every module that has a `.browser` sibling (see // In the browser bundle, swap every module that has a `.browser` sibling (see
// BROWSER_VARIANT_MODULES, generated from the filesystem) for that sibling. The default // BROWSER_VARIANT_MODULES, generated from the filesystem) for that sibling. The default
...@@ -579,7 +580,7 @@ pub async fn get_next_client_resolved_map( ...@@ -579,7 +580,7 @@ pub async fn get_next_client_resolved_map(
// filesystem root so it matches wherever `next` resolves from (node_modules, pnpm store, // filesystem root so it matches wherever `next` resolves from (node_modules, pnpm store,
// or monorepo `packages/next`). // or monorepo `packages/next`).
let fs_root = root.root().owned().await?; let fs_root = root.root().owned().await?;
let mut glob_mappings = Vec::with_capacity(BROWSER_VARIANT_MODULES.len()); let mut glob_mappings = Vec::with_capacity(BROWSER_VARIANT_MODULES.len() + 1);
for module in BROWSER_VARIANT_MODULES { for module in BROWSER_VARIANT_MODULES {
glob_mappings.push(( glob_mappings.push((
fs_root.clone(), fs_root.clone(),
...@@ -595,6 +596,30 @@ pub async fn get_next_client_resolved_map( ...@@ -595,6 +596,30 @@ pub async fn get_next_client_resolved_map(
), ),
)); ));
} }
// When the Instant Navigation Testing API is disabled (production build
// without `experimental.exposeTestingApiInProductionBuild`), swap the
// navigation lock implementation for an inert shim so the testing
// machinery does not ship in the browser bundle. This mirrors the webpack
// alias in `create-compiler-aliases.ts`.
if !expose_testing_api {
glob_mappings.push((
fs_root,
Glob::new(
rcstr!("**/next/dist/client/components/segment-cache/navigation-testing-lock.js"),
GlobOptions::default(),
)
.to_resolved()
.await?,
request_to_import_mapping(
context_path.clone(),
rcstr!(
"next/dist/client/components/segment-cache/navigation-testing-lock.disabled"
),
),
));
}
Ok(ResolvedMap { Ok(ResolvedMap {
by_glob: glob_mappings, by_glob: glob_mappings,
} }
......
...@@ -180,6 +180,22 @@ export function createWebpackAliases({ ...@@ -180,6 +180,22 @@ export function createWebpackAliases({
`next/dist/${moduleId}.browser`, `next/dist/${moduleId}.browser`,
]) ])
), ),
// When the Instant Navigation Testing API is disabled (production
// build without `experimental.exposeTestingApiInProductionBuild`),
// swap the navigation lock implementation for an inert shim so the
// testing machinery does not ship in the browser bundle. Same
// resolved-path matching as the browser-variant swap above.
...(!dev &&
config.experimental.exposeTestingApiInProductionBuild !== true
? {
[path.join(
NEXT_PROJECT_ROOT_DIST,
'client/components/segment-cache/navigation-testing-lock.js'
) + '$']:
'next/dist/client/components/segment-cache/navigation-testing-lock.disabled',
}
: {}),
} }
: {}), : {}),
......
/**
* Inert stand-in for `./navigation-testing-lock`.
*
* When the Instant Navigation Testing API is disabled (a production build
* without `experimental.exposeTestingApiInProductionBuild`), the browser
* bundle resolves `./navigation-testing-lock` to this module instead of the
* real implementation, so none of the lock machinery ships. The alias is set
* up in `create-compiler-aliases.ts` (webpack) and
* `crates/next-core/src/next_import_map.rs` (Turbopack).
*
* Every export mirrors the real module's signature and returns the value the
* real implementation produces when no lock is held.
*/
import type { FlightRouterState } from '../../../shared/lib/app-router-types'
import type { PendingSegmentCacheEntry, SegmentCacheEntry } from './cache'
import type { FetchStrategy } from './types'
import type {
NavigationLockPrefetch,
NavigationLockState,
} from './navigation-testing-lock'
export type {
NavigationLockPrefetch,
NavigationLockState,
} from './navigation-testing-lock'
export function getPreLockFetch(): typeof fetch | null {
return null
}
export function beginNavigationLockPrefetch(): NavigationLockPrefetch | null {
return null
}
export function recordNavigationLockOwnedEntry(
_entry: SegmentCacheEntry
): void {}
export function trackNavigationLockPrefetchEntry(
_prefetch: NavigationLockPrefetch,
_entry: PendingSegmentCacheEntry
): void {}
export function finishNavigationLockPrefetchSpawning(
_prefetch: NavigationLockPrefetch
): void {}
export function startListeningForInstantNavigationCookie(): void {}
export function updateCapturedSPAToTree(
_fromTree: FlightRouterState,
_toTree: FlightRouterState
): void {}
export function isNavigationLocked(): boolean {
return false
}
export function getCurrentNavigationLock(): NavigationLockState | null {
return null
}
export function shouldRestrictNavigationToShell(
_rootPrefetchHints: number,
_linkFetchStrategy: FetchStrategy
): boolean {
return false
}
export async function waitForNavigationLockIfActive(
_lock: NavigationLockState | null = null
): Promise<void> {}
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