Skip to content

Commit c43aa2c

Browse files
shudingunstubbable
authored andcommitted
Unwrap createServerReference, and pass additional parameters (vercel#69190)
For facebook/react#30741 This PR adds several additional parameters to the `createServerReference` calls generated by the Server Reference SWC transform, to later enable React to map server actions that are imported into client components back to their server locations (dev mode only). Currently the inner `findSourceMapURL` function is not yet implemented. In a follow-up we will unwrap `registerServerReference` as well, which is needed for server actions in the react server layer. --------- Co-authored-by: Hendrik Liebau <[email protected]>
1 parent 5f924fa commit c43aa2c

File tree

10 files changed

+75
-49
lines changed

10 files changed

+75
-49
lines changed

crates/next-custom-transforms/src/transforms/react_server_components.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ impl ReactServerComponentValidator {
833833
}
834834
}
835835

836-
/// ```
836+
/// ```js
837837
/// import dynamic from 'next/dynamic'
838838
///
839839
/// dynamic(() => import(...)) // ✅

crates/next-custom-transforms/src/transforms/server_actions.rs

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,18 +1345,38 @@ impl<C: Comments> VisitMut for ServerActions<C> {
13451345
// If it's compiled in the client layer, each export field needs to be
13461346
// wrapped by a reference creation call.
13471347
let create_ref_ident = private_ident!("createServerReference");
1348+
let call_server_ident = private_ident!("callServer");
1349+
let find_source_map_url_ident = private_ident!("findSourceMapURL");
1350+
13481351
if !self.config.is_react_server_layer {
1349-
// import { createServerReference } from
1350-
// 'private-next-rsc-action-client-wrapper'
1352+
// import {
1353+
// createServerReference,
1354+
// callServer,
1355+
// findSourceMapURL
1356+
// } from 'private-next-rsc-action-client-wrapper'
13511357
// createServerReference("action_id")
13521358
new.push(ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
13531359
span: DUMMY_SP,
1354-
specifiers: vec![ImportSpecifier::Named(ImportNamedSpecifier {
1355-
span: DUMMY_SP,
1356-
local: create_ref_ident.clone(),
1357-
imported: None,
1358-
is_type_only: false,
1359-
})],
1360+
specifiers: vec![
1361+
ImportSpecifier::Named(ImportNamedSpecifier {
1362+
span: DUMMY_SP,
1363+
local: create_ref_ident.clone(),
1364+
imported: None,
1365+
is_type_only: false,
1366+
}),
1367+
ImportSpecifier::Named(ImportNamedSpecifier {
1368+
span: DUMMY_SP,
1369+
local: call_server_ident.clone(),
1370+
imported: None,
1371+
is_type_only: false,
1372+
}),
1373+
ImportSpecifier::Named(ImportNamedSpecifier {
1374+
span: DUMMY_SP,
1375+
local: find_source_map_url_ident.clone(),
1376+
imported: None,
1377+
is_type_only: false,
1378+
}),
1379+
],
13601380
src: Box::new(Str {
13611381
span: DUMMY_SP,
13621382
value: "private-next-rsc-action-client-wrapper".into(),
@@ -1366,6 +1386,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
13661386
with: None,
13671387
phase: Default::default(),
13681388
})));
1389+
new.rotate_right(1);
13691390
}
13701391

13711392
for (id, export_name) in self.exported_idents.iter() {
@@ -1386,7 +1407,13 @@ impl<C: Comments> VisitMut for ServerActions<C> {
13861407
callee: Callee::Expr(Box::new(Expr::Ident(
13871408
create_ref_ident.clone(),
13881409
))),
1389-
args: vec![action_id.as_arg()],
1410+
args: vec![
1411+
action_id.as_arg(),
1412+
call_server_ident.clone().as_arg(),
1413+
Expr::undefined(DUMMY_SP).as_arg(),
1414+
find_source_map_url_ident.clone().as_arg(),
1415+
"default".as_arg(),
1416+
],
13901417
..Default::default()
13911418
})),
13921419
},
@@ -1410,7 +1437,13 @@ impl<C: Comments> VisitMut for ServerActions<C> {
14101437
callee: Callee::Expr(Box::new(Expr::Ident(
14111438
create_ref_ident.clone(),
14121439
))),
1413-
args: vec![action_id.as_arg()],
1440+
args: vec![
1441+
action_id.as_arg(),
1442+
call_server_ident.clone().as_arg(),
1443+
Expr::undefined(DUMMY_SP).as_arg(),
1444+
find_source_map_url_ident.clone().as_arg(),
1445+
export_name.clone().as_arg(),
1446+
],
14141447
..Default::default()
14151448
}))),
14161449
definite: false,
@@ -1520,11 +1553,6 @@ impl<C: Comments> VisitMut for ServerActions<C> {
15201553
text: generate_server_actions_comment(actions).into(),
15211554
},
15221555
);
1523-
1524-
if !self.config.is_react_server_layer {
1525-
// Make it the first item
1526-
new.rotate_right(1);
1527-
}
15281556
}
15291557

15301558
// import { cache as $cache } from "private-next-rsc-cache-wrapper";
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/* __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() {}
2-
export default function App() {
1+
/* __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() {
32
var fn = registerServerReference("6a88810ecce4a4e8b59d53b8327d7e98bbf251d7", $$RSC_SERVER_ACTION_0);
43
return <div>App</div>;
54
}
5+
export async function $$RSC_SERVER_ACTION_0() {}

crates/next-custom-transforms/tests/fixture/server-actions/client/1/output.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// app/send.ts
2-
/* __next_internal_action_entry_do_not_use__ {"ab21efdafbe611287bc25c0462b1e0510d13e48b":"foo"} */ export var foo = /*#__PURE__*/ createServerReference("ab21efdafbe611287bc25c0462b1e0510d13e48b");
3-
import { createServerReference } from "private-next-rsc-action-client-wrapper";
2+
/* __next_internal_action_entry_do_not_use__ {"ab21efdafbe611287bc25c0462b1e0510d13e48b":"foo"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper";
3+
export var foo = /*#__PURE__*/ createServerReference("ab21efdafbe611287bc25c0462b1e0510d13e48b", callServer, void 0, findSourceMapURL, "foo");

crates/next-custom-transforms/tests/fixture/server-actions/client/3/output.js

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

crates/next-custom-transforms/tests/fixture/server-actions/client/4/output.js

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

crates/next-custom-transforms/tests/fixture/server-actions/client/5/output.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
// This file must be bundled in the app's client layer, it shouldn't be directly
22
// imported by the server.
33

4-
import { callServer } from 'next/dist/client/app-call-server'
4+
export { callServer } from 'next/dist/client/app-call-server'
55

66
// A noop wrapper to let the Flight client create the server reference.
77
// See also: https://github.com/facebook/react/pull/26632
8-
export function createServerReference(id: string) {
9-
// Since we're using the Edge build of Flight client for SSR [1], here we need to
10-
// also use the same Edge build to create the reference. For the client bundle,
11-
// we use the default and let Webpack to resolve it to the correct version.
12-
// 1: https://github.com/vercel/next.js/blob/16eb80b0b0be13f04a6407943664b5efd8f3d7d0/packages/next/src/server/app-render/use-flight-response.tsx#L24-L26
13-
const { createServerReference: createServerReferenceImpl } = (
14-
!!process.env.NEXT_RUNTIME
15-
? // eslint-disable-next-line import/no-extraneous-dependencies
16-
require('react-server-dom-webpack/client.edge')
17-
: // eslint-disable-next-line import/no-extraneous-dependencies
18-
require('react-server-dom-webpack/client')
19-
) as typeof import('react-server-dom-webpack/client')
8+
// Since we're using the Edge build of Flight client for SSR [1], here we need to
9+
// also use the same Edge build to create the reference. For the client bundle,
10+
// we use the default and let Webpack to resolve it to the correct version.
11+
// 1: https://github.com/vercel/next.js/blob/16eb80b0b0be13f04a6407943664b5efd8f3d7d0/packages/next/src/server/app-render/use-flight-response.tsx#L24-L26
12+
export const createServerReference = (
13+
(!!process.env.NEXT_RUNTIME
14+
? // eslint-disable-next-line import/no-extraneous-dependencies
15+
require('react-server-dom-webpack/client.edge')
16+
: // eslint-disable-next-line import/no-extraneous-dependencies
17+
require('react-server-dom-webpack/client')) as typeof import('react-server-dom-webpack/client')
18+
).createServerReference
2019

21-
return createServerReferenceImpl(id, callServer)
22-
}
20+
export const findSourceMapURL = undefined

packages/next/src/build/webpack/loaders/next-flight-server-reference-proxy-loader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ const flightServerReferenceProxyLoader: webpack.LoaderDefinitionFunction<{
1818
// Because of that, Webpack is able to concatenate the modules and inline the
1919
// reference IDs recursively directly into the module that uses them.
2020
return `\
21-
import { createServerReference } from 'private-next-rsc-action-client-wrapper'
21+
import { createServerReference, callServer, findSourceMapURL } from 'private-next-rsc-action-client-wrapper'
2222
export ${
2323
name === 'default' ? 'default' : `const ${name} =`
24-
} /*#__PURE__*/createServerReference(${JSON.stringify(id)})`
24+
} /*#__PURE__*/createServerReference(${JSON.stringify(id)}, callServer, undefined, findSourceMapURL, ${JSON.stringify(name)})`
2525
}
2626

2727
export default flightServerReferenceProxyLoader

0 commit comments

Comments
 (0)