Unverified Commit 8e723ede authored by Shu Ding's avatar Shu Ding Committed by GitHub

Change Server Reference creation on client (#48824)

Mostly mirrors the changed made in
https://github.com/facebook/react/pull/26632 to our SWC transform. The
implementation difference is that the AST transformer only adds a
general purpose wrapper call `createServerReference(id)` from an aliased
import, so we can easily change the underlying function in the bundler.
This change only affects the client layer (when `self.config.is_server
=== false`).

Needs to be landed after another React upgrade:
https://github.com/vercel/next.js/pull/48697.

cc @sebmarkbage.
parent fafecb84
// app/send.ts // app/send.ts
/* __next_internal_action_entry_do_not_use__ myAction,default */ export async function myAction(...args) { /* __next_internal_action_entry_do_not_use__ myAction,default */ import createServerReference from "private-next-rsc-action-client-wrapper";
return __build_action__(myAction, args); export const myAction = createServerReference("e10665baac148856374b2789aceb970f66fec33e");
} export default createServerReference("c18c215a6b7cdc64bf709f3a714ffdef1bf9651d");
export default async function $$ACTION_0(...args) {
return __build_action__($$ACTION_0, args);
};
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
myAction,
$$ACTION_0
]);
myAction.$$typeof = Symbol.for("react.server.reference");
myAction.$$id = "e10665baac148856374b2789aceb970f66fec33e";
myAction.$$bound = [];
myAction.$$with_bound = false;
$$ACTION_0.$$typeof = Symbol.for("react.server.reference");
$$ACTION_0.$$id = "c18c215a6b7cdc64bf709f3a714ffdef1bf9651d";
$$ACTION_0.$$bound = [];
$$ACTION_0.$$with_bound = false;
// app/send.ts // app/send.ts
/* __next_internal_action_entry_do_not_use__ foo */ export async function foo(...args) { /* __next_internal_action_entry_do_not_use__ foo */ import createServerReference from "private-next-rsc-action-client-wrapper";
return __build_action__(foo, args); export const foo = createServerReference("ab21efdafbe611287bc25c0462b1e0510d13e48b");
}
import ensureServerEntryExports from "private-next-rsc-action-proxy";
ensureServerEntryExports([
foo
]);
foo.$$typeof = Symbol.for("react.server.reference");
foo.$$id = "ab21efdafbe611287bc25c0462b1e0510d13e48b";
foo.$$bound = [];
foo.$$with_bound = false;
declare module 'next/dist/compiled/react-server-dom-webpack/client' declare module 'next/dist/compiled/react-server-dom-webpack/client'
declare module 'next/dist/client/app-call-server'
...@@ -12,6 +12,7 @@ import { ...@@ -12,6 +12,7 @@ import {
APP_DIR_ALIAS, APP_DIR_ALIAS,
WEBPACK_LAYERS, WEBPACK_LAYERS,
RSC_ACTION_PROXY_ALIAS, RSC_ACTION_PROXY_ALIAS,
RSC_ACTION_CLIENT_WRAPPER_ALIAS,
} from '../lib/constants' } from '../lib/constants'
import { fileExists } from '../lib/file-exists' import { fileExists } from '../lib/file-exists'
import { CustomRoutes } from '../lib/load-custom-routes.js' import { CustomRoutes } from '../lib/load-custom-routes.js'
...@@ -1058,6 +1059,9 @@ export default async function getBaseWebpackConfig( ...@@ -1058,6 +1059,9 @@ export default async function getBaseWebpackConfig(
[RSC_ACTION_PROXY_ALIAS]: [RSC_ACTION_PROXY_ALIAS]:
'next/dist/build/webpack/loaders/next-flight-loader/action-proxy', 'next/dist/build/webpack/loaders/next-flight-loader/action-proxy',
[RSC_ACTION_CLIENT_WRAPPER_ALIAS]:
'next/dist/build/webpack/loaders/next-flight-loader/action-client-wrapper',
...(isClient || isEdgeServer ...(isClient || isEdgeServer
? { ? {
[clientResolveRewrites]: hasRewrites [clientResolveRewrites]: hasRewrites
...@@ -1263,7 +1267,7 @@ export default async function getBaseWebpackConfig( ...@@ -1263,7 +1267,7 @@ export default async function getBaseWebpackConfig(
} }
const notExternalModules = const notExternalModules =
/^(?:private-next-pages\/|next\/(?:dist\/pages\/|(?:app|document|link|image|legacy\/image|constants|dynamic|script|navigation|headers)$)|string-hash|private-next-rsc-action-proxy$)/ /^(?:private-next-pages\/|next\/(?:dist\/pages\/|(?:app|document|link|image|legacy\/image|constants|dynamic|script|navigation|headers)$)|string-hash|private-next-rsc-action-proxy|private-next-rsc-action-client-wrapper$)/
if (notExternalModules.test(request)) { if (notExternalModules.test(request)) {
return return
} }
......
// This file must be bundled in the app's client layer.
import { createServerReference } from 'next/dist/compiled/react-server-dom-webpack/client'
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) {
return createServerReference(id, callServer)
}
...@@ -25,6 +25,8 @@ export const ROOT_DIR_ALIAS = 'private-next-root-dir' ...@@ -25,6 +25,8 @@ export const ROOT_DIR_ALIAS = 'private-next-root-dir'
export const APP_DIR_ALIAS = 'private-next-app-dir' export const APP_DIR_ALIAS = 'private-next-app-dir'
export const RSC_MOD_REF_PROXY_ALIAS = 'private-next-rsc-mod-ref-proxy' export const RSC_MOD_REF_PROXY_ALIAS = 'private-next-rsc-mod-ref-proxy'
export const RSC_ACTION_PROXY_ALIAS = 'private-next-rsc-action-proxy' export const RSC_ACTION_PROXY_ALIAS = 'private-next-rsc-action-proxy'
export const RSC_ACTION_CLIENT_WRAPPER_ALIAS =
'private-next-rsc-action-client-wrapper'
export const PUBLIC_DIR_MIDDLEWARE_CONFLICT = `You can not have a '_next' folder inside of your public folder. This conflicts with the internal '/_next' route. https://nextjs.org/docs/messages/public-next-folder-conflict` export const PUBLIC_DIR_MIDDLEWARE_CONFLICT = `You can not have a '_next' folder inside of your public folder. This conflicts with the internal '/_next' route. https://nextjs.org/docs/messages/public-next-folder-conflict`
......
...@@ -15,6 +15,7 @@ declare module 'next/dist/compiled/react-server-dom-webpack/client.edge' ...@@ -15,6 +15,7 @@ declare module 'next/dist/compiled/react-server-dom-webpack/client.edge'
declare module 'next/dist/compiled/react-server-dom-webpack/client.browser' declare module 'next/dist/compiled/react-server-dom-webpack/client.browser'
declare module 'next/dist/compiled/react-server-dom-webpack/server.browser' declare module 'next/dist/compiled/react-server-dom-webpack/server.browser'
declare module 'next/dist/compiled/react-server-dom-webpack/server.edge' declare module 'next/dist/compiled/react-server-dom-webpack/server.edge'
declare module 'next/dist/client/app-call-server'
declare module 'next/dist/compiled/react-dom/server' declare module 'next/dist/compiled/react-dom/server'
declare module 'next/dist/compiled/react-dom/server.edge' declare module 'next/dist/compiled/react-dom/server.edge'
declare module 'next/dist/compiled/react-dom/server.browser' declare module 'next/dist/compiled/react-dom/server.browser'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment