@@ -1418,6 +1418,95 @@ describe('ReactFlightDOM', () => {
14181418 expect ( reportedErrors ) . toEqual ( [ ] ) ;
14191419 } ) ;
14201420
1421+ it ( 'should not retain stale error reason after reentrant module chunk initialization' , async ( ) => {
1422+ function MyComponent ( ) {
1423+ return < div > hello from client component</ div > ;
1424+ }
1425+ const ClientComponent = clientExports ( MyComponent ) ;
1426+
1427+ let resolveAsyncComponent ;
1428+ async function AsyncComponent ( ) {
1429+ await new Promise ( r => {
1430+ resolveAsyncComponent = r ;
1431+ } ) ;
1432+ return null ;
1433+ }
1434+
1435+ function ServerComponent ( ) {
1436+ return (
1437+ < >
1438+ < ClientComponent />
1439+ < Suspense >
1440+ < AsyncComponent />
1441+ </ Suspense >
1442+ </ >
1443+ ) ;
1444+ }
1445+
1446+ const { writable : flightWritable , readable : flightReadable } =
1447+ getTestStream ( ) ;
1448+ const { writable : fizzWritable , readable : fizzReadable } = getTestStream ( ) ;
1449+
1450+ const { pipe} = await serverAct ( ( ) =>
1451+ ReactServerDOMServer . renderToPipeableStream (
1452+ < ServerComponent /> ,
1453+ webpackMap ,
1454+ ) ,
1455+ ) ;
1456+ pipe ( flightWritable ) ;
1457+
1458+ let response = null ;
1459+ function getResponse ( ) {
1460+ if ( response === null ) {
1461+ response =
1462+ ReactServerDOMClient . createFromReadableStream ( flightReadable ) ;
1463+ }
1464+ return response ;
1465+ }
1466+
1467+ // Simulate a module that calls captureOwnerStack() during evaluation.
1468+ // In Fizz SSR, this causes a reentrant readChunk on the same module chunk.
1469+ // The reentrant require throws a TDZ error.
1470+ let evaluatingModuleId = null ;
1471+ const origRequire = global . __webpack_require__ ;
1472+ global . __webpack_require__ = function ( id ) {
1473+ if ( id === evaluatingModuleId ) {
1474+ throw new ReferenceError (
1475+ "Cannot access 'MyComponent' before initialization" ,
1476+ ) ;
1477+ }
1478+ const result = origRequire ( id ) ;
1479+ if ( result === MyComponent ) {
1480+ evaluatingModuleId = id ;
1481+ if ( __DEV__ ) {
1482+ React . captureOwnerStack ( ) ;
1483+ }
1484+ evaluatingModuleId = null ;
1485+ }
1486+ return result ;
1487+ } ;
1488+
1489+ function App ( ) {
1490+ return use ( getResponse ( ) ) ;
1491+ }
1492+
1493+ await serverAct ( async ( ) => {
1494+ ReactDOMFizzServer . renderToPipeableStream ( < App /> ) . pipe ( fizzWritable ) ;
1495+ } ) ;
1496+
1497+ global . __webpack_require__ = origRequire ;
1498+
1499+ // Resolve the async component so the Flight stream closes after the client
1500+ // module chunk was initialized.
1501+ await serverAct ( async ( ) => {
1502+ resolveAsyncComponent ( ) ;
1503+ } ) ;
1504+
1505+ const container = document . createElement ( 'div' ) ;
1506+ await readInto ( container , fizzReadable ) ;
1507+ expect ( container . innerHTML ) . toContain ( 'hello from client component' ) ;
1508+ } ) ;
1509+
14211510 it ( 'should be able to recover from a direct reference erroring server-side' , async ( ) => {
14221511 const reportedErrors = [ ] ;
14231512
0 commit comments