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

Next Router Middleware Support #3690

Merged
merged 9 commits into from
Feb 16, 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
14 changes: 13 additions & 1 deletion crates/next-core/js/src/entry/router.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type { Ipc } from "@vercel/turbopack-next/ipc/index";
import type { IncomingMessage, ServerResponse } from "node:http";
import { Buffer } from "node:buffer";
import { join } from "node:path";
import { createServer, makeRequest } from "@vercel/turbopack-next/ipc/server";
import { makeResolver } from "next/dist/server/router.js";
import loadConfig from "next/dist/server/config";
import { PHASE_DEVELOPMENT_SERVER } from "next/dist/shared/lib/constants";

import "next/dist/server/node-polyfill-fetch.js";

import middlewareChunkGroup from "MIDDLEWARE_CHUNK_GROUP";

type RouterRequest = {
method: string;
pathname: string;
Expand Down Expand Up @@ -71,7 +74,16 @@ async function getResolveRoute(
true
);

return await makeResolver(dir, nextConfig);
const edgeInfo = {
name: "edge",
paths: middlewareChunkGroup.map((chunk: string) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be called files and be relative to the distDir to match our middleware plugin
x-ref: https://github.com/vercel/next.js/blob/7d8f85bd8dbd767dfac0d92eed651877268e62cb/packages/next/src/build/webpack/plugins/middleware-plugin.ts#L35

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I aligned with the return value of getEdgeFunctionInfo, which converts the format in the manifest file into one with paths

https://github.com/vercel/next.js/blob/7d8f85bd8dbd767dfac0d92eed651877268e62cb/packages/next/src/server/next-server.ts#L1640-L1654

So when this info object is provided it could skip calling getEdgeFunctionInfo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, yeah aligning on that return type seems alright

join(process.cwd(), chunk)
),
wasm: [],
env: [],
assets: [],
};
return await makeResolver(dir, nextConfig, edgeInfo);
}

export default async function route(
Expand Down
2 changes: 1 addition & 1 deletion crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ pub async fn load_next_config(execution_context: ExecutionContextVc) -> Result<N
import_map.insert_exact_alias("styled-jsx", ImportMapping::External(None).into());
import_map.insert_wildcard_alias("styled-jsx/", ImportMapping::External(None).into());

let context = node_evaluate_asset_context(Some(import_map.cell()));
let context = node_evaluate_asset_context(Some(import_map.cell()), None);
let find_config_result = find_context_file(project_root, next_configs());
let config_asset = match &*find_config_result.await? {
FindContextFileResult::Found(config_path, _) => Some(SourceAssetVc::new(*config_path)),
Expand Down
7 changes: 5 additions & 2 deletions crates/next-core/src/next_edge/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ use crate::{
};

#[turbo_tasks::function]
pub fn get_edge_compile_time_info(server_addr: ServerAddrVc) -> CompileTimeInfoVc {
pub fn get_edge_compile_time_info(
server_addr: ServerAddrVc,
intention: Value<EnvironmentIntention>,
) -> CompileTimeInfoVc {
CompileTimeInfo {
environment: EnvironmentVc::new(
Value::new(ExecutionEnvironment::EdgeWorker(
EdgeWorkerEnvironment { server_addr }.into(),
)),
Value::new(EnvironmentIntention::Api),
intention,
),
}
.cell()
Expand Down
2 changes: 2 additions & 0 deletions crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub async fn get_next_server_import_map(
);
}
}
ServerContextType::Middleware => {}
}

Ok(import_map.cell())
Expand Down Expand Up @@ -329,6 +330,7 @@ pub async fn insert_next_server_special_aliases(
request_to_import_mapping(app_dir, "next/dist/compiled/react-dom/*"),
);
}
ServerContextType::Middleware => {}
}
Ok(())
}
Expand Down
39 changes: 39 additions & 0 deletions crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum ServerContextType {
PagesData { pages_dir: FileSystemPathVc },
AppSSR { app_dir: FileSystemPathVc },
AppRSC { app_dir: FileSystemPathVc },
Middleware,
}

#[turbo_tasks::function]
Expand Down Expand Up @@ -111,6 +112,24 @@ pub async fn get_server_resolve_options_context(
..resolve_options_context
}
}
ServerContextType::Middleware => {
let resolve_options_context = ResolveOptionsContext {
enable_node_modules: true,
enable_node_externals: true,
module: true,
custom_conditions: vec!["development".to_string()],
..Default::default()
};
ResolveOptionsContext {
enable_typescript: true,
enable_react: true,
rules: vec![(
foreign_code_context_condition,
resolve_options_context.clone().cell(),
)],
..resolve_options_context
}
}
}
.cell())
}
Expand All @@ -134,6 +153,7 @@ pub fn get_server_compile_time_info(
ServerContextType::AppRSC { .. } => {
Value::new(EnvironmentIntention::ServerRendering)
}
ServerContextType::Middleware => Value::new(EnvironmentIntention::Middleware),
},
),
}
Expand Down Expand Up @@ -215,6 +235,25 @@ pub async fn get_server_module_options_context(
..module_options_context
}
}
ServerContextType::Middleware => {
let module_options_context = ModuleOptionsContext {
execution_context: Some(execution_context),
..Default::default()
};
ModuleOptionsContext {
enable_jsx: true,
enable_styled_jsx: true,
enable_postcss_transform,
enable_webpack_loaders,
enable_typescript_transform: true,
rules: vec![(
foreign_code_context_condition,
module_options_context.clone().cell(),
)],
custom_rules,
..module_options_context
}
}
}
.cell();

Expand Down
1 change: 1 addition & 0 deletions crates/next-core/src/next_server/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub async fn get_next_server_transforms_rules(
}
ServerContextType::AppSSR { .. } => (false, None),
ServerContextType::AppRSC { .. } => (true, None),
ServerContextType::Middleware { .. } => (false, None),
};

rules.push(get_next_dynamic_transform_rule(
Expand Down
5 changes: 3 additions & 2 deletions crates/next-core/src/page_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use turbopack_core::{
asset::{Asset, AssetVc},
chunk::{dev::DevChunkingContextVc, ChunkingContextVc},
context::{AssetContext, AssetContextVc},
environment::ServerAddrVc,
environment::{EnvironmentIntention, ServerAddrVc},
reference_type::{EntryReferenceSubType, ReferenceType},
source_asset::SourceAssetVc,
virtual_asset::VirtualAssetVc,
Expand Down Expand Up @@ -136,7 +136,8 @@ pub async fn create_page_source(
.cell()
.into();

let edge_compile_time_info = get_edge_compile_time_info(server_addr);
let edge_compile_time_info =
get_edge_compile_time_info(server_addr, Value::new(EnvironmentIntention::Api));

let edge_chunking_context = DevChunkingContextVc::builder(
project_path,
Expand Down
Loading