Handle async module for client components (#39953)
### Problem
esm modules imports from client components will be compiled to `m = import('module-name')` when webpack bundles them for server components flight rendering. In this case, they will all become async modules since dyanmic imports will return a promise which react flight cannot handle it then results into module resolving error on server flight rendering.
### Solution
* React flight renderer supports handling async modules in https://github.com/facebook/react/pull/25138
* On next.js side leverage the module proxy change for each client reference, to make sure it always resolve the correct client module
The idea is wrapping each module with a module proxy, and if the module is async and accessed as thenable, it will return a new module reference with `async` label to tell react to handle it as async modules:
exported client reference `*` --> not async module (non thenable) --> original module reference `''`
exported client reference `*` --> it's async module (thenable) --> wrapped module reference `'*'` with `async` label
### Note
Since we need to check if user having incorrect gSSP/gSP specifying in layout client componet, so we still need to parse it and assign those info to the proxy (Does client module containing `ssr`, `ssg` exports). Otherwise the proxy will return the cached module reference
Showing
| ... | @@ -177,8 +177,8 @@ | ... | @@ -177,8 +177,8 @@ |
| "react-17": "npm:react@17.0.2", | "react-17": "npm:react@17.0.2", | ||
| "react-dom": "18.2.0", | "react-dom": "18.2.0", | ||
| "react-dom-17": "npm:react-dom@17.0.2", | "react-dom-17": "npm:react-dom@17.0.2", | ||
| "react-dom-exp": "npm:react-dom@0.0.0-experimental-6ef466c68-20220816", | "react-dom-exp": "npm:react-dom@0.0.0-experimental-0de3ddf56-20220825", | ||
| "react-exp": "npm:react@0.0.0-experimental-6ef466c68-20220816", | "react-exp": "npm:react@0.0.0-experimental-0de3ddf56-20220825", | ||
| "react-ssr-prepass": "1.0.8", | "react-ssr-prepass": "1.0.8", | ||
| "react-virtualized": "9.22.3", | "react-virtualized": "9.22.3", | ||
| "relay-compiler": "13.0.2", | "relay-compiler": "13.0.2", | ||
| ... | ... |
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Please
register
or
sign in
to comment