Unverified Commit 6ff2d98c authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: remove pages router double template (#86842)

Webpack does:
`templates/edge-ssr.js -> user's pages/index.js`

Turbopack does
`templates/edge-ssr.js { INNER_PAGE_ENTRY => \"[project]/packages/next/dist/esm/build/templates/pages.js { INNER_PAGE => \\\"[project]/test/tmp/next-test-1764875742357-479/pages/index.js`



`VAR_USERLAND` here: https://github.com/vercel/next.js/blob/ee3d017e43eb953a62746a0e6110a94dc7a3c980/packages/next/src/build/templates/edge-ssr.ts#L12-L13
for Webpack is actually the user's page file with the `export default Page()`
for Turbopack is actually whatever `templates/pages.js` exports and it relies on this default reexport: https://github.com/vercel/next.js/blob/38ef2360e1e133f329a9061ce17aad8aaab33cb2/packages/next/src/build/templates/pages.ts#L15 while ignoring everything else in there
parent b35713c4
...@@ -890,33 +890,16 @@ impl PageEndpoint { ...@@ -890,33 +890,16 @@ impl PageEndpoint {
runtime: NextRuntime::NodeJs, runtime: NextRuntime::NodeJs,
regions: config.preferred_region.clone(), regions: config.preferred_region.clone(),
} }
} else if runtime == NextRuntime::Edge {
let modules = create_page_ssr_entry_module(
this.pathname.clone(),
reference_type,
project_root,
Vc::upcast(edge_module_context),
self.source(),
this.original_name.clone(),
*this.pages_structure,
runtime,
this.pages_project.project().next_config(),
)
.await?;
InternalSsrChunkModule {
ssr_module: modules.ssr_module,
app_module: modules.app_module,
document_module: modules.document_module,
runtime,
regions: config.preferred_region.clone(),
}
} else { } else {
let modules = create_page_ssr_entry_module( let modules = create_page_ssr_entry_module(
this.pathname.clone(), this.pathname.clone(),
reference_type, reference_type,
project_root, project_root,
Vc::upcast(module_context), if runtime == NextRuntime::Edge {
Vc::upcast(edge_module_context)
} else {
Vc::upcast(module_context)
},
self.source(), self.source(),
this.original_name.clone(), this.original_name.clone(),
*this.pages_structure, *this.pages_structure,
...@@ -924,6 +907,7 @@ impl PageEndpoint { ...@@ -924,6 +907,7 @@ impl PageEndpoint {
this.pages_project.project().next_config(), this.pages_project.project().next_config(),
) )
.await?; .await?;
InternalSsrChunkModule { InternalSsrChunkModule {
ssr_module: modules.ssr_module, ssr_module: modules.ssr_module,
app_module: modules.app_module, app_module: modules.app_module,
......
...@@ -91,9 +91,9 @@ pub async fn get_middleware_module( ...@@ -91,9 +91,9 @@ pub async fn get_middleware_module(
let source = load_next_js_template( let source = load_next_js_template(
"middleware.js", "middleware.js",
project_root, project_root,
&[("VAR_USERLAND", INNER), ("VAR_DEFINITION_PAGE", page_path)], [("VAR_USERLAND", INNER), ("VAR_DEFINITION_PAGE", page_path)],
&[], [],
&[], [],
) )
.await?; .await?;
......
...@@ -76,7 +76,7 @@ pub async fn get_app_page_entry( ...@@ -76,7 +76,7 @@ pub async fn get_app_page_entry(
let source = load_next_js_template( let source = load_next_js_template(
"app-page.js", "app-page.js",
project_root.clone(), project_root.clone(),
&[ [
("VAR_DEFINITION_PAGE", &*page.to_string()), ("VAR_DEFINITION_PAGE", &*page.to_string()),
("VAR_DEFINITION_PATHNAME", &pathname), ("VAR_DEFINITION_PATHNAME", &pathname),
( (
...@@ -88,12 +88,12 @@ pub async fn get_app_page_entry( ...@@ -88,12 +88,12 @@ pub async fn get_app_page_entry(
}, },
), ),
], ],
&[ [
("tree", &*loader_tree_code), ("tree", &*loader_tree_code),
("__next_app_require__", &TURBOPACK_REQUIRE.bound()), ("__next_app_require__", &TURBOPACK_REQUIRE.bound()),
("__next_app_load_chunk__", &TURBOPACK_LOAD.bound()), ("__next_app_load_chunk__", &TURBOPACK_LOAD.bound()),
], ],
&[], [],
) )
.await?; .await?;
...@@ -150,13 +150,13 @@ async fn wrap_edge_page( ...@@ -150,13 +150,13 @@ async fn wrap_edge_page(
let source = load_next_js_template( let source = load_next_js_template(
"edge-ssr-app.js", "edge-ssr-app.js",
project_root.clone(), project_root.clone(),
&[("VAR_USERLAND", INNER), ("VAR_PAGE", &page.to_string())], [("VAR_USERLAND", INNER), ("VAR_PAGE", &page.to_string())],
&[ [
// TODO do we really need to pass the entire next config here? // TODO do we really need to pass the entire next config here?
// This is bad for invalidation as any config change will invalidate this // This is bad for invalidation as any config change will invalidate this
("nextConfig", &*serde_json::to_string(next_config_val)?), ("nextConfig", &*serde_json::to_string(next_config_val)?),
], ],
&[("incrementalCacheHandler", None)], [("incrementalCacheHandler", None)],
) )
.await?; .await?;
......
...@@ -73,7 +73,7 @@ pub async fn get_app_route_entry( ...@@ -73,7 +73,7 @@ pub async fn get_app_route_entry(
let virtual_source = load_next_js_template( let virtual_source = load_next_js_template(
"app-route.js", "app-route.js",
project_root.clone(), project_root.clone(),
&[ [
("VAR_DEFINITION_PAGE", &*page.to_string()), ("VAR_DEFINITION_PAGE", &*page.to_string()),
("VAR_DEFINITION_PATHNAME", &pathname), ("VAR_DEFINITION_PATHNAME", &pathname),
("VAR_DEFINITION_FILENAME", path.file_stem().unwrap()), ("VAR_DEFINITION_FILENAME", path.file_stem().unwrap()),
...@@ -82,8 +82,8 @@ pub async fn get_app_route_entry( ...@@ -82,8 +82,8 @@ pub async fn get_app_route_entry(
("VAR_RESOLVED_PAGE_PATH", &path.value_to_string().await?), ("VAR_RESOLVED_PAGE_PATH", &path.value_to_string().await?),
("VAR_USERLAND", &inner), ("VAR_USERLAND", &inner),
], ],
&[("nextConfigOutput", output_type)], [("nextConfigOutput", output_type)],
&[], [],
) )
.await?; .await?;
...@@ -141,9 +141,9 @@ async fn wrap_edge_route( ...@@ -141,9 +141,9 @@ async fn wrap_edge_route(
let source = load_next_js_template( let source = load_next_js_template(
"edge-app-route.js", "edge-app-route.js",
project_root.clone(), project_root.clone(),
&[("VAR_USERLAND", &*inner), ("VAR_PAGE", &page.to_string())], [("VAR_USERLAND", &*inner), ("VAR_PAGE", &page.to_string())],
&[("nextConfig", &*serde_json::to_string(next_config)?)], [("nextConfig", &*serde_json::to_string(next_config)?)],
&[], [],
) )
.await?; .await?;
......
...@@ -243,12 +243,12 @@ pub enum MiddlewareMatcherKind { ...@@ -243,12 +243,12 @@ pub enum MiddlewareMatcherKind {
/// Loads a next.js template, replaces `replacements` and `injections` and makes /// Loads a next.js template, replaces `replacements` and `injections` and makes
/// sure there are none left over. /// sure there are none left over.
pub async fn load_next_js_template( pub async fn load_next_js_template<'b>(
template_path: &str, template_path: &'b str,
project_path: FileSystemPath, project_path: FileSystemPath,
replacements: &[(&str, &str)], replacements: impl IntoIterator<Item = (&'b str, &'b str)>,
injections: &[(&str, &str)], injections: impl IntoIterator<Item = (&'b str, &'b str)>,
imports: &[(&str, Option<&str>)], imports: impl IntoIterator<Item = (&'b str, Option<&'b str>)>,
) -> Result<Vc<Box<dyn Source>>> { ) -> Result<Vc<Box<dyn Source>>> {
let template_path = virtual_next_js_template_path(project_path.clone(), template_path).await?; let template_path = virtual_next_js_template_path(project_path.clone(), template_path).await?;
...@@ -261,9 +261,9 @@ pub async fn load_next_js_template( ...@@ -261,9 +261,9 @@ pub async fn load_next_js_template(
&content, &content,
&template_path.path, &template_path.path,
&package_root.path, &package_root.path,
replacements.iter().copied(), replacements,
injections.iter().copied(), injections,
imports.iter().copied(), imports,
)?; )?;
let file = File::from(content); let file = File::from(content);
......
...@@ -91,7 +91,7 @@ async function requestHandler( ...@@ -91,7 +91,7 @@ async function requestHandler(
req: NextRequestHint, req: NextRequestHint,
_event: NextFetchEvent _event: NextFetchEvent
): Promise<Response> { ): Promise<Response> {
let srcPage = 'VAR_PAGE' let srcPage = 'VAR_DEFINITION_PATHNAME'
const relativeUrl = `${req.nextUrl.pathname}${req.nextUrl.search}` const relativeUrl = `${req.nextUrl.pathname}${req.nextUrl.search}`
const baseReq = new WebNextRequest(req) const baseReq = new WebNextRequest(req)
...@@ -364,7 +364,7 @@ const handler: EdgeHandler = (opts) => { ...@@ -364,7 +364,7 @@ const handler: EdgeHandler = (opts) => {
handler: requestHandler, handler: requestHandler,
incrementalCacheHandler, incrementalCacheHandler,
bypassNextUrl: true, bypassNextUrl: true,
page: 'VAR_PAGE', page: 'VAR_DEFINITION_PATHNAME',
}) })
} }
export default handler export default handler
...@@ -172,7 +172,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> = ...@@ -172,7 +172,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
'edge-ssr', 'edge-ssr',
{ {
VAR_USERLAND: pageModPath, VAR_USERLAND: pageModPath,
VAR_PAGE: page, VAR_DEFINITION_PATHNAME: page,
VAR_MODULE_DOCUMENT: documentPath, VAR_MODULE_DOCUMENT: documentPath,
VAR_MODULE_APP: appPath, VAR_MODULE_APP: appPath,
VAR_MODULE_GLOBAL_ERROR: errorPath, VAR_MODULE_GLOBAL_ERROR: errorPath,
......
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