Unverified Commit 964a92a2 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add node middleware handling for turbopack (#76360)

Continuation of https://github.com/vercel/next.js/pull/75624 this adds
the same support for turbopack and enables the related test suites.
parent b60bb5e7
This diff is collapsed.
...@@ -843,7 +843,7 @@ impl PageEndpoint { ...@@ -843,7 +843,7 @@ impl PageEndpoint {
.process(self.source(), reference_type.clone()) .process(self.source(), reference_type.clone())
.module(); .module();
let config = parse_config_from_source(ssr_module).await?; let config = parse_config_from_source(ssr_module, NextRuntime::default()).await?;
let is_edge = matches!(config.runtime, NextRuntime::Edge); let is_edge = matches!(config.runtime, NextRuntime::Edge);
let ssr_module = if is_edge { let ssr_module = if is_edge {
......
...@@ -18,7 +18,7 @@ use next_core::{ ...@@ -18,7 +18,7 @@ use next_core::{
get_server_resolve_options_context, ServerContextType, get_server_resolve_options_context, ServerContextType,
}, },
next_telemetry::NextFeatureTelemetry, next_telemetry::NextFeatureTelemetry,
util::NextRuntime, util::{parse_config_from_source, NextRuntime},
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tracing::Instrument; use tracing::Instrument;
...@@ -55,6 +55,7 @@ use turbopack_core::{ ...@@ -55,6 +55,7 @@ use turbopack_core::{
module::Module, module::Module,
module_graph::{GraphEntries, ModuleGraph, SingleModuleGraph, VisitedModules}, module_graph::{GraphEntries, ModuleGraph, SingleModuleGraph, VisitedModules},
output::{OutputAsset, OutputAssets}, output::{OutputAsset, OutputAssets},
reference_type::{EntryReferenceSubType, ReferenceType},
resolve::{find_context_file, FindContextFileResult}, resolve::{find_context_file, FindContextFileResult},
source_map::OptionStringifiedSourceMap, source_map::OptionStringifiedSourceMap,
version::{ version::{
...@@ -1243,9 +1244,9 @@ impl Project { ...@@ -1243,9 +1244,9 @@ impl Project {
)); ));
} }
Ok(Vc::upcast(ModuleAssetContext::new( let edge_module_context = ModuleAssetContext::new(
TransitionOptions { TransitionOptions {
named_transitions: transitions.into_iter().collect(), named_transitions: transitions.clone().into_iter().collect(),
..Default::default() ..Default::default()
} }
.cell(), .cell(),
...@@ -1273,7 +1274,60 @@ impl Project { ...@@ -1273,7 +1274,60 @@ impl Project {
self.execution_context(), self.execution_context(),
), ),
Vc::cell("middleware".into()), Vc::cell("middleware".into()),
))) );
let middleware = self.find_middleware();
let FindContextFileResult::Found(fs_path, _) = *middleware.await? else {
return Ok(Vc::upcast(edge_module_context));
};
let source = Vc::upcast(FileSource::new(*fs_path));
let module = edge_module_context
.process(
source,
Value::new(ReferenceType::Entry(EntryReferenceSubType::Middleware)),
)
.module();
let config = parse_config_from_source(module, NextRuntime::Edge).await?;
if matches!(config.runtime, NextRuntime::NodeJs) {
let server_module_context = ModuleAssetContext::new(
TransitionOptions {
named_transitions: transitions.clone().into_iter().collect(),
..Default::default()
}
.cell(),
self.server_compile_time_info(),
get_server_module_options_context(
self.project_path(),
self.execution_context(),
Value::new(ServerContextType::Middleware {
app_dir,
ecmascript_client_reference_transition_name,
}),
self.next_mode(),
self.next_config(),
NextRuntime::NodeJs,
self.encryption_key(),
),
get_server_resolve_options_context(
self.project_path(),
Value::new(ServerContextType::Middleware {
app_dir,
ecmascript_client_reference_transition_name,
}),
self.next_mode(),
self.next_config(),
self.execution_context(),
),
Vc::cell("middleware".into()),
);
Ok(Vc::upcast(server_module_context))
} else {
Ok(Vc::upcast(edge_module_context))
}
} }
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -1300,6 +1354,7 @@ impl Project { ...@@ -1300,6 +1354,7 @@ impl Project {
Ok(Vc::upcast(MiddlewareEndpoint::new( Ok(Vc::upcast(MiddlewareEndpoint::new(
self, self,
self.await?.build_id.clone(),
middleware_asset_context, middleware_asset_context,
source, source,
app_dir.as_deref().copied(), app_dir.as_deref().copied(),
......
...@@ -270,10 +270,7 @@ pub async fn get_server_resolve_options_context( ...@@ -270,10 +270,7 @@ pub async fn get_server_resolve_options_context(
ResolvedVc::upcast(next_external_plugin), ResolvedVc::upcast(next_external_plugin),
] ]
} }
ServerContextType::Middleware { .. } => { ServerContextType::Middleware { .. } | ServerContextType::Instrumentation { .. } => {
vec![ResolvedVc::upcast(next_node_shared_runtime_plugin)]
}
ServerContextType::Instrumentation { .. } => {
vec![ vec![
ResolvedVc::upcast(next_node_shared_runtime_plugin), ResolvedVc::upcast(next_node_shared_runtime_plugin),
ResolvedVc::upcast(server_external_packages_plugin), ResolvedVc::upcast(server_external_packages_plugin),
......
...@@ -427,6 +427,7 @@ async fn parse_route_matcher_from_js_value( ...@@ -427,6 +427,7 @@ async fn parse_route_matcher_from_js_value(
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn parse_config_from_source( pub async fn parse_config_from_source(
module: ResolvedVc<Box<dyn Module>>, module: ResolvedVc<Box<dyn Module>>,
default_runtime: NextRuntime,
) -> Result<Vc<NextSourceConfig>> { ) -> Result<Vc<NextSourceConfig>> {
if let Some(ecmascript_asset) = ResolvedVc::try_sidecast::<Box<dyn EcmascriptParsable>>(module) if let Some(ecmascript_asset) = ResolvedVc::try_sidecast::<Box<dyn EcmascriptParsable>>(module)
{ {
...@@ -456,9 +457,13 @@ pub async fn parse_config_from_source( ...@@ -456,9 +457,13 @@ pub async fn parse_config_from_source(
return WrapFuture::new( return WrapFuture::new(
async { async {
let value = eval_context.eval(init); let value = eval_context.eval(init);
Ok(parse_config_from_js_value(*module, &value) Ok(parse_config_from_js_value(
.await? *module,
.cell()) &value,
default_runtime,
)
.await?
.cell())
}, },
|f, ctx| GLOBALS.set(globals, || f.poll(ctx)), |f, ctx| GLOBALS.set(globals, || f.poll(ctx)),
) )
...@@ -537,14 +542,23 @@ pub async fn parse_config_from_source( ...@@ -537,14 +542,23 @@ pub async fn parse_config_from_source(
} }
} }
} }
Ok(Default::default()) let config = NextSourceConfig {
runtime: default_runtime,
..Default::default()
};
Ok(config.cell())
} }
async fn parse_config_from_js_value( async fn parse_config_from_js_value(
module: Vc<Box<dyn Module>>, module: Vc<Box<dyn Module>>,
value: &JsValue, value: &JsValue,
default_runtime: NextRuntime,
) -> Result<NextSourceConfig> { ) -> Result<NextSourceConfig> {
let mut config = NextSourceConfig::default(); let mut config = NextSourceConfig {
runtime: default_runtime,
..Default::default()
};
if let JsValue::Object { parts, .. } = value { if let JsValue::Object { parts, .. } = value {
for part in parts { for part in parts {
......
...@@ -80,6 +80,7 @@ import { ...@@ -80,6 +80,7 @@ import {
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY, UNDERSCORE_NOT_FOUND_ROUTE_ENTRY,
UNDERSCORE_NOT_FOUND_ROUTE, UNDERSCORE_NOT_FOUND_ROUTE,
DYNAMIC_CSS_MANIFEST, DYNAMIC_CSS_MANIFEST,
TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST,
} from '../shared/lib/constants' } from '../shared/lib/constants'
import { import {
getSortedRoutes, getSortedRoutes,
...@@ -2348,6 +2349,18 @@ export default async function build( ...@@ -2348,6 +2349,18 @@ export default async function build(
}, },
], ],
} }
if (turboNextBuild) {
await writeManifest(
path.join(
distDir,
'static',
buildId,
TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST
),
functionsConfigManifest.functions['/_middleware'].matchers || []
)
}
} }
} }
......
...@@ -692,12 +692,14 @@ export async function handleEntrypoints({ ...@@ -692,12 +692,14 @@ export async function handleEntrypoints({
dev.hooks.handleWrittenEndpoint(key, writtenEndpoint) dev.hooks.handleWrittenEndpoint(key, writtenEndpoint)
processIssues(currentEntryIssues, key, writtenEndpoint, false, logErrors) processIssues(currentEntryIssues, key, writtenEndpoint, false, logErrors)
await manifestLoader.loadMiddlewareManifest('middleware', 'middleware') await manifestLoader.loadMiddlewareManifest('middleware', 'middleware')
if (dev) { const middlewareConfig =
manifestLoader.getMiddlewareManifest(key)?.middleware['/']
if (dev && middlewareConfig) {
dev.serverFields.middleware = { dev.serverFields.middleware = {
match: null as any, match: null as any,
page: '/', page: '/',
matchers: matchers: middlewareConfig.matchers,
manifestLoader.getMiddlewareManifest(key)?.middleware['/'].matchers,
} }
} }
} }
......
...@@ -49,6 +49,7 @@ import { ...@@ -49,6 +49,7 @@ import {
DEV_CLIENT_PAGES_MANIFEST, DEV_CLIENT_PAGES_MANIFEST,
DEV_CLIENT_MIDDLEWARE_MANIFEST, DEV_CLIENT_MIDDLEWARE_MANIFEST,
PHASE_DEVELOPMENT_SERVER, PHASE_DEVELOPMENT_SERVER,
TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST,
} from '../../../shared/lib/constants' } from '../../../shared/lib/constants'
import { getMiddlewareRouteMatcher } from '../../../shared/lib/router/utils/middleware-route-matcher' import { getMiddlewareRouteMatcher } from '../../../shared/lib/router/utils/middleware-route-matcher'
...@@ -902,6 +903,9 @@ async function startWatcher(opts: SetupOpts) { ...@@ -902,6 +903,9 @@ async function startWatcher(opts: SetupOpts) {
const devMiddlewareManifestPath = `/_next/${CLIENT_STATIC_FILES_PATH}/development/${DEV_CLIENT_MIDDLEWARE_MANIFEST}` const devMiddlewareManifestPath = `/_next/${CLIENT_STATIC_FILES_PATH}/development/${DEV_CLIENT_MIDDLEWARE_MANIFEST}`
opts.fsChecker.devVirtualFsItems.add(devMiddlewareManifestPath) opts.fsChecker.devVirtualFsItems.add(devMiddlewareManifestPath)
const devTurbopackMiddlewareManifestPath = `/_next/${CLIENT_STATIC_FILES_PATH}/development/${TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST}`
opts.fsChecker.devVirtualFsItems.add(devTurbopackMiddlewareManifestPath)
async function requestHandler(req: IncomingMessage, res: ServerResponse) { async function requestHandler(req: IncomingMessage, res: ServerResponse) {
const parsedUrl = url.parse(req.url || '/') const parsedUrl = url.parse(req.url || '/')
...@@ -918,7 +922,10 @@ async function startWatcher(opts: SetupOpts) { ...@@ -918,7 +922,10 @@ async function startWatcher(opts: SetupOpts) {
return { finished: true } return { finished: true }
} }
if (parsedUrl.pathname?.includes(devMiddlewareManifestPath)) { if (
parsedUrl.pathname?.includes(devMiddlewareManifestPath) ||
parsedUrl.pathname?.includes(devTurbopackMiddlewareManifestPath)
) {
res.statusCode = 200 res.statusCode = 200
res.setHeader('Content-Type', 'application/json; charset=utf-8') res.setHeader('Content-Type', 'application/json; charset=utf-8')
res.end(JSON.stringify(serverFields.middleware?.matchers || [])) res.end(JSON.stringify(serverFields.middleware?.matchers || []))
......
import { NextResponse } from 'next/server'
export default function middleware(request) {
const res = NextResponse.rewrite(new URL('/', request.url))
res.headers.set('X-From-Middleware', 'true')
return res
}
export const config = {
runtime: 'nodejs',
matcher: [
{ source: '/source-match' },
{
source: '/has-match-1',
has: [
{
type: 'header',
key: 'x-my-header',
value: '(?<myHeader>.*)',
},
],
},
{
source: '/has-match-2',
has: [
{
type: 'query',
key: 'my-query',
},
],
},
{
source: '/has-match-3',
has: [
{
type: 'cookie',
key: 'loggedIn',
value: '(?<loggedIn>true)',
},
],
},
{
source: '/has-match-4',
has: [
{
type: 'host',
value: 'example.com',
},
],
},
{
source: '/has-match-5',
has: [
{
type: 'header',
key: 'hasParam',
value: 'with-params',
},
],
},
{
source: '/missing-match-1',
missing: [
{
type: 'header',
key: 'hello',
value: '(.*)',
},
],
},
{
source: '/missing-match-2',
missing: [
{
type: 'query',
key: 'test',
value: 'value',
},
],
},
{
source:
'/((?!api|monitoring|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt|manifest|icon|source-match|has-match-1|has-match-2|has-match-3|has-match-4|has-match-5|missing-match-1|missing-match-2|routes).*)',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
],
},
],
}
module.exports = {
experimental: {
nodeMiddleware: true,
},
}
...@@ -13,9 +13,20 @@ const isModeDeploy = process.env.NEXT_TEST_MODE === 'deploy' ...@@ -13,9 +13,20 @@ const isModeDeploy = process.env.NEXT_TEST_MODE === 'deploy'
describe('Middleware custom matchers', () => { describe('Middleware custom matchers', () => {
let next: NextInstance let next: NextInstance
if ((global as any).isNextDeploy && process.env.TEST_NODE_MIDDLEWARE) {
return it('should skip deploy for now', () => {})
}
beforeAll(async () => { beforeAll(async () => {
next = await createNext({ next = await createNext({
files: new FileRef(join(__dirname, '../app')), files: new FileRef(join(__dirname, '../app')),
overrideFiles: process.env.TEST_NODE_MIDDLEWARE
? {
'middleware.js': new FileRef(
join(__dirname, '../app/middleware-node.js')
),
}
: {},
}) })
}) })
afterAll(() => next.destroy()) afterAll(() => next.destroy())
......
process.env.TEST_NODE_MIDDLEWARE = 'true'
require('./index.test')
process.env.TEST_NODE_MIDDLEWARE = 'true' process.env.TEST_NODE_MIDDLEWARE = 'true'
if (process.env.TURBOPACK) { require('./index.test')
it('should skip', () => {})
} else {
require('./index.test')
}
...@@ -111,14 +111,18 @@ describe('Edge runtime code with imports', () => { ...@@ -111,14 +111,18 @@ describe('Edge runtime code with imports', () => {
stderr: true, stderr: true,
}) })
expect(stderr).toContain(getUnsupportedModuleWarning(moduleName)) expect(stderr).toContain(getUnsupportedModuleWarning(moduleName))
context.app = await nextStart(
context.appDir, // TODO: should this be failing build or not in turbopack
context.appPort, if (!process.env.TURBOPACK) {
appOption context.app = await nextStart(
) context.appDir,
const res = await fetchViaHTTP(context.appPort, url) context.appPort,
expect(res.status).toBe(500) appOption
expectUnsupportedModuleProdError(moduleName) )
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500)
expectUnsupportedModuleProdError(moduleName)
}
}) })
} }
) )
......
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