Skip to content

Commit 297acd6

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 a135a7d commit 297acd6

File tree

105 files changed

+774
-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.

105 files changed

+774
-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: {

test/e2e/app-dir/hello-world/node_modules/.modules.yaml

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/app-dir/hello-world/node_modules/.pnpm/foo@file+packages+foo/node_modules/foo/entry/edge-light.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/app-dir/hello-world/node_modules/.pnpm/foo@file+packages+foo/node_modules/foo/entry/imp.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/app-dir/hello-world/node_modules/.pnpm/foo@file+packages+foo/node_modules/foo/entry/next-js.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/app-dir/hello-world/node_modules/.pnpm/foo@file+packages+foo/node_modules/foo/entry/req.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/app-dir/hello-world/node_modules/.pnpm/foo@file+packages+foo/node_modules/foo/package.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/app-dir/hello-world/node_modules/.pnpm/lock.yaml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)