Skip to content

Commit

Permalink
route/middleware/instrumentation use server assets
Browse files Browse the repository at this point in the history
server assets use full url rewrite
  • Loading branch information
sokra committed Feb 16, 2024
1 parent 12639c4 commit 206a357
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 52 deletions.
90 changes: 79 additions & 11 deletions packages/next-swc/crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ impl AppProject {
}
}

fn route_ty(self: Vc<Self>) -> ServerContextType {
ServerContextType::AppRoute {
app_dir: self.app_dir(),
}
}

fn ssr_ty(self: Vc<Self>) -> ServerContextType {
ServerContextType::AppSSR {
app_dir: self.app_dir(),
Expand Down Expand Up @@ -174,6 +180,17 @@ impl AppProject {
))
}

#[turbo_tasks::function]
async fn route_module_options_context(self: Vc<Self>) -> Result<Vc<ModuleOptionsContext>> {
Ok(get_server_module_options_context(
self.project().project_path(),
self.project().execution_context(),
Value::new(self.route_ty()),
self.project().next_mode(),
self.project().next_config(),
))
}

#[turbo_tasks::function]
async fn rsc_resolve_options_context(self: Vc<Self>) -> Result<Vc<ResolveOptionsContext>> {
Ok(get_server_resolve_options_context(
Expand All @@ -196,6 +213,30 @@ impl AppProject {
))
}

#[turbo_tasks::function]
async fn route_resolve_options_context(self: Vc<Self>) -> Result<Vc<ResolveOptionsContext>> {
Ok(get_server_resolve_options_context(
self.project().project_path(),
Value::new(self.route_ty()),
self.project().next_mode(),
self.project().next_config(),
self.project().execution_context(),
))
}

#[turbo_tasks::function]
async fn edge_route_resolve_options_context(
self: Vc<Self>,
) -> Result<Vc<ResolveOptionsContext>> {
Ok(get_edge_resolve_options_context(
self.project().project_path(),
Value::new(self.route_ty()),
self.project().next_mode(),
self.project().next_config(),
self.project().execution_context(),
))
}

#[turbo_tasks::function]
fn rsc_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> {
let transitions = [
Expand Down Expand Up @@ -253,6 +294,28 @@ impl AppProject {
)
}

#[turbo_tasks::function]
fn route_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> {
ModuleAssetContext::new(
Default::default(),
self.project().server_compile_time_info(),
self.route_module_options_context(),
self.route_resolve_options_context(),
Vc::cell("app-route".to_string()),
)
}

#[turbo_tasks::function]
fn edge_route_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> {
ModuleAssetContext::new(
Default::default(),
self.project().edge_compile_time_info(),
self.route_module_options_context(),
self.edge_route_resolve_options_context(),
Vc::cell("app-edge-route".to_string()),
)
}

#[turbo_tasks::function]
fn client_module_context(self: Vc<Self>) -> Vc<ModuleAssetContext> {
ModuleAssetContext::new(
Expand Down Expand Up @@ -476,8 +539,8 @@ impl AppEndpoint {
#[turbo_tasks::function]
fn app_route_entry(&self, path: Vc<FileSystemPath>) -> Vc<AppEntry> {
get_app_route_entry(
self.app_project.rsc_module_context(),
self.app_project.edge_rsc_module_context(),
self.app_project.route_module_context(),
self.app_project.edge_route_module_context(),
Vc::upcast(FileSource::new(path)),
self.page.clone(),
self.app_project.project().project_path(),
Expand Down Expand Up @@ -596,9 +659,9 @@ impl AppEndpoint {
let ssr_chunking_context = if process_ssr {
Some(match runtime {
NextRuntime::NodeJs => {
Vc::upcast(this.app_project.project().server_chunking_context())
Vc::upcast(this.app_project.project().server_chunking_context(true))
}
NextRuntime::Edge => this.app_project.project().edge_chunking_context(),
NextRuntime::Edge => this.app_project.project().edge_chunking_context(true),
})
} else {
None
Expand Down Expand Up @@ -824,7 +887,10 @@ impl AppEndpoint {
let endpoint_output = match runtime {
NextRuntime::Edge => {
// create edge chunks
let chunking_context = this.app_project.project().edge_chunking_context();
let chunking_context = this
.app_project
.project()
.edge_chunking_context(process_client);
let mut evaluatable_assets = this
.app_project
.edge_rsc_runtime_entries()
Expand Down Expand Up @@ -971,6 +1037,11 @@ impl AppEndpoint {
let mut evaluatable_assets =
this.app_project.rsc_runtime_entries().await?.clone_value();

let chunking_context = this
.app_project
.project()
.server_chunking_context(process_client);

if let Some(app_server_reference_modules) = app_server_reference_modules {
let (loader, manifest) = create_server_actions_manifest(
Vc::upcast(app_entry.rsc_entry),
Expand All @@ -981,7 +1052,7 @@ impl AppEndpoint {
&app_entry.original_name,
NextRuntime::NodeJs,
Vc::upcast(this.app_project.rsc_module_context()),
Vc::upcast(this.app_project.project().server_chunking_context()),
Vc::upcast(chunking_context),
)
.await?;
server_assets.push(manifest);
Expand All @@ -990,10 +1061,7 @@ impl AppEndpoint {

let EntryChunkGroupResult {
asset: rsc_chunk, ..
} = *this
.app_project
.project()
.server_chunking_context()
} = *chunking_context
.entry_chunk_group(
server_path.join(format!(
"app{original_name}.js",
Expand Down Expand Up @@ -1024,7 +1092,7 @@ impl AppEndpoint {
let dynamic_import_modules =
collect_next_dynamic_imports(app_entry.rsc_entry).await?;
let dynamic_import_entries = collect_chunk_group(
this.app_project.project().server_chunking_context(),
chunking_context,
dynamic_import_modules,
availability_info,
)
Expand Down
12 changes: 3 additions & 9 deletions packages/next-swc/crates/next-api/src/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use next_core::{
all_assets_from_entries,
next_edge::entry::wrap_edge_entry,
next_manifests::{InstrumentationDefinition, MiddlewaresManifestV2},
next_server::{get_server_chunking_context, get_server_runtime_entries, ServerContextType},
next_server::{get_server_runtime_entries, ServerContextType},
};
use tracing::Instrument;
use turbo_tasks::{Completion, Value, Vc};
Expand Down Expand Up @@ -94,7 +94,7 @@ impl InstrumentationEndpoint {
};
evaluatable_assets.push(evaluatable);

let edge_chunking_context = self.project.edge_chunking_context();
let edge_chunking_context = self.project.edge_chunking_context(false);

let edge_files = edge_chunking_context.evaluated_chunk_group_assets(
module.ident(),
Expand All @@ -107,13 +107,7 @@ impl InstrumentationEndpoint {

#[turbo_tasks::function]
async fn node_chunk(&self) -> Result<Vc<Box<dyn OutputAsset>>> {
let chunking_context = get_server_chunking_context(
self.project.project_path(),
self.project.node_root(),
self.project.client_relative_path(),
self.project.next_config().computed_asset_prefix(),
self.project.server_compile_time_info().environment(),
);
let chunking_context = self.project.server_chunking_context(false);

let userland_module = self
.context
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/next-api/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl MiddlewareEndpoint {
.context("Entry module must be evaluatable")?;
evaluatable_assets.push(evaluatable);

let edge_chunking_context = self.project.edge_chunking_context();
let edge_chunking_context = self.project.edge_chunking_context(false);

let edge_files = edge_chunking_context.evaluated_chunk_group_assets(
module.ident(),
Expand Down
12 changes: 6 additions & 6 deletions packages/next-swc/crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,8 @@ impl PageEndpoint {
this.pages_project.project().project_path(),
this.pages_project.ssr_module_context(),
this.pages_project.edge_ssr_module_context(),
this.pages_project.project().server_chunking_context(),
this.pages_project.project().edge_chunking_context(),
this.pages_project.project().server_chunking_context(true),
this.pages_project.project().edge_chunking_context(true),
this.pages_project.ssr_runtime_entries(),
this.pages_project.edge_ssr_runtime_entries(),
))
Expand All @@ -784,8 +784,8 @@ impl PageEndpoint {
this.pages_project.project().project_path(),
this.pages_project.ssr_data_module_context(),
this.pages_project.edge_ssr_data_module_context(),
this.pages_project.project().server_chunking_context(),
this.pages_project.project().edge_chunking_context(),
this.pages_project.project().server_chunking_context(true),
this.pages_project.project().edge_chunking_context(true),
this.pages_project.ssr_data_runtime_entries(),
this.pages_project.edge_ssr_data_runtime_entries(),
))
Expand All @@ -803,8 +803,8 @@ impl PageEndpoint {
this.pages_project.project().project_path(),
this.pages_project.api_module_context(),
this.pages_project.edge_api_module_context(),
this.pages_project.project().server_chunking_context(),
this.pages_project.project().edge_chunking_context(),
this.pages_project.project().server_chunking_context(false),
this.pages_project.project().edge_chunking_context(false),
this.pages_project.ssr_runtime_entries(),
this.pages_project.edge_ssr_runtime_entries(),
))
Expand Down
62 changes: 43 additions & 19 deletions packages/next-swc/crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ use indexmap::{map::Entry, IndexMap};
use next_core::{
all_assets_from_entries,
app_structure::find_app_dir,
emit_assets, get_edge_chunking_context, get_edge_compile_time_info,
emit_assets, get_edge_chunking_context_with_client_assets, get_edge_compile_time_info,
get_edge_resolve_options_context,
instrumentation::instrumentation_files,
middleware::middleware_files,
mode::NextMode,
next_client::{get_client_chunking_context, get_client_compile_time_info},
next_config::{JsConfig, NextConfig},
next_edge::context::get_edge_chunking_context,
next_server::{
get_server_chunking_context, get_server_compile_time_info,
get_server_module_options_context, get_server_resolve_options_context, ServerContextType,
get_server_chunking_context, get_server_chunking_context_with_client_assets,
get_server_compile_time_info, get_server_module_options_context,
get_server_resolve_options_context, ServerContextType,
},
next_telemetry::NextFeatureTelemetry,
};
Expand Down Expand Up @@ -519,25 +521,47 @@ impl Project {
}

#[turbo_tasks::function]
pub(super) fn server_chunking_context(self: Vc<Self>) -> Vc<BuildChunkingContext> {
get_server_chunking_context(
self.project_path(),
self.node_root(),
self.client_relative_path(),
self.next_config().computed_asset_prefix(),
self.server_compile_time_info().environment(),
)
pub(super) fn server_chunking_context(
self: Vc<Self>,
client_assets: bool,
) -> Vc<BuildChunkingContext> {
if client_assets {
get_server_chunking_context_with_client_assets(
self.project_path(),
self.node_root(),
self.client_relative_path(),
self.next_config().computed_asset_prefix(),
self.server_compile_time_info().environment(),
)
} else {
get_server_chunking_context(
self.project_path(),
self.node_root(),
self.server_compile_time_info().environment(),
)
}
}

#[turbo_tasks::function]
pub(super) fn edge_chunking_context(self: Vc<Self>) -> Vc<Box<dyn EcmascriptChunkingContext>> {
get_edge_chunking_context(
self.project_path(),
self.node_root(),
self.client_relative_path(),
self.next_config().computed_asset_prefix(),
self.edge_compile_time_info().environment(),
)
pub(super) fn edge_chunking_context(
self: Vc<Self>,
client_assets: bool,
) -> Vc<Box<dyn EcmascriptChunkingContext>> {
if client_assets {
get_edge_chunking_context_with_client_assets(
self.project_path(),
self.node_root(),
self.client_relative_path(),
self.next_config().computed_asset_prefix(),
self.edge_compile_time_info().environment(),
)
} else {
get_edge_chunking_context(
self.project_path(),
self.node_root(),
self.edge_compile_time_info().environment(),
)
}
}

/// Emit a telemetry event corresponding to [webpack configuration telemetry](https://github.com/vercel/next.js/blob/9da305fe320b89ee2f8c3cfb7ecbf48856368913/packages/next/src/build/webpack-config.ts#L2516)
Expand Down
3 changes: 2 additions & 1 deletion packages/next-swc/crates/next-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ pub use app_segment_config::{
};
pub use emit::{all_assets_from_entries, emit_all_assets, emit_assets, emit_client_assets};
pub use next_edge::context::{
get_edge_chunking_context, get_edge_compile_time_info, get_edge_resolve_options_context,
get_edge_chunking_context_with_client_assets, get_edge_compile_time_info,
get_edge_resolve_options_context,
};
pub use page_loader::{create_page_loader_entry_module, PageLoaderAsset};
use turbopack_binding::{turbo, turbopack};
Expand Down
23 changes: 22 additions & 1 deletion packages/next-swc/crates/next-core/src/next_edge/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub async fn get_edge_resolve_options_context(
}

#[turbo_tasks::function]
pub fn get_edge_chunking_context(
pub fn get_edge_chunking_context_with_client_assets(
project_path: Vc<FileSystemPath>,
node_root: Vc<FileSystemPath>,
client_root: Vc<FileSystemPath>,
Expand All @@ -172,3 +172,24 @@ pub fn get_edge_chunking_context(
.build(),
)
}

#[turbo_tasks::function]
pub fn get_edge_chunking_context(
project_path: Vc<FileSystemPath>,
node_root: Vc<FileSystemPath>,
environment: Vc<Environment>,
) -> Vc<Box<dyn EcmascriptChunkingContext>> {
let output_root = node_root.join("server/edge".to_string());
Vc::upcast(
DevChunkingContext::builder(
project_path,
output_root,
output_root,
output_root.join("chunks".to_string()),
output_root.join("assets".to_string()),
environment,
)
.reference_chunk_source_maps(should_debug("edge"))
.build(),
)
}
Loading

0 comments on commit 206a357

Please sign in to comment.