Skip to content

Commit

Permalink
Fix default export of server action utility aliases (#54254)
Browse files Browse the repository at this point in the history
Similar to #54004, this PR changes `private-next-rsc-action-validate`
and `private-next-rsc-action-client-wrapper` to export named values
instead of default values. In #54230 (currently broken), the Server
Action file was bundled via Vite/Rollup as an external dependency so
we're not doing the ESM interoperation correctly.

Closes #54230.
  • Loading branch information
shuding authored Aug 20, 2023
1 parent 24cd55c commit a47d92f
Show file tree
Hide file tree
Showing 25 changed files with 38 additions and 30 deletions.
13 changes: 9 additions & 4 deletions packages/next-swc/crates/core/src/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,13 +898,16 @@ impl<C: Comments> VisitMut for ServerActions<C> {
// wrapped by a reference creation call.
let create_ref_ident = private_ident!("createServerReference");
if !self.config.is_server {
// import createServerReference from 'private-next-rsc-action-client-wrapper'
// import { createServerReference } from
// 'private-next-rsc-action-client-wrapper'
// createServerReference("action_id")
new.push(ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
span: DUMMY_SP,
specifiers: vec![ImportSpecifier::Default(ImportDefaultSpecifier {
specifiers: vec![ImportSpecifier::Named(ImportNamedSpecifier {
span: DUMMY_SP,
local: create_ref_ident.clone(),
imported: None,
is_type_only: false,
})],
src: Box::new(Str {
span: DUMMY_SP,
Expand Down Expand Up @@ -986,9 +989,11 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let ensure_ident = private_ident!("ensureServerEntryExports");
new.push(ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
span: DUMMY_SP,
specifiers: vec![ImportSpecifier::Default(ImportDefaultSpecifier {
specifiers: vec![ImportSpecifier::Named(ImportNamedSpecifier {
span: DUMMY_SP,
local: ensure_ident.clone(),
imported: None,
is_type_only: false,
})],
src: Box::new(Str {
span: DUMMY_SP,
Expand Down Expand Up @@ -1056,7 +1061,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
);

// import { createActionProxy } from 'private-next-rsc-action-proxy'
// createServerReference("action_id")
// createActionProxy("action_id")
new.push(ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
span: DUMMY_SP,
specifiers: vec![ImportSpecifier::Named(ImportNamedSpecifier {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* __next_internal_action_entry_do_not_use__ foo */ import { createActionProxy } from "private-next-rsc-action-proxy";
export function foo() {}
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
foo
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* __next_internal_action_entry_do_not_use__ bar */ import { createActionProxy } from "private-next-rsc-action-proxy";
'use strict';
export function bar() {}
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
bar
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* __next_internal_action_entry_do_not_use__ x */ import { createActionProxy } from "private-next-rsc-action-proxy";
export const x = 1;
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
x
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export default class Component {
return null;
}
}
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([]);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* __next_internal_action_entry_do_not_use__ */ import { createActionProxy } from "private-next-rsc-action-proxy";
export * from 'foo';
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([]);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* __next_internal_action_entry_do_not_use__ */ import { createActionProxy } from "private-next-rsc-action-proxy";
export default (()=>{});
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([]);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// app/send.ts
/* __next_internal_action_entry_do_not_use__ myAction,default */ import { createActionProxy } from "private-next-rsc-action-proxy";
import createServerReference from "private-next-rsc-action-client-wrapper";
import { createServerReference } from "private-next-rsc-action-client-wrapper";
export var myAction = createServerReference("e10665baac148856374b2789aceb970f66fec33e");
export default createServerReference("c18c215a6b7cdc64bf709f3a714ffdef1bf9651d");
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// app/send.ts
/* __next_internal_action_entry_do_not_use__ foo */ import { createActionProxy } from "private-next-rsc-action-proxy";
import createServerReference from "private-next-rsc-action-client-wrapper";
import { createServerReference } from "private-next-rsc-action-client-wrapper";
export var foo = createServerReference("ab21efdafbe611287bc25c0462b1e0510d13e48b");
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* __next_internal_action_entry_do_not_use__ sampleFunction,sampleFunction2,sampleFunction3,sampleFunction4 */ import { createActionProxy } from "private-next-rsc-action-proxy";
import createServerReference from "private-next-rsc-action-client-wrapper";
import { createServerReference } from "private-next-rsc-action-client-wrapper";
export var sampleFunction = createServerReference("bd336abe00c3c59da66acb696fc8e151d8e54ea4");
export var sampleFunction2 = createServerReference("a0c73dd6f5af839e3335c6b19262ecb86cca6af4");
export var sampleFunction3 = createServerReference("d4f2e95bc745b6500b439c0847003511748c8ece");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/* __next_internal_action_entry_do_not_use__ bar */ import { createActionProxy } from "private-next-rsc-action-proxy";
import createServerReference from "private-next-rsc-action-client-wrapper";
import { createServerReference } from "private-next-rsc-action-client-wrapper";
export var bar = createServerReference("ac840dcaf5e8197cb02b7f3a43c119b7a770b272");
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* __next_internal_action_entry_do_not_use__ default */ import { createActionProxy } from "private-next-rsc-action-proxy";
export default async function foo() {}
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
foo
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* __next_internal_action_entry_do_not_use__ default */ import { createActionProxy } from "private-next-rsc-action-proxy";
export default async function $$ACTION_0() {}
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
$$ACTION_0
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* __next_internal_action_entry_do_not_use__ default */ import { createActionProxy } from "private-next-rsc-action-proxy";
async function foo() {}
export default foo;
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
foo
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const foo = async function() {};
export default foo;
const bar = async function() {};
export { bar };
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
foo,
bar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export async function foo() {
async function bar() {}
}
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
foo
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default $$ACTION_0 = async (a, b)=>{
console.log(a, b);
};
var $$ACTION_0;
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
$$ACTION_0
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export const foo = async ()=>{};
const bar = async ()=>{};
export { bar };
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
foo,
bar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const [foo] = [
null
];
export default foo;
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
foo
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { validator } from 'auth';
export const action = validator(async ()=>{});
export default $$ACTION_0 = validator(async ()=>{});
var $$ACTION_0;
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
action,
$$ACTION_0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
export async function myAction(a, b, c) {
console.log('a');
}
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
myAction
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function d() {}
function Foo() {
async function e() {}
}
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
a,
b,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ async function bar() {}
export { bar as baz };
async function qux() {}
export { qux as default };
import ensureServerEntryExports from "private-next-rsc-action-validate";
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
foo,
bar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import { 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 default function (id: string) {
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 } = (
const { createServerReference: createServerReferenceImpl } = (
typeof window === 'undefined'
? // 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')

return createServerReference(id, callServer)
return createServerReferenceImpl(id, callServer)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export default function ensureServerEntryExports(actions: any[]) {
// This function ensures that all the exported values are valid server actions,
// during the runtime. By definition all actions are required to be async
// functions, but here we can only check that they are functions.
export function ensureServerEntryExports(actions: any[]) {
for (let i = 0; i < actions.length; i++) {
const action = actions[i]
if (typeof action !== 'function') {
Expand Down

0 comments on commit a47d92f

Please sign in to comment.