Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbopack: find_actions layout segment optimization #73048

Open
wants to merge 9 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 48 additions & 37 deletions crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use next_core::{
},
get_edge_resolve_options_context, get_next_package,
next_app::{
app_client_references_chunks::get_app_server_reference_modules,
get_app_client_references_chunks, get_app_client_shared_chunk_group, get_app_page_entry,
get_app_route_entry, include_modules_module::IncludeModulesModule,
metadata::route::get_app_metadata_route_entry, AppEntry, AppPage,
Expand Down Expand Up @@ -909,7 +908,7 @@ impl AppEndpoint {
};

let (
app_server_reference_modules,
server_action_manifest_loader,
client_dynamic_imports,
client_references,
client_references_chunks,
Expand Down Expand Up @@ -970,7 +969,10 @@ impl AppEndpoint {
.values()
{
let result = collect_next_dynamic_imports(
refs.clone(),
refs.iter()
.map(|r| async move { Ok(Vc::upcast(*r.await?.ssr_module)) })
.try_join()
.await?,
Vc::upcast(this.app_project.client_module_context()),
visited_modules,
)
Expand Down Expand Up @@ -1122,10 +1124,26 @@ impl AppEndpoint {
}
}

let server_action_manifest = create_server_actions_manifest(
*ResolvedVc::upcast(app_entry.rsc_entry),
client_references_cell,
this.app_project.project().project_path(),
node_root,
app_entry.original_name.clone(),
runtime,
match runtime {
NextRuntime::Edge => Vc::upcast(this.app_project.edge_rsc_module_context()),
NextRuntime::NodeJs => Vc::upcast(this.app_project.rsc_module_context()),
},
this.app_project
.project()
.runtime_chunking_context(process_client_assets, runtime),
)
.await?;
server_assets.insert(server_action_manifest.manifest);

(
Some(get_app_server_reference_modules(
client_references_cell.types(),
)),
Some(server_action_manifest.loader),
Some(client_dynamic_imports),
Some(client_references_cell),
Some(client_references_chunks),
Expand All @@ -1134,30 +1152,6 @@ impl AppEndpoint {
(None, None, None, None)
};

let server_action_manifest_loader =
if let Some(app_server_reference_modules) = app_server_reference_modules {
let server_action_manifest = create_server_actions_manifest(
*ResolvedVc::upcast(app_entry.rsc_entry),
app_server_reference_modules,
this.app_project.project().project_path(),
node_root,
app_entry.original_name.clone(),
runtime,
match runtime {
NextRuntime::Edge => Vc::upcast(this.app_project.edge_rsc_module_context()),
NextRuntime::NodeJs => Vc::upcast(this.app_project.rsc_module_context()),
},
this.app_project
.project()
.runtime_chunking_context(process_client_assets, runtime),
)
.await?;
server_assets.insert(server_action_manifest.manifest);
Some(server_action_manifest.loader)
} else {
None
};

let (app_entry_chunks, app_entry_chunks_availability) = &*self
.app_entry_chunks(
client_references,
Expand Down Expand Up @@ -1636,15 +1630,32 @@ impl Endpoint for AppEndpoint {
.project()
.emit_all_output_assets(Vc::cell(output_assets));

let node_root = this.app_project.project().node_root();
let server_paths = all_server_paths(output_assets, node_root)
let (server_paths, client_paths) = if this
.app_project
.project()
.next_mode()
.await?
.clone_value();
.is_development()
{
let node_root = this.app_project.project().node_root();
let server_paths = all_server_paths(output_assets, node_root)
.await?
.clone_value();

let client_relative_root = this.app_project.project().client_relative_path();
let client_paths = all_paths_in_root(output_assets, client_relative_root)
.await?
.clone_value();
let client_relative_root = this.app_project.project().client_relative_path();
let client_paths = async {
anyhow::Ok(
all_paths_in_root(output_assets, client_relative_root)
.await?
.clone_value(),
)
}
.instrument(tracing::info_span!("client_paths"))
.await?;
(server_paths, client_paths)
} else {
(vec![], vec![])
};

let written_endpoint = match *output {
AppEndpointOutput::NodeJs { rsc_chunk, .. } => WrittenEndpoint::NodeJs {
Expand Down
12 changes: 8 additions & 4 deletions crates/next-api/src/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,14 @@ impl Endpoint for InstrumentationEndpoint {
let _ = output_assets.resolve().await?;
let _ = this.project.emit_all_output_assets(Vc::cell(output_assets));

let node_root = this.project.node_root();
let server_paths = all_server_paths(output_assets, node_root)
.await?
.clone_value();
let server_paths = if this.project.next_mode().await?.is_development() {
let node_root = this.project.node_root();
all_server_paths(output_assets, node_root)
.await?
.clone_value()
} else {
vec![]
};

Ok(WrittenEndpoint::Edge {
server_paths,
Expand Down
31 changes: 21 additions & 10 deletions crates/next-api/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,27 @@ impl Endpoint for MiddlewareEndpoint {
let _ = output_assets.resolve().await?;
let _ = this.project.emit_all_output_assets(Vc::cell(output_assets));

let node_root = this.project.node_root();
let server_paths = all_server_paths(output_assets, node_root)
.await?
.clone_value();

// Middleware could in theory have a client path (e.g. `new URL`).
let client_relative_root = this.project.client_relative_path();
let client_paths = all_paths_in_root(output_assets, client_relative_root)
.await?
.clone_value();
let (server_paths, client_paths) = if this.project.next_mode().await?.is_development() {
let node_root = this.project.node_root();
let server_paths = all_server_paths(output_assets, node_root)
.await?
.clone_value();

// Middleware could in theory have a client path (e.g. `new URL`).
let client_relative_root = this.project.client_relative_path();
let client_paths = async {
anyhow::Ok(
all_paths_in_root(output_assets, client_relative_root)
.await?
.clone_value(),
)
}
.instrument(tracing::info_span!("client_paths"))
.await?;
(server_paths, client_paths)
} else {
(vec![], vec![])
};

Ok(WrittenEndpoint::Edge {
server_paths,
Expand Down
30 changes: 24 additions & 6 deletions crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,14 +1273,32 @@ impl Endpoint for PageEndpoint {
.emit_all_output_assets(Vc::cell(output_assets));

let node_root = this.pages_project.project().node_root();
let server_paths = all_server_paths(output_assets, node_root)
.await?
.clone_value();

let client_relative_root = this.pages_project.project().client_relative_path();
let client_paths = all_paths_in_root(output_assets, client_relative_root)
let (server_paths, client_paths) = if this
.pages_project
.project()
.next_mode()
.await?
.clone_value();
.is_development()
{
let server_paths = all_server_paths(output_assets, node_root)
.await?
.clone_value();

let client_relative_root = this.pages_project.project().client_relative_path();
let client_paths = async {
anyhow::Ok(
all_paths_in_root(output_assets, client_relative_root)
.await?
.clone_value(),
)
}
.instrument(tracing::info_span!("client_paths"))
.await?;
(server_paths, client_paths)
} else {
(vec![], vec![])
};

let node_root = &node_root.await?;
let written_endpoint = match *output {
Expand Down
54 changes: 30 additions & 24 deletions crates/next-api/src/paths.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use next_core::{all_assets_from_entries, next_manifests::AssetBinding};
use serde::{Deserialize, Serialize};
use tracing::Instrument;
use turbo_rcstr::RcStr;
use turbo_tasks::{trace::TraceRawVcs, ResolvedVc, TryFlatJoinIterExt, Vc};
use turbo_tasks_fs::FileSystemPath;
Expand Down Expand Up @@ -29,30 +30,35 @@ pub async fn all_server_paths(
assets: Vc<OutputAssets>,
node_root: Vc<FileSystemPath>,
) -> Result<Vc<ServerPaths>> {
let all_assets = all_assets_from_entries(assets).await?;
let node_root = &node_root.await?;
Ok(Vc::cell(
all_assets
.iter()
.map(|&asset| async move {
Ok(
if let Some(path) = node_root.get_path_to(&*asset.ident().path().await?) {
let content_hash = match *asset.content().await? {
AssetContent::File(file) => *file.hash().await?,
AssetContent::Redirect { .. } => 0,
};
Some(ServerPath {
path: path.to_string(),
content_hash,
})
} else {
None
},
)
})
.try_flat_join()
.await?,
))
let span = tracing::info_span!("all_server_paths");
async move {
let all_assets = all_assets_from_entries(assets).await?;
let node_root = &node_root.await?;
Ok(Vc::cell(
all_assets
.iter()
.map(|&asset| async move {
Ok(
if let Some(path) = node_root.get_path_to(&*asset.ident().path().await?) {
let content_hash = match *asset.content().await? {
AssetContent::File(file) => *file.hash().await?,
AssetContent::Redirect { .. } => 0,
};
Some(ServerPath {
path: path.to_string(),
content_hash,
})
} else {
None
},
)
})
.try_flat_join()
.await?,
))
}
.instrument(span)
.await
}

/// Return a list of relative paths to `root` for all output assets references
Expand Down
Loading
Loading