Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Separate routing code from render servers (#52492)" #53016

Merged
merged 2 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 4 additions & 142 deletions packages/next-swc/crates/next-build/src/next_pages/page_entries.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::io::Write;

use anyhow::{bail, Result};
use indexmap::indexmap;
use indoc::writedoc;
use next_core::{
create_page_loader_entry_module, get_asset_path_from_pathname,
mode::NextMode,
Expand All @@ -24,23 +20,17 @@ use next_core::{
};
use turbo_tasks::Vc;
use turbopack_binding::{
turbo::{
tasks::Value,
tasks_env::ProcessEnv,
tasks_fs::{rope::RopeBuilder, File, FileSystemPath},
},
turbo::{tasks::Value, tasks_env::ProcessEnv, tasks_fs::FileSystemPath},
turbopack::{
build::BuildChunkingContext,
core::{
asset::AssetContent,
chunk::{ChunkableModule, ChunkingContext, EvaluatableAssets},
compile_time_info::CompileTimeInfo,
context::AssetContext,
file_source::FileSource,
output::OutputAsset,
reference_type::{EntryReferenceSubType, ReferenceType},
source::Source,
virtual_source::VirtualSource,
},
ecmascript::{
chunk::{EcmascriptChunkPlaceable, EcmascriptChunkingContext},
Expand Down Expand Up @@ -201,8 +191,6 @@ async fn get_page_entries_for_root_directory(
Vc::upcast(FileSource::new(app.project_path)),
next_router_root,
app.next_router_path,
app.original_path,
PathType::Page,
));

// This only makes sense on the server.
Expand All @@ -213,8 +201,6 @@ async fn get_page_entries_for_root_directory(
Vc::upcast(FileSource::new(document.project_path)),
next_router_root,
document.next_router_path,
document.original_path,
PathType::Page,
));

// This only makes sense on both the client and the server, but they should map
Expand All @@ -226,8 +212,6 @@ async fn get_page_entries_for_root_directory(
Vc::upcast(FileSource::new(error.project_path)),
next_router_root,
error.next_router_path,
error.original_path,
PathType::Page,
));

if let Some(api) = api {
Expand All @@ -237,7 +221,6 @@ async fn get_page_entries_for_root_directory(
api,
next_router_root,
&mut entries,
PathType::PagesAPI,
)
.await?;
}
Expand All @@ -249,7 +232,6 @@ async fn get_page_entries_for_root_directory(
pages,
next_router_root,
&mut entries,
PathType::Page,
)
.await?;
}
Expand All @@ -264,7 +246,6 @@ async fn get_page_entries_for_directory(
pages_structure: Vc<PagesDirectoryStructure>,
next_router_root: Vc<FileSystemPath>,
entries: &mut Vec<Vc<PageEntry>>,
path_type: PathType,
) -> Result<()> {
let PagesDirectoryStructure {
ref items,
Expand All @@ -276,16 +257,14 @@ async fn get_page_entries_for_directory(
let PagesStructureItem {
project_path,
next_router_path,
original_path,
original_path: _,
} = *item.await?;
entries.push(get_page_entry_for_file(
ssr_module_context,
client_module_context,
Vc::upcast(FileSource::new(project_path)),
next_router_root,
next_router_path,
original_path,
path_type,
));
}

Expand All @@ -296,7 +275,6 @@ async fn get_page_entries_for_directory(
*child,
next_router_root,
entries,
path_type,
)
.await?;
}
Expand All @@ -322,129 +300,13 @@ async fn get_page_entry_for_file(
source: Vc<Box<dyn Source>>,
next_router_root: Vc<FileSystemPath>,
next_router_path: Vc<FileSystemPath>,
next_original_path: Vc<FileSystemPath>,
path_type: PathType,
) -> Result<Vc<PageEntry>> {
let reference_type = Value::new(ReferenceType::Entry(match path_type {
PathType::Page => EntryReferenceSubType::Page,
PathType::PagesAPI => EntryReferenceSubType::PagesApi,
_ => bail!("Invalid path type"),
}));

let pathname = pathname_for_path(next_router_root, next_router_path, path_type);
let reference_type = Value::new(ReferenceType::Entry(EntryReferenceSubType::Page));

let definition_page = format!("/{}", next_original_path.await?);
let definition_pathname = pathname.await?;
let pathname = pathname_for_path(next_router_root, next_router_path, PathType::Page);

let ssr_module = ssr_module_context.process(source, reference_type.clone());

let mut result = RopeBuilder::default();

match path_type {
PathType::Page => {
// Sourced from https://github.com/vercel/next.js/blob/2848ce51d1552633119c89ab49ff7fe2e4e91c91/packages/next/src/build/webpack/loaders/next-route-loader/index.ts
writedoc!(
result,
r#"
import RouteModule from "next/dist/server/future/route-modules/pages/module"
import {{ hoist }} from "next/dist/build/webpack/loaders/next-route-loader/helpers"

import Document from "@vercel/turbopack-next/pages/_document"
import App from "@vercel/turbopack-next/pages/_app"

import * as userland from "INNER"

export default hoist(userland, "default")

export const getStaticProps = hoist(userland, "getStaticProps")
export const getStaticPaths = hoist(userland, "getStaticPaths")
export const getServerSideProps = hoist(userland, "getServerSideProps")
export const config = hoist(userland, "config")
export const reportWebVitals = hoist(userland, "reportWebVitals")

export const unstable_getStaticProps = hoist(userland, "unstable_getStaticProps")
export const unstable_getStaticPaths = hoist(userland, "unstable_getStaticPaths")
export const unstable_getStaticParams = hoist(userland, "unstable_getStaticParams")
export const unstable_getServerProps = hoist(userland, "unstable_getServerProps")
export const unstable_getServerSideProps = hoist(userland, "unstable_getServerSideProps")

export const routeModule = new RouteModule({{
definition: {{
kind: "PAGES",
page: "{definition_page}",
pathname: "{definition_pathname}",
// The following aren't used in production, but are
// required for the RouteModule constructor.
bundlePath: "",
filename: "",
}},
components: {{
App,
Document,
}},
userland,
}})
"#
)?;

// When we're building the instrumentation page (only when the
// instrumentation file conflicts with a page also labeled
// /instrumentation) hoist the `register` method.
if definition_page == "/instrumentation" || definition_page == "/src/instrumentation" {
writeln!(
result,
r#"export const register = hoist(userland, "register")"#
)?;
}
}
PathType::PagesAPI => {
// Sourced from https://github.com/vercel/next.js/blob/2848ce51d1552633119c89ab49ff7fe2e4e91c91/packages/next/src/build/webpack/loaders/next-route-loader/index.ts
writedoc!(
result,
r#"
import RouteModule from "next/dist/server/future/route-modules/pages-api/module"
import {{ hoist }} from "next/dist/build/webpack/loaders/next-route-loader/helpers"

import * as userland from "INNER"

export default hoist(userland, "default")
export const config = hoist(userland, "config")

export const routeModule = new RouteModule({{
definition: {{
kind: "PAGES_API",
page: "{definition_page}",
pathname: "{definition_pathname}",
// The following aren't used in production, but are
// required for the RouteModule constructor.
bundlePath: "",
filename: "",
}},
userland,
}})
"#
)?;
}
_ => bail!("Invalid path type"),
};

let file = File::from(result.build());

let asset = VirtualSource::new(
source.ident().path().join(match path_type {
PathType::Page => "pages-entry.tsx".to_string(),
PathType::PagesAPI => "pages-api-entry.tsx".to_string(),
_ => bail!("Invalid path type"),
}),
AssetContent::file(file.into()),
);
let ssr_module = ssr_module_context.process(
Vc::upcast(asset),
Value::new(ReferenceType::Internal(Vc::cell(indexmap! {
"INNER".to_string() => ssr_module,
}))),
);

let client_module = create_page_loader_entry_module(client_module_context, source, pathname);

let Some(client_module) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// the other imports
import startHandler from '../internal/page-server-handler'

import Document from '@vercel/turbopack-next/pages/_document'
import App from '@vercel/turbopack-next/pages/_app'
import Document from '@vercel/turbopack-next/pages/_document'

import chunkGroup from 'INNER_CLIENT_CHUNK_GROUP'

Expand Down
1 change: 0 additions & 1 deletion packages/next-swc/crates/next-core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use crate::next_config::{NextConfig, OutputType};
#[derive(Debug, Clone, Copy, PartialEq, Eq, TaskInput)]
pub enum PathType {
Page,
PagesAPI,
Data,
}

Expand Down
18 changes: 4 additions & 14 deletions packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ import { fileExists } from '../lib/file-exists'
import { getRouteLoaderEntry } from './webpack/loaders/next-route-loader'
import { isInternalComponent } from '../lib/is-internal-component'
import { isStaticMetadataRouteFile } from '../lib/metadata/is-metadata-route'
import { RouteKind } from '../server/future/route-kind'
import { encodeToBase64 } from './webpack/loaders/utils'

export async function getStaticInfoIncludingLayouts({
isInsideAppDir,
Expand Down Expand Up @@ -591,31 +589,23 @@ export async function createEntrypoints(
assetPrefix: config.assetPrefix,
nextConfigOutput: config.output,
preferredRegion: staticInfo.preferredRegion,
middlewareConfig: encodeToBase64(staticInfo.middleware || {}),
middlewareConfig: Buffer.from(
JSON.stringify(staticInfo.middleware || {})
).toString('base64'),
})
} else if (isInstrumentationHookFile(page) && pagesType === 'root') {
server[serverBundlePath.replace('src/', '')] = {
import: absolutePagePath,
// the '../' is needed to make sure the file is not chunked
filename: `../${INSTRUMENTATION_HOOK_FILENAME}.js`,
}
} else if (isAPIRoute(page)) {
server[serverBundlePath] = [
getRouteLoaderEntry({
kind: RouteKind.PAGES_API,
page,
absolutePagePath,
preferredRegion: staticInfo.preferredRegion,
middlewareConfig: staticInfo.middleware || {},
}),
]
} else if (
!isAPIRoute(page) &&
!isMiddlewareFile(page) &&
!isInternalComponent(absolutePagePath)
) {
server[serverBundlePath] = [
getRouteLoaderEntry({
kind: RouteKind.PAGES,
page,
pages,
absolutePagePath,
Expand Down
Loading
Loading