Skip to content

Commit 69d4b80

Browse files
authored
[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".
1 parent 65a56d0 commit 69d4b80

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

packages/react-server-dom-webpack/src/client/ReactFlightClientConfigBundlerWebpack.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ export function resolveClientReference<T>(
8686
}
8787
name = metadata[NAME];
8888
}
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.
8996
if (isAsyncImport(metadata)) {
9097
return [
9198
resolvedModuleData.id,
@@ -128,7 +135,19 @@ export function resolveServerReference<T>(
128135
);
129136
}
130137
}
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+
}
132151
return [resolvedModuleData.id, resolvedModuleData.chunks, name];
133152
}
134153

0 commit comments

Comments
 (0)