Skip to content

Commit

Permalink
Next Router Middleware Support (vercel/turborepo#3690)
Browse files Browse the repository at this point in the history
This updates our Next.js router, passing the `edgeInfo` manifest generated from the `middleware.js` file (or any other configured page extension).

Fixes WEB-277
Fixes WEB-370
  • Loading branch information
jridgewell authored Feb 16, 2023
1 parent 35895f4 commit 34ad8bd
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 35 deletions.
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) =>
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

0 comments on commit 34ad8bd

Please sign in to comment.