[Flight] Support Async Modules in Server References (#31313)
This is required to support for example top level await in a "use server" module or dependency of a "use server".
Sebastian Markbåge committed
Oct 21, 2024 at 14:52 UTC
69d4b800a6c31561bd928eef4a4592fdb38471cb
1 file changed
+20
-1
packages/react-server-dom-webpack/src/client/ReactFlightClientConfigBundlerWebpack.js
+20
-1
@@ -86,6 +86,13 @@ export function resolveClientReference<T>(
86
}
87
name = metadata[NAME];
88
}
89
+ // Note that resolvedModuleData.async may be set if this is an Async Module.
90
+ // For Client References we don't actually care because what matters is whether
91
+ // the consumer expects an unwrapped async module or just a raw Promise so it
92
+ // has to already know which one it wants.
93
+ // We could error if this is an Async Import but it's not an Async Module.
94
+ // However, we also support plain CJS exporting a top level Promise which is not
95
+ // an Async Module according to the bundle graph but is effectively the same.
96
if (isAsyncImport(metadata)) {
97
return [
98
resolvedModuleData.id,
@@ -128,7 +135,19 @@ export function resolveServerReference<T>(
135
);
136
}
137
}
131
- // TODO: This needs to return async: true if it's an async module.
138
+ if (resolvedModuleData.async) {
139
+ // If the module is marked as async in a Client Reference, we don't actually care.
140
+ // What matters is whether the consumer wants to unwrap it or not.
141
+ // For Server References, it is different because the consumer is completely internal
142
+ // to the bundler. So instead of passing it to each reference we can mark it in the
143
+ // manifest.
144
+ return [
145
+ resolvedModuleData.id,
146
+ resolvedModuleData.chunks,
147
+ name,
148
+ 1 /* async */,
149
+ ];
150
+ }
151
return [resolvedModuleData.id, resolvedModuleData.chunks, name];
152
}
153