Unverified Commit 2cd0c8ab authored by Alex Kirszenberg's avatar Alex Kirszenberg Committed by GitHub

Add app, error, and document entrypoints (#53013)

### What

adds endpoints for `_app` `_error` and `_document` for pages
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent c8eb7f3d
...@@ -207,6 +207,9 @@ impl NapiMiddleware { ...@@ -207,6 +207,9 @@ impl NapiMiddleware {
struct NapiEntrypoints { struct NapiEntrypoints {
pub routes: Vec<NapiRoute>, pub routes: Vec<NapiRoute>,
pub middleware: Option<NapiMiddleware>, pub middleware: Option<NapiMiddleware>,
pub pages_document_endpoint: External<ExternalEndpoint>,
pub pages_app_endpoint: External<ExternalEndpoint>,
pub pages_error_endpoint: External<ExternalEndpoint>,
pub issues: Vec<NapiIssue>, pub issues: Vec<NapiIssue>,
pub diagnostics: Vec<NapiDiagnostic>, pub diagnostics: Vec<NapiDiagnostic>,
} }
...@@ -245,6 +248,18 @@ pub fn project_entrypoints_subscribe( ...@@ -245,6 +248,18 @@ pub fn project_entrypoints_subscribe(
.as_ref() .as_ref()
.map(|m| NapiMiddleware::from_middleware(m, &turbo_tasks)) .map(|m| NapiMiddleware::from_middleware(m, &turbo_tasks))
.transpose()?, .transpose()?,
pages_document_endpoint: External::new(ExternalEndpoint(VcArc::new(
turbo_tasks.clone(),
entrypoints.pages_document_endpoint,
))),
pages_app_endpoint: External::new(ExternalEndpoint(VcArc::new(
turbo_tasks.clone(),
entrypoints.pages_app_endpoint,
))),
pages_error_endpoint: External::new(ExternalEndpoint(VcArc::new(
turbo_tasks.clone(),
entrypoints.pages_error_endpoint,
))),
issues: issues issues: issues
.iter() .iter()
.map(|issue| NapiIssue::from(&**issue)) .map(|issue| NapiIssue::from(&**issue))
......
use indexmap::IndexMap; use indexmap::IndexMap;
use turbo_tasks::Vc;
use crate::{project::Middleware, route::Route}; use crate::{
project::Middleware,
route::{Endpoint, Route},
};
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
pub struct Entrypoints { pub struct Entrypoints {
pub routes: IndexMap<String, Route>, pub routes: IndexMap<String, Route>,
pub middleware: Option<Middleware>, pub middleware: Option<Middleware>,
pub pages_document_endpoint: Vc<Box<dyn Endpoint>>,
pub pages_app_endpoint: Vc<Box<dyn Endpoint>>,
pub pages_error_endpoint: Vc<Box<dyn Endpoint>>,
} }
...@@ -66,8 +66,33 @@ impl PagesProject { ...@@ -66,8 +66,33 @@ impl PagesProject {
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn routes(self: Vc<Self>) -> Result<Vc<Routes>> { 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(); 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( async fn add_dir_to_routes(
routes: &mut IndexMap<String, Route>, routes: &mut IndexMap<String, Route>,
dir: Vc<PagesDirectoryStructure>, dir: Vc<PagesDirectoryStructure>,
...@@ -82,16 +107,7 @@ impl PagesProject { ...@@ -82,16 +107,7 @@ impl PagesProject {
project_path: _, project_path: _,
} = *dir.await?; } = *dir.await?;
for &item in items.iter() { for &item in items.iter() {
let PagesStructureItem { add_page_to_routes(routes, item, &make_route).await?;
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);
} }
for &child in children.iter() { for &child in children.iter() {
queue.push(child); queue.push(child);
...@@ -99,6 +115,7 @@ impl PagesProject { ...@@ -99,6 +115,7 @@ impl PagesProject {
} }
Ok(()) Ok(())
} }
if let Some(api) = api { if let Some(api) = api {
add_dir_to_routes(&mut routes, *api, |pathname, original_name, path| { add_dir_to_routes(&mut routes, *api, |pathname, original_name, path| {
Route::PageApi { Route::PageApi {
...@@ -113,30 +130,74 @@ impl PagesProject { ...@@ -113,30 +130,74 @@ impl PagesProject {
}) })
.await?; .await?;
} }
if let Some(page) = pages {
add_dir_to_routes(&mut routes, *page, |pathname, original_name, path| { let make_page_route = |pathname, original_name, path| Route::Page {
Route::Page { html_endpoint: Vc::upcast(PageEndpoint::new(
html_endpoint: Vc::upcast(PageEndpoint::new( PageEndpointType::Html,
PageEndpointType::Html, self,
self, pathname,
pathname, original_name,
original_name, path,
path, )),
)), data_endpoint: Vc::upcast(PageEndpoint::new(
data_endpoint: Vc::upcast(PageEndpoint::new( PageEndpointType::Data,
PageEndpointType::Data, self,
self, pathname,
pathname, original_name,
original_name, path,
path, )),
)), };
}
}) if let Some(pages) = pages {
.await?; add_dir_to_routes(&mut routes, *pages, make_page_route).await?;
} }
Ok(Vc::cell(routes)) Ok(Vc::cell(routes))
} }
#[turbo_tasks::function]
async fn to_endpoint(
self: Vc<Self>,
item: Vc<PagesStructureItem>,
ty: PageEndpointType,
) -> Result<Vc<Box<dyn Endpoint>>> {
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 path = project_path;
let endpoint = Vc::upcast(PageEndpoint::new(
ty,
self,
pathname_vc,
original_name,
path,
));
Ok(endpoint)
}
#[turbo_tasks::function]
pub async fn document_endpoint(self: Vc<Self>) -> Result<Vc<Box<dyn Endpoint>>> {
Ok(self.to_endpoint(
self.pages_structure().await?.document,
PageEndpointType::SsrOnly,
))
}
#[turbo_tasks::function]
pub async fn app_endpoint(self: Vc<Self>) -> Result<Vc<Box<dyn Endpoint>>> {
Ok(self.to_endpoint(self.pages_structure().await?.app, PageEndpointType::Html))
}
#[turbo_tasks::function]
pub async fn error_endpoint(self: Vc<Self>) -> Result<Vc<Box<dyn Endpoint>>> {
Ok(self.to_endpoint(self.pages_structure().await?.error, PageEndpointType::Html))
}
#[turbo_tasks::function] #[turbo_tasks::function]
fn project(&self) -> Vc<Project> { fn project(&self) -> Vc<Project> {
self.project self.project
...@@ -407,6 +468,7 @@ enum PageEndpointType { ...@@ -407,6 +468,7 @@ enum PageEndpointType {
Api, Api,
Html, Html,
Data, Data,
SsrOnly,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -665,6 +727,7 @@ impl PageEndpoint { ...@@ -665,6 +727,7 @@ impl PageEndpoint {
} }
PageEndpointType::Data => self.ssr_data_chunk(), PageEndpointType::Data => self.ssr_data_chunk(),
PageEndpointType::Api => self.api_chunk(), PageEndpointType::Api => self.api_chunk(),
PageEndpointType::SsrOnly => self.ssr_chunk(),
}; };
let page_output = match *ssr_chunk.await? { let page_output = match *ssr_chunk.await? {
......
...@@ -373,6 +373,9 @@ impl Project { ...@@ -373,6 +373,9 @@ impl Project {
Ok(Entrypoints { Ok(Entrypoints {
routes, routes,
middleware: None, middleware: None,
pages_document_endpoint: self.pages_project().document_endpoint(),
pages_app_endpoint: self.pages_project().app_endpoint(),
pages_error_endpoint: self.pages_project().error_endpoint(),
} }
.cell()) .cell())
} }
......
...@@ -393,6 +393,9 @@ interface Middleware { ...@@ -393,6 +393,9 @@ interface Middleware {
interface Entrypoints { interface Entrypoints {
routes: Map<string, Route> routes: Map<string, Route>
middleware?: Middleware middleware?: Middleware
pagesDocumentEndpoint: Endpoint
pagesAppEndpoint: Endpoint
pagesErrorEndpoint: Endpoint
} }
interface Project { interface Project {
...@@ -587,6 +590,9 @@ function bindingToApi(binding: any, _wasm: boolean) { ...@@ -587,6 +590,9 @@ function bindingToApi(binding: any, _wasm: boolean) {
type NapiEntrypoints = { type NapiEntrypoints = {
routes: NapiRoute[] routes: NapiRoute[]
middleware?: NapiMiddleware middleware?: NapiMiddleware
pagesDocumentEndpoint: NapiEndpoint
pagesAppEndpoint: NapiEndpoint
pagesErrorEndpoint: NapiEndpoint
issues: Issue[] issues: Issue[]
diagnostics: Diagnostics[] diagnostics: Diagnostics[]
} }
...@@ -683,6 +689,13 @@ function bindingToApi(binding: any, _wasm: boolean) { ...@@ -683,6 +689,13 @@ function bindingToApi(binding: any, _wasm: boolean) {
yield { yield {
routes, routes,
middleware, middleware,
pagesDocumentEndpoint: new EndpointImpl(
entrypoints.pagesDocumentEndpoint
),
pagesAppEndpoint: new EndpointImpl(entrypoints.pagesAppEndpoint),
pagesErrorEndpoint: new EndpointImpl(
entrypoints.pagesErrorEndpoint
),
issues: entrypoints.issues, issues: entrypoints.issues,
diagnostics: entrypoints.diagnostics, diagnostics: entrypoints.diagnostics,
} }
......
...@@ -240,20 +240,29 @@ const nextDev: CliCommand = async (argv) => { ...@@ -240,20 +240,29 @@ const nextDev: CliCommand = async (argv) => {
// Just testing code here: // Just testing code here:
const project = await bindings.turbo.createProject({ const options = {
projectPath: dir, projectPath: dir,
rootPath: dir, rootPath: args['--root'] ?? findRootDir(dir) ?? dir,
nextConfig: config, nextConfig: config,
env: { env: {
NEXT_PUBLIC_ENV_VAR: 'world', NEXT_PUBLIC_ENV_VAR: 'world',
}, },
watch: true, watch: true,
}) }
const project = await bindings.turbo.createProject(options)
const iter = project.entrypointsSubscribe() const iter = project.entrypointsSubscribe()
try { try {
for await (const entrypoints of iter) { for await (const entrypoints of iter) {
Log.info(entrypoints) Log.info(entrypoints)
Log.info(`writing _document to disk`)
Log.info(await entrypoints.pagesDocumentEndpoint.writeToDisk())
Log.info(`writing _app to disk`)
Log.info(await entrypoints.pagesAppEndpoint.writeToDisk())
Log.info(`writing _error to disk`)
Log.info(await entrypoints.pagesErrorEndpoint.writeToDisk())
for (const [pathname, route] of entrypoints.routes) { for (const [pathname, route] of entrypoints.routes) {
switch (route.type) { switch (route.type) {
case 'page': { case 'page': {
...@@ -287,13 +296,10 @@ const nextDev: CliCommand = async (argv) => { ...@@ -287,13 +296,10 @@ const nextDev: CliCommand = async (argv) => {
} }
Log.info('iteration done') Log.info('iteration done')
await project.update({ await project.update({
projectPath: dir, ...options,
rootPath: dir,
nextConfig: config,
env: { env: {
NEXT_PUBLIC_ENV_VAR: 'hello', NEXT_PUBLIC_ENV_VAR: 'hello',
}, },
watch: true,
}) })
} }
} catch (e) { } catch (e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment