Skip to content

Commit 2718ee4

Browse files
committed
Exclude next-js condition from middleware, proxy, and instrumentation
These scopes aren't fully Next.js environments and can't access many Next.js APIs. In the future this might change but for now we will exclude the `next-js` condition when bundling for these entrypoints so that 3rd parties that target Next.js can do so with the expectation that regardless of what environment they are running in the full suite of Next.js libraries they support will work.
1 parent 69e1989 commit 2718ee4

File tree

90 files changed

+672
-17
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+672
-17
lines changed

crates/next-core/src/next_edge/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ pub async fn get_edge_resolve_options_context(
153153
custom_conditions.push(rcstr!("react-server"));
154154
};
155155

156-
if *next_config.enable_cache_components().await? {
157-
custom_conditions.push(rcstr!("next-js"));
158-
};
156+
// Edge runtime is disabled for projects with Cache Components enabled except for Middleware
157+
// but Middleware doesn't have all Next.js APIs so we omit the "next-js" condition for all edge
158+
// entrypoints
159159

160160
let resolve_options_context = ResolveOptionsContext {
161161
enable_node_modules: Some(project_path.root().owned().await?),

crates/next-core/src/next_server/context.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,10 @@ pub async fn get_server_resolve_options_context(
214214
custom_conditions.push(rcstr!("react-server"));
215215
};
216216

217-
if *next_config.enable_cache_components().await? {
217+
if *next_config.enable_cache_components().await?
218+
// Middleware shouldn't use the "next-js" condition because it doesn't have all Next.js APIs available
219+
&& !matches!(ty, ServerContextType::Middleware { .. } | ServerContextType::Instrumentation { .. })
220+
{
218221
custom_conditions.push(rcstr!("next-js"));
219222
};
220223

packages/next/src/build/webpack-config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,6 @@ export default async function getBaseWebpackConfig(
687687
: distDir
688688

689689
const conditionNames = [
690-
...(config.cacheComponents === true ? ['next-js'] : []),
691690
...(isEdgeServer ? [edgeConditionName] : []),
692691
// inherits Webpack's default conditions
693692
'...',
@@ -1389,6 +1388,17 @@ export default async function getBaseWebpackConfig(
13891388
},
13901389
module: {
13911390
rules: [
1391+
{
1392+
issuerLayer: {
1393+
not: [WEBPACK_LAYERS.middleware, WEBPACK_LAYERS.instrument],
1394+
},
1395+
resolve: {
1396+
conditionNames: [
1397+
config.cacheComponents ? 'next-js' : '',
1398+
'...',
1399+
].filter(Boolean) as string[],
1400+
},
1401+
},
13921402
// Alias server-only and client-only to proper exports based on bundling layers
13931403
{
13941404
issuerLayer: {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return <div>Hello World</div>
3+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import CJSExportsDefault from 'my-cjs-package/exports'
2+
import ExternalCJSExportsDefault from 'my-external-cjs-package/exports'
3+
import ESMExportsDefault from 'my-esm-package/exports'
4+
import ExternalESMExportsDefault from 'my-external-esm-package/exports'
5+
6+
export function register() {
7+
console.log('==== REGISTER START ====')
8+
console.log('CJSExportsDefault:', CJSExportsDefault)
9+
console.log('ExternalCJSExportsDefault:', ExternalCJSExportsDefault)
10+
console.log('ESMExportsDefault:', ESMExportsDefault)
11+
console.log('ExternalESMExportsDefault:', ExternalESMExportsDefault)
12+
console.log('==== REGISTER END ====')
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"dependencies": {
3+
"my-cjs-package": "file:./sym-linked-packages/my-cjs-package",
4+
"my-esm-package": "file:./sym-linked-packages/my-esm-package",
5+
"my-external-cjs-package": "file:./sym-linked-packages/my-external-cjs-package",
6+
"my-external-esm-package": "file:./sym-linked-packages/my-external-esm-package"
7+
}
8+
}

0 commit comments

Comments
 (0)