diff --git a/crates/next-custom-transforms/src/transforms/react_server_components.rs b/crates/next-custom-transforms/src/transforms/react_server_components.rs index da68d741af5712..d53f0a3e4661f8 100644 --- a/crates/next-custom-transforms/src/transforms/react_server_components.rs +++ b/crates/next-custom-transforms/src/transforms/react_server_components.rs @@ -833,7 +833,7 @@ impl ReactServerComponentValidator { } } - /// ``` + /// ```js /// import dynamic from 'next/dynamic' /// /// dynamic(() => import(...)) // ✅ diff --git a/crates/next-custom-transforms/src/transforms/server_actions.rs b/crates/next-custom-transforms/src/transforms/server_actions.rs index 37ad8aa980d6bb..3bb41264d00b68 100644 --- a/crates/next-custom-transforms/src/transforms/server_actions.rs +++ b/crates/next-custom-transforms/src/transforms/server_actions.rs @@ -1345,18 +1345,38 @@ impl VisitMut for ServerActions { // If it's compiled in the client layer, each export field needs to be // wrapped by a reference creation call. let create_ref_ident = private_ident!("createServerReference"); + let call_server_ident = private_ident!("callServer"); + let find_source_map_url_ident = private_ident!("findSourceMapURL"); + if !self.config.is_react_server_layer { - // import { createServerReference } from - // 'private-next-rsc-action-client-wrapper' + // import { + // createServerReference, + // callServer, + // findSourceMapURL + // } from 'private-next-rsc-action-client-wrapper' // createServerReference("action_id") new.push(ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl { span: DUMMY_SP, - specifiers: vec![ImportSpecifier::Named(ImportNamedSpecifier { - span: DUMMY_SP, - local: create_ref_ident.clone(), - imported: None, - is_type_only: false, - })], + specifiers: vec![ + ImportSpecifier::Named(ImportNamedSpecifier { + span: DUMMY_SP, + local: create_ref_ident.clone(), + imported: None, + is_type_only: false, + }), + ImportSpecifier::Named(ImportNamedSpecifier { + span: DUMMY_SP, + local: call_server_ident.clone(), + imported: None, + is_type_only: false, + }), + ImportSpecifier::Named(ImportNamedSpecifier { + span: DUMMY_SP, + local: find_source_map_url_ident.clone(), + imported: None, + is_type_only: false, + }), + ], src: Box::new(Str { span: DUMMY_SP, value: "private-next-rsc-action-client-wrapper".into(), @@ -1366,6 +1386,7 @@ impl VisitMut for ServerActions { with: None, phase: Default::default(), }))); + new.rotate_right(1); } for (id, export_name) in self.exported_idents.iter() { @@ -1386,7 +1407,13 @@ impl VisitMut for ServerActions { callee: Callee::Expr(Box::new(Expr::Ident( create_ref_ident.clone(), ))), - args: vec![action_id.as_arg()], + args: vec![ + action_id.as_arg(), + call_server_ident.clone().as_arg(), + Expr::undefined(DUMMY_SP).as_arg(), + find_source_map_url_ident.clone().as_arg(), + "default".as_arg(), + ], ..Default::default() })), }, @@ -1410,7 +1437,13 @@ impl VisitMut for ServerActions { callee: Callee::Expr(Box::new(Expr::Ident( create_ref_ident.clone(), ))), - args: vec![action_id.as_arg()], + args: vec![ + action_id.as_arg(), + call_server_ident.clone().as_arg(), + Expr::undefined(DUMMY_SP).as_arg(), + find_source_map_url_ident.clone().as_arg(), + export_name.clone().as_arg(), + ], ..Default::default() }))), definite: false, @@ -1520,11 +1553,6 @@ impl VisitMut for ServerActions { text: generate_server_actions_comment(actions).into(), }, ); - - if !self.config.is_react_server_layer { - // Make it the first item - new.rotate_right(1); - } } // import { cache as $cache } from "private-next-rsc-cache-wrapper"; diff --git a/crates/next-custom-transforms/tests/errors/server-actions/client-graph/1/output.js b/crates/next-custom-transforms/tests/errors/server-actions/client-graph/1/output.js index af4108db83a766..e5d9d5a247959d 100644 --- a/crates/next-custom-transforms/tests/errors/server-actions/client-graph/1/output.js +++ b/crates/next-custom-transforms/tests/errors/server-actions/client-graph/1/output.js @@ -1,5 +1,5 @@ -/* __next_internal_client_entry_do_not_use__ default auto */ /* __next_internal_action_entry_do_not_use__ {"6a88810ecce4a4e8b59d53b8327d7e98bbf251d7":"$$RSC_SERVER_ACTION_0"} */ export async function $$RSC_SERVER_ACTION_0() {} -export default function App() { +/* __next_internal_client_entry_do_not_use__ default auto */ /* __next_internal_action_entry_do_not_use__ {"6a88810ecce4a4e8b59d53b8327d7e98bbf251d7":"$$RSC_SERVER_ACTION_0"} */ export default function App() { var fn = registerServerReference("6a88810ecce4a4e8b59d53b8327d7e98bbf251d7", $$RSC_SERVER_ACTION_0); return
App
; } +export async function $$RSC_SERVER_ACTION_0() {} diff --git a/crates/next-custom-transforms/tests/fixture/server-actions/client/1/output.js b/crates/next-custom-transforms/tests/fixture/server-actions/client/1/output.js index d6868e65d8390f..fc7f21eff86dde 100644 --- a/crates/next-custom-transforms/tests/fixture/server-actions/client/1/output.js +++ b/crates/next-custom-transforms/tests/fixture/server-actions/client/1/output.js @@ -1,4 +1,4 @@ // app/send.ts -/* __next_internal_action_entry_do_not_use__ {"c18c215a6b7cdc64bf709f3a714ffdef1bf9651d":"default","e10665baac148856374b2789aceb970f66fec33e":"myAction"} */ export default /*#__PURE__*/ createServerReference("c18c215a6b7cdc64bf709f3a714ffdef1bf9651d"); -import { createServerReference } from "private-next-rsc-action-client-wrapper"; -export var myAction = /*#__PURE__*/ createServerReference("e10665baac148856374b2789aceb970f66fec33e"); +/* __next_internal_action_entry_do_not_use__ {"c18c215a6b7cdc64bf709f3a714ffdef1bf9651d":"default","e10665baac148856374b2789aceb970f66fec33e":"myAction"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper"; +export var myAction = /*#__PURE__*/ createServerReference("e10665baac148856374b2789aceb970f66fec33e", callServer, void 0, findSourceMapURL, "myAction"); +export default /*#__PURE__*/ createServerReference("c18c215a6b7cdc64bf709f3a714ffdef1bf9651d", callServer, void 0, findSourceMapURL, "default"); diff --git a/crates/next-custom-transforms/tests/fixture/server-actions/client/2/output.js b/crates/next-custom-transforms/tests/fixture/server-actions/client/2/output.js index 84e74bf7a68126..acf440a6ed4ee6 100644 --- a/crates/next-custom-transforms/tests/fixture/server-actions/client/2/output.js +++ b/crates/next-custom-transforms/tests/fixture/server-actions/client/2/output.js @@ -1,3 +1,3 @@ // app/send.ts -/* __next_internal_action_entry_do_not_use__ {"ab21efdafbe611287bc25c0462b1e0510d13e48b":"foo"} */ export var foo = /*#__PURE__*/ createServerReference("ab21efdafbe611287bc25c0462b1e0510d13e48b"); -import { createServerReference } from "private-next-rsc-action-client-wrapper"; +/* __next_internal_action_entry_do_not_use__ {"ab21efdafbe611287bc25c0462b1e0510d13e48b":"foo"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper"; +export var foo = /*#__PURE__*/ createServerReference("ab21efdafbe611287bc25c0462b1e0510d13e48b", callServer, void 0, findSourceMapURL, "foo"); diff --git a/crates/next-custom-transforms/tests/fixture/server-actions/client/3/output.js b/crates/next-custom-transforms/tests/fixture/server-actions/client/3/output.js index 267291d34b85ba..da69b38a030c34 100644 --- a/crates/next-custom-transforms/tests/fixture/server-actions/client/3/output.js +++ b/crates/next-custom-transforms/tests/fixture/server-actions/client/3/output.js @@ -1,5 +1,5 @@ -/* __next_internal_action_entry_do_not_use__ {"a0c73dd6f5af839e3335c6b19262ecb86cca6af4":"sampleFunction2","bd336abe00c3c59da66acb696fc8e151d8e54ea4":"sampleFunction","d4f2e95bc745b6500b439c0847003511748c8ece":"sampleFunction3","f03b256ee88a51700367acee3082894e25e6e7d9":"sampleFunction4"} */ export var sampleFunction4 = /*#__PURE__*/ createServerReference("f03b256ee88a51700367acee3082894e25e6e7d9"); -import { createServerReference } from "private-next-rsc-action-client-wrapper"; -export var sampleFunction = /*#__PURE__*/ createServerReference("bd336abe00c3c59da66acb696fc8e151d8e54ea4"); -export var sampleFunction2 = /*#__PURE__*/ createServerReference("a0c73dd6f5af839e3335c6b19262ecb86cca6af4"); -export var sampleFunction3 = /*#__PURE__*/ createServerReference("d4f2e95bc745b6500b439c0847003511748c8ece"); +/* __next_internal_action_entry_do_not_use__ {"a0c73dd6f5af839e3335c6b19262ecb86cca6af4":"sampleFunction2","bd336abe00c3c59da66acb696fc8e151d8e54ea4":"sampleFunction","d4f2e95bc745b6500b439c0847003511748c8ece":"sampleFunction3","f03b256ee88a51700367acee3082894e25e6e7d9":"sampleFunction4"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper"; +export var sampleFunction = /*#__PURE__*/ createServerReference("bd336abe00c3c59da66acb696fc8e151d8e54ea4", callServer, void 0, findSourceMapURL, "sampleFunction"); +export var sampleFunction2 = /*#__PURE__*/ createServerReference("a0c73dd6f5af839e3335c6b19262ecb86cca6af4", callServer, void 0, findSourceMapURL, "sampleFunction2"); +export var sampleFunction3 = /*#__PURE__*/ createServerReference("d4f2e95bc745b6500b439c0847003511748c8ece", callServer, void 0, findSourceMapURL, "sampleFunction3"); +export var sampleFunction4 = /*#__PURE__*/ createServerReference("f03b256ee88a51700367acee3082894e25e6e7d9", callServer, void 0, findSourceMapURL, "sampleFunction4"); diff --git a/crates/next-custom-transforms/tests/fixture/server-actions/client/4/output.js b/crates/next-custom-transforms/tests/fixture/server-actions/client/4/output.js index 47e1f1bbe7e00a..aad83b202d0daf 100644 --- a/crates/next-custom-transforms/tests/fixture/server-actions/client/4/output.js +++ b/crates/next-custom-transforms/tests/fixture/server-actions/client/4/output.js @@ -1,2 +1,2 @@ -/* __next_internal_action_entry_do_not_use__ {"ac840dcaf5e8197cb02b7f3a43c119b7a770b272":"bar"} */ export var bar = /*#__PURE__*/ createServerReference("ac840dcaf5e8197cb02b7f3a43c119b7a770b272"); -import { createServerReference } from "private-next-rsc-action-client-wrapper"; +/* __next_internal_action_entry_do_not_use__ {"ac840dcaf5e8197cb02b7f3a43c119b7a770b272":"bar"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper"; +export var bar = /*#__PURE__*/ createServerReference("ac840dcaf5e8197cb02b7f3a43c119b7a770b272", callServer, void 0, findSourceMapURL, "bar"); diff --git a/crates/next-custom-transforms/tests/fixture/server-actions/client/5/output.js b/crates/next-custom-transforms/tests/fixture/server-actions/client/5/output.js index 0f7dcebdb06cba..5f4193468a5044 100644 --- a/crates/next-custom-transforms/tests/fixture/server-actions/client/5/output.js +++ b/crates/next-custom-transforms/tests/fixture/server-actions/client/5/output.js @@ -1,2 +1,2 @@ -/* __next_internal_action_entry_do_not_use__ {"0090eaf4e1f08a2d94f6be401e54a2ded399b87c":"action0","1c36b06e398c97abe5d5d7ae8c672bfddf4e1b91":"$$RSC_SERVER_ACTION_2","6a88810ecce4a4e8b59d53b8327d7e98bbf251d7":"$$RSC_SERVER_ACTION_0","90b5db271335765a4b0eab01f044b381b5ebd5cd":"$$RSC_SERVER_ACTION_1"} */ export var action0 = /*#__PURE__*/ createServerReference("0090eaf4e1f08a2d94f6be401e54a2ded399b87c"); -import { createServerReference } from "private-next-rsc-action-client-wrapper"; +/* __next_internal_action_entry_do_not_use__ {"0090eaf4e1f08a2d94f6be401e54a2ded399b87c":"action0","1c36b06e398c97abe5d5d7ae8c672bfddf4e1b91":"$$RSC_SERVER_ACTION_2","6a88810ecce4a4e8b59d53b8327d7e98bbf251d7":"$$RSC_SERVER_ACTION_0","90b5db271335765a4b0eab01f044b381b5ebd5cd":"$$RSC_SERVER_ACTION_1"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper"; +export var action0 = /*#__PURE__*/ createServerReference("0090eaf4e1f08a2d94f6be401e54a2ded399b87c", callServer, void 0, findSourceMapURL, "action0"); diff --git a/packages/next/src/build/webpack/loaders/next-flight-loader/action-client-wrapper.ts b/packages/next/src/build/webpack/loaders/next-flight-loader/action-client-wrapper.ts index 7288263bb43035..38e54b9effa5b5 100644 --- a/packages/next/src/build/webpack/loaders/next-flight-loader/action-client-wrapper.ts +++ b/packages/next/src/build/webpack/loaders/next-flight-loader/action-client-wrapper.ts @@ -1,22 +1,20 @@ // This file must be bundled in the app's client layer, it shouldn't be directly // imported by the server. -import { callServer } from 'next/dist/client/app-call-server' +export { callServer } from 'next/dist/client/app-call-server' // A noop wrapper to let the Flight client create the server reference. // See also: https://github.com/facebook/react/pull/26632 -export function createServerReference(id: string) { - // Since we're using the Edge build of Flight client for SSR [1], here we need to - // also use the same Edge build to create the reference. For the client bundle, - // we use the default and let Webpack to resolve it to the correct version. - // 1: https://github.com/vercel/next.js/blob/16eb80b0b0be13f04a6407943664b5efd8f3d7d0/packages/next/src/server/app-render/use-flight-response.tsx#L24-L26 - const { createServerReference: createServerReferenceImpl } = ( - !!process.env.NEXT_RUNTIME - ? // eslint-disable-next-line import/no-extraneous-dependencies - require('react-server-dom-webpack/client.edge') - : // eslint-disable-next-line import/no-extraneous-dependencies - require('react-server-dom-webpack/client') - ) as typeof import('react-server-dom-webpack/client') +// Since we're using the Edge build of Flight client for SSR [1], here we need to +// also use the same Edge build to create the reference. For the client bundle, +// we use the default and let Webpack to resolve it to the correct version. +// 1: https://github.com/vercel/next.js/blob/16eb80b0b0be13f04a6407943664b5efd8f3d7d0/packages/next/src/server/app-render/use-flight-response.tsx#L24-L26 +export const createServerReference = ( + (!!process.env.NEXT_RUNTIME + ? // eslint-disable-next-line import/no-extraneous-dependencies + require('react-server-dom-webpack/client.edge') + : // eslint-disable-next-line import/no-extraneous-dependencies + require('react-server-dom-webpack/client')) as typeof import('react-server-dom-webpack/client') +).createServerReference - return createServerReferenceImpl(id, callServer) -} +export const findSourceMapURL = undefined diff --git a/packages/next/src/build/webpack/loaders/next-flight-server-reference-proxy-loader.ts b/packages/next/src/build/webpack/loaders/next-flight-server-reference-proxy-loader.ts index 91757845a6249f..07ade3302b83f1 100644 --- a/packages/next/src/build/webpack/loaders/next-flight-server-reference-proxy-loader.ts +++ b/packages/next/src/build/webpack/loaders/next-flight-server-reference-proxy-loader.ts @@ -18,10 +18,10 @@ const flightServerReferenceProxyLoader: webpack.LoaderDefinitionFunction<{ // Because of that, Webpack is able to concatenate the modules and inline the // reference IDs recursively directly into the module that uses them. return `\ -import { createServerReference } from 'private-next-rsc-action-client-wrapper' +import { createServerReference, callServer, findSourceMapURL } from 'private-next-rsc-action-client-wrapper' export ${ name === 'default' ? 'default' : `const ${name} =` - } /*#__PURE__*/createServerReference(${JSON.stringify(id)})` + } /*#__PURE__*/createServerReference(${JSON.stringify(id)}, callServer, undefined, findSourceMapURL, ${JSON.stringify(name)})` } export default flightServerReferenceProxyLoader