Skip to content

Commit

Permalink
ignore fully dynamic requests on server side (vercel#62949)
Browse files Browse the repository at this point in the history
### What?

make sure that we don't error for dynamic requests on server side.

It will throw at runtime when using a dynamic request.

### Why?

Not all packages are fully bundler compatible, but still work when only
using the parts that work. We don't want to block users from using them
by having an hard compile error.

### How?


Closes PACK-2675
  • Loading branch information
sokra authored Mar 11, 2024
1 parent d55699d commit b4b757c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 31 deletions.
42 changes: 11 additions & 31 deletions packages/next-swc/crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,15 @@ pub async fn get_server_module_options_context(
get_styled_components_transform_rule(next_config).await?;
let styled_jsx_transform_rule = get_styled_jsx_transform_rule(next_config, versions).await?;

let module_options_context = ModuleOptionsContext {
execution_context: Some(execution_context),
use_swc_css,
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
import_externals: *next_config.import_externals().await?,
ignore_dynamic_requests: true,
..Default::default()
};

let module_options_context = match ty.into_value() {
ServerContextType::Pages { .. }
| ServerContextType::PagesData { .. }
Expand Down Expand Up @@ -441,12 +450,8 @@ pub async fn get_server_module_options_context(
);

let module_options_context = ModuleOptionsContext {
execution_context: Some(execution_context),
esm_url_rewrite_behavior: url_rewrite_behavior,
use_swc_css,
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
import_externals: *next_config.import_externals().await?,
..Default::default()
..module_options_context
};

let foreign_code_module_options_context = ModuleOptionsContext {
Expand Down Expand Up @@ -503,14 +508,6 @@ pub async fn get_server_module_options_context(
next_server_rules.extend(custom_source_transform_rules.clone());
next_server_rules.extend(source_transform_rules);

let module_options_context = ModuleOptionsContext {
execution_context: Some(execution_context),
use_swc_css,
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
import_externals: *next_config.import_externals().await?,
..Default::default()
};

let foreign_code_module_options_context = ModuleOptionsContext {
custom_rules: foreign_next_server_rules.clone(),
enable_webpack_loaders: foreign_webpack_loaders,
Expand Down Expand Up @@ -579,14 +576,6 @@ pub async fn get_server_module_options_context(
next_server_rules.extend(custom_source_transform_rules.clone());
next_server_rules.extend(source_transform_rules);

let module_options_context = ModuleOptionsContext {
execution_context: Some(execution_context),
use_swc_css,
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
import_externals: *next_config.import_externals().await?,
..Default::default()
};

let foreign_code_module_options_context = ModuleOptionsContext {
custom_rules: foreign_next_server_rules.clone(),
enable_webpack_loaders: foreign_webpack_loaders,
Expand Down Expand Up @@ -624,10 +613,7 @@ pub async fn get_server_module_options_context(
next_server_rules.extend(source_transform_rules);

let module_options_context = ModuleOptionsContext {
execution_context: Some(execution_context),
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
import_externals: *next_config.import_externals().await?,
..Default::default()
..module_options_context
};
let foreign_code_module_options_context = ModuleOptionsContext {
custom_rules: internal_custom_rules.clone(),
Expand Down Expand Up @@ -671,12 +657,6 @@ pub async fn get_server_module_options_context(
next_server_rules.extend(custom_source_transform_rules);
next_server_rules.extend(source_transform_rules);

let module_options_context = ModuleOptionsContext {
execution_context: Some(execution_context),
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly),
import_externals: *next_config.import_externals().await?,
..Default::default()
};
let foreign_code_module_options_context = ModuleOptionsContext {
custom_rules: internal_custom_rules.clone(),
enable_webpack_loaders: foreign_webpack_loaders,
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/app-dir/dynamic-requests/app/hello/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function GET() {
if (Math.random() < 0) dynamic()
return new Response('Hello World')
}

function dynamic() {
const dynamic = Math.random() + ''
require(dynamic)
import(dynamic)
}
7 changes: 7 additions & 0 deletions test/e2e/app-dir/dynamic-requests/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
10 changes: 10 additions & 0 deletions test/e2e/app-dir/dynamic-requests/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default async function Page() {
if (Math.random() < 0) dynamic()
return <p>Hello World</p>
}

function dynamic() {
const dynamic = Math.random() + ''
require(dynamic)
import(dynamic)
}
18 changes: 18 additions & 0 deletions test/e2e/app-dir/dynamic-requests/dynamic-requests.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { nextTestSetup } from 'e2e-utils'

describe('dynamic-requests', () => {
const { next } = nextTestSetup({
files: __dirname,
})

it('should not error for dynamic requests in pages', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('Hello World')
})

it('should not error for dynamic requests in routes', async () => {
const res = await next.fetch('/hello')
const html = await res.text()
expect(html).toContain('Hello World')
})
})
6 changes: 6 additions & 0 deletions test/e2e/app-dir/dynamic-requests/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}

module.exports = nextConfig

0 comments on commit b4b757c

Please sign in to comment.