share asset and chunking context between server, server data and client navigation (vercel/turbo#3738)

This improves the performance for client-side navigation as it can reuse the modules from the initial navigation. Same for server data requests.
This commit is contained in:
Tobias Koppers 2023-02-10 13:10:52 +01:00 committed by GitHub
parent 3679862c21
commit c039615006
4 changed files with 43 additions and 42 deletions

View file

@ -21,7 +21,7 @@ use turbopack::{
use turbopack_core::{
chunk::dev::DevChunkingContextVc,
context::{AssetContext, AssetContextVc},
environment::ServerAddrVc,
environment::{EnvironmentVc, ServerAddrVc},
issue::{Issue, IssueSeverity, IssueSeverityVc, IssueVc},
virtual_asset::VirtualAssetVc,
};
@ -78,11 +78,10 @@ async fn next_client_transition(
server_root: FileSystemPathVc,
app_dir: FileSystemPathVc,
env: ProcessEnvVc,
browserslist_query: &str,
client_environment: EnvironmentVc,
next_config: NextConfigVc,
) -> Result<TransitionVc> {
let ty = Value::new(ClientContextType::App { app_dir });
let client_environment = get_client_environment(browserslist_query);
let client_chunking_context =
get_client_chunking_context(project_path, server_root, client_environment, ty);
let client_module_options_context = get_client_module_options_context(
@ -172,7 +171,7 @@ fn app_context(
server_root: FileSystemPathVc,
app_dir: FileSystemPathVc,
env: ProcessEnvVc,
browserslist_query: &str,
client_environment: EnvironmentVc,
ssr: bool,
next_config: NextConfigVc,
server_addr: ServerAddrVc,
@ -204,7 +203,7 @@ fn app_context(
server_root,
app_dir,
env,
browserslist_query,
client_environment,
next_config,
),
);
@ -216,7 +215,7 @@ fn app_context(
execution_context,
client_ty,
server_root,
browserslist_query,
client_environment,
next_config,
)
.into(),
@ -270,7 +269,10 @@ pub async fn create_app_source(
src_app
} else {
return Ok(NoContentSourceVc::new().into());
};
}
.resolve()
.await?;
let client_environment = get_client_environment(browserslist_query);
let context_ssr = app_context(
project_path,
@ -278,7 +280,7 @@ pub async fn create_app_source(
server_root,
app_dir,
env,
browserslist_query,
client_environment,
true,
next_config,
server_addr,
@ -289,7 +291,7 @@ pub async fn create_app_source(
server_root,
app_dir,
env,
browserslist_query,
client_environment,
false,
next_config,
server_addr,
@ -306,7 +308,7 @@ pub async fn create_app_source(
execution_context,
server_root,
env,
browserslist_query,
client_environment,
next_config,
);

View file

@ -10,6 +10,7 @@ use turbopack::{
use turbopack_core::{
chunk::ChunkGroupVc,
context::AssetContextVc,
environment::EnvironmentVc,
resolve::{options::ImportMap, origin::PlainResolveOriginVc},
};
use turbopack_dev_server::html::DevHtmlAssetVc;
@ -17,7 +18,7 @@ use turbopack_node::execution_context::ExecutionContextVc;
use crate::{
next_client::context::{
get_client_chunking_context, get_client_environment, get_client_module_options_context,
get_client_chunking_context, get_client_module_options_context,
get_client_resolve_options_context, get_client_runtime_entries, ClientContextType,
},
next_config::NextConfigVc,
@ -31,21 +32,20 @@ pub async fn get_fallback_page(
execution_context: ExecutionContextVc,
dev_server_root: FileSystemPathVc,
env: ProcessEnvVc,
browserslist_query: &str,
client_environment: EnvironmentVc,
next_config: NextConfigVc,
) -> Result<DevHtmlAssetVc> {
let ty = Value::new(ClientContextType::Fallback);
let environment = get_client_environment(browserslist_query);
let resolve_options_context = get_client_resolve_options_context(project_path, ty, next_config);
let module_options_context = get_client_module_options_context(
project_path,
execution_context,
environment,
client_environment,
ty,
next_config,
);
let chunking_context =
get_client_chunking_context(project_path, dev_server_root, environment, ty);
get_client_chunking_context(project_path, dev_server_root, client_environment, ty);
let entries = get_client_runtime_entries(project_path, env, ty, next_config);
let mut import_map = ImportMap::empty();
@ -60,7 +60,7 @@ pub async fn get_fallback_page(
let context: AssetContextVc = ModuleAssetContextVc::new(
TransitionsByNameVc::cell(HashMap::new()),
environment,
client_environment,
module_options_context,
resolve_options_context.with_extended_import_map(import_map.cell()),
)

View file

@ -14,7 +14,7 @@ use turbopack_node::execution_context::ExecutionContextVc;
use super::with_chunks::WithChunksAsset;
use crate::{
next_client::context::{
get_client_chunking_context, get_client_environment, get_client_module_options_context,
get_client_chunking_context, get_client_module_options_context,
get_client_resolve_options_context, ClientContextType,
},
next_config::NextConfigVc,
@ -37,10 +37,9 @@ impl NextClientChunksTransitionVc {
execution_context: ExecutionContextVc,
ty: Value<ClientContextType>,
server_root: FileSystemPathVc,
browserslist_query: &str,
client_environment: EnvironmentVc,
next_config: NextConfigVc,
) -> NextClientChunksTransitionVc {
let client_environment = get_client_environment(browserslist_query);
let client_chunking_context =
get_client_chunking_context(project_path, server_root, client_environment, ty);

View file

@ -1,5 +1,3 @@
use std::collections::HashMap;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use turbo_tasks::{
@ -97,7 +95,9 @@ pub async fn create_page_source(
src_pages
} else {
return Ok(NoContentSourceVc::new().into());
};
}
.resolve()
.await?;
let client_ty = Value::new(ClientContextType::Pages { pages_dir });
let server_ty = Value::new(ServerContextType::Pages { pages_dir });
@ -113,13 +113,6 @@ pub async fn create_page_source(
);
let client_resolve_options_context =
get_client_resolve_options_context(project_path, client_ty, next_config);
let client_context: AssetContextVc = ModuleAssetContextVc::new(
TransitionsByNameVc::cell(HashMap::new()),
client_environment,
client_module_options_context,
client_resolve_options_context,
)
.into();
let client_chunking_context =
get_client_chunking_context(project_path, server_root, client_environment, client_ty);
@ -171,7 +164,15 @@ pub async fn create_page_source(
let server_module_options_context =
get_server_module_options_context(project_path, execution_context, server_ty, next_config);
let server_transitions = TransitionsByNameVc::cell(
let server_data_module_options_context = get_server_module_options_context(
project_path,
execution_context,
server_data_ty,
next_config,
);
let transitions = TransitionsByNameVc::cell(
[
("next-edge".to_string(), next_edge_transition),
("next-client".to_string(), next_client_transition),
@ -182,7 +183,7 @@ pub async fn create_page_source(
execution_context,
client_ty,
server_root,
browserslist_query,
client_environment,
next_config,
)
.into(),
@ -192,23 +193,22 @@ pub async fn create_page_source(
.collect(),
);
let client_context: AssetContextVc = ModuleAssetContextVc::new(
transitions,
client_environment,
client_module_options_context,
client_resolve_options_context,
)
.into();
let server_context: AssetContextVc = ModuleAssetContextVc::new(
server_transitions,
transitions,
server_environment,
server_module_options_context,
server_resolve_options_context,
)
.into();
let server_data_module_options_context = get_server_module_options_context(
project_path,
execution_context,
server_data_ty,
next_config,
);
let server_data_context: AssetContextVc = ModuleAssetContextVc::new(
TransitionsByNameVc::cell(HashMap::new()),
transitions,
server_environment,
server_data_module_options_context,
server_resolve_options_context,
@ -226,7 +226,7 @@ pub async fn create_page_source(
execution_context,
server_root,
env,
browserslist_query,
client_environment,
next_config,
);