Skip to content

Commit

Permalink
Add app, error, and document entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkirsz committed Jul 21, 2023
1 parent e887772 commit 8c14d3e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 31 deletions.
6 changes: 6 additions & 0 deletions packages/next-swc/crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ impl NapiRoute {
data_endpoint: convert_endpoint(data_endpoint),
..Default::default()
},
Route::PageSsr { endpoint } => NapiRoute {
pathname,
r#type: "page-ssr",
endpoint: convert_endpoint(endpoint),
..Default::default()
},
Route::PageApi { endpoint } => NapiRoute {
pathname,
r#type: "page-api",
Expand Down
98 changes: 67 additions & 31 deletions packages/next-swc/crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,33 @@ impl PagesProject {

#[turbo_tasks::function]
pub async fn routes(self: Vc<Self>) -> Result<Vc<Routes>> {
let PagesStructure { api, pages, .. } = &*self.pages_structure().await?;
let PagesStructure {
api,
pages,
app,
document,
error,
} = &*self.pages_structure().await?;
let mut routes = IndexMap::new();

async fn add_page_to_routes(
routes: &mut IndexMap<String, Route>,
page: Vc<PagesStructureItem>,
make_route: impl Fn(Vc<String>, Vc<String>, Vc<FileSystemPath>) -> Route,
) -> Result<()> {
let PagesStructureItem {
next_router_path,
project_path,
original_path,
} = *page.await?;
let pathname = format!("/{}", next_router_path.await?.path);
let pathname_vc = Vc::cell(pathname.clone());
let original_name = Vc::cell(format!("/{}", original_path.await?.path));
let route = make_route(pathname_vc, original_name, project_path);
routes.insert(pathname, route);
Ok(())
}

async fn add_dir_to_routes(
routes: &mut IndexMap<String, Route>,
dir: Vc<PagesDirectoryStructure>,
Expand All @@ -82,23 +107,15 @@ impl PagesProject {
project_path: _,
} = *dir.await?;
for &item in items.iter() {
let PagesStructureItem {
next_router_path,
project_path,
original_path,
} = *item.await?;
let pathname = format!("/{}", next_router_path.await?.path);
let pathname_vc = Vc::cell(pathname.clone());
let original_name = Vc::cell(format!("/{}", original_path.await?.path));
let route = make_route(pathname_vc, original_name, project_path);
routes.insert(pathname, route);
add_page_to_routes(routes, item, &make_route).await?;
}
for &child in children.iter() {
queue.push(child);
}
}
Ok(())
}

if let Some(api) = api {
add_dir_to_routes(&mut routes, *api, |pathname, original_name, path| {
Route::PageApi {
Expand All @@ -113,27 +130,44 @@ impl PagesProject {
})
.await?;
}
if let Some(page) = pages {
add_dir_to_routes(&mut routes, *page, |pathname, original_name, path| {
Route::Page {
html_endpoint: Vc::upcast(PageEndpoint::new(
PageEndpointType::Html,
self,
pathname,
original_name,
path,
)),
data_endpoint: Vc::upcast(PageEndpoint::new(
PageEndpointType::Data,
self,
pathname,
original_name,
path,
)),
}
})
.await?;

let make_page_route = |pathname, original_name, path| Route::Page {
html_endpoint: Vc::upcast(PageEndpoint::new(
PageEndpointType::Html,
self,
pathname,
original_name,
path,
)),
data_endpoint: Vc::upcast(PageEndpoint::new(
PageEndpointType::Data,
self,
pathname,
original_name,
path,
)),
};

if let Some(pages) = pages {
add_dir_to_routes(&mut routes, *pages, make_page_route).await?;
}

// The document route only makes sense in a SSR setting.
add_page_to_routes(&mut routes, *document, |pathname, original_name, path| {
Route::PageSsr {
endpoint: Vc::upcast(PageEndpoint::new(
PageEndpointType::SsrOnly,
self,
pathname,
original_name,
path,
)),
}
})
.await?;
add_page_to_routes(&mut routes, *app, make_page_route).await?;
add_page_to_routes(&mut routes, *error, make_page_route).await?;

Ok(Vc::cell(routes))
}

Expand Down Expand Up @@ -407,6 +441,7 @@ enum PageEndpointType {
Api,
Html,
Data,
SsrOnly,
}

#[turbo_tasks::value_impl]
Expand Down Expand Up @@ -665,6 +700,7 @@ impl PageEndpoint {
}
PageEndpointType::Data => self.ssr_data_chunk(),
PageEndpointType::Api => self.api_chunk(),
PageEndpointType::SsrOnly => self.ssr_chunk(),
};

let page_output = match *ssr_chunk.await? {
Expand Down
3 changes: 3 additions & 0 deletions packages/next-swc/crates/next-api/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pub enum Route {
PageApi {
endpoint: Vc<Box<dyn Endpoint>>,
},
PageSsr {
endpoint: Vc<Box<dyn Endpoint>>,
},
AppPage {
html_endpoint: Vc<Box<dyn Endpoint>>,
rsc_endpoint: Vc<Box<dyn Endpoint>>,
Expand Down
14 changes: 14 additions & 0 deletions packages/next/src/build/swc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ type Route =
type: 'page-api'
endpoint: Endpoint
}
| {
type: 'page-ssr'
endpoint: Endpoint
}

interface Endpoint {
/** Write files for the endpoint to disk. */
Expand Down Expand Up @@ -609,6 +613,10 @@ function bindingToApi(binding: any, _wasm: boolean) {
type: 'page-api'
endpoint: NapiEndpoint
}
| {
type: 'page-ssr'
endpoint: NapiEndpoint
}
| {
type: 'app-page'
htmlEndpoint: NapiEndpoint
Expand Down Expand Up @@ -645,6 +653,12 @@ function bindingToApi(binding: any, _wasm: boolean) {
endpoint: new EndpointImpl(nativeRoute.endpoint),
}
break
case 'page-ssr':
route = {
type: 'page-ssr',
endpoint: new EndpointImpl(nativeRoute.endpoint),
}
break
case 'app-page':
route = {
type: 'app-page',
Expand Down
6 changes: 6 additions & 0 deletions packages/next/src/cli/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ const nextDev: CliCommand = async (argv) => {
Log.info(written)
break
}
case 'page-ssr': {
Log.info(`writing ${pathname} to disk`)
const written = await route.endpoint.writeToDisk()
Log.info(written)
break
}
case 'app-page': {
Log.info(`writing ${pathname} to disk`)
const written = await route.rscEndpoint.writeToDisk()
Expand Down

0 comments on commit 8c14d3e

Please sign in to comment.