Skip to content

Commit

Permalink
resolve endpoints to avoid extra nesting in tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Feb 21, 2024
1 parent 2f961d3 commit 2c2cd80
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/next-swc/crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ impl AppProject {
}

#[turbo_tasks::function]
pub async fn app_entry_point_to_route(
pub fn app_entry_point_to_route(
app_project: Vc<AppProject>,
entrypoint: AppEntrypoint,
) -> Vc<Route> {
Expand Down
4 changes: 4 additions & 0 deletions packages/next-swc/crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ impl PagesProject {
add_dir_to_routes(&mut routes, *pages, make_page_route).await?;
}

for route in routes.values_mut() {
route.resolve().await?;
}

Ok(Vc::cell(routes))
}

Expand Down
41 changes: 41 additions & 0 deletions packages/next-swc/crates/next-api/src/route.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use turbo_tasks::{debug::ValueDebugFormat, trace::TraceRawVcs, Completion, Vc};
Expand All @@ -11,6 +12,19 @@ pub struct AppPageRoute {
pub rsc_endpoint: Vc<Box<dyn Endpoint>>,
}

impl AppPageRoute {
async fn resolve(&mut self) -> Result<()> {
let Self {
html_endpoint,
rsc_endpoint,
..
} = self;
*html_endpoint = html_endpoint.resolve().await?;
*rsc_endpoint = rsc_endpoint.resolve().await?;
Ok(())
}
}

#[turbo_tasks::value(shared)]
#[derive(Clone, Debug)]
pub enum Route {
Expand All @@ -29,6 +43,33 @@ pub enum Route {
Conflict,
}

impl Route {
pub async fn resolve(&mut self) -> Result<()> {
match self {
Route::Page {
html_endpoint,
data_endpoint,
} => {
*html_endpoint = html_endpoint.resolve().await?;
*data_endpoint = data_endpoint.resolve().await?;
}
Route::PageApi { endpoint } => {
*endpoint = endpoint.resolve().await?;
}
Route::AppPage(routes) => {
for route in routes {
route.resolve().await?;
}
}
Route::AppRoute { endpoint, .. } => {
*endpoint = endpoint.resolve().await?;
}
Route::Conflict => {}
}
Ok(())
}
}

#[turbo_tasks::value_trait]
pub trait Endpoint {
fn write_to_disk(self: Vc<Self>) -> Vc<WrittenEndpoint>;
Expand Down

0 comments on commit 2c2cd80

Please sign in to comment.