@@ -98,6 +98,7 @@ as a renderFunction and not a simple React Function Component.`);
9898 renderingError = e ;
9999 }
100100
101+ const consoleHistoryAfterSyncExecution = console . history ;
101102 const addRenderingErrors = ( resultObject : RenderResult , renderError : RenderingError ) => {
102103 resultObject . renderingError = { // eslint-disable-line no-param-reassign
103104 message : renderError . message ,
@@ -110,9 +111,17 @@ as a renderFunction and not a simple React Function Component.`);
110111 let promiseResult ;
111112
112113 try {
114+ const awaitedRenderResult = await renderResult ;
115+ const consoleHistoryAfterAsyncExecution = console . history ;
116+ let consoleReplayScript = '' ;
117+ if ( ( consoleHistoryAfterAsyncExecution ?. length ?? 0 ) > ( consoleHistoryAfterSyncExecution ?. length ?? 0 ) ) {
118+ consoleReplayScript = buildConsoleReplay ( consoleHistoryAfterAsyncExecution ) ;
119+ } else {
120+ consoleReplayScript = buildConsoleReplay ( consoleHistoryAfterSyncExecution ) ;
121+ }
113122 promiseResult = {
114- html : await renderResult ,
115- consoleReplayScript : buildConsoleReplay ( ) ,
123+ html : awaitedRenderResult ,
124+ consoleReplayScript,
116125 hasErrors,
117126 } ;
118127 } catch ( e : any ) {
@@ -125,7 +134,7 @@ as a renderFunction and not a simple React Function Component.`);
125134 name,
126135 serverSide : true ,
127136 } ) ,
128- consoleReplayScript : buildConsoleReplay ( ) ,
137+ consoleReplayScript : buildConsoleReplay ( consoleHistoryAfterSyncExecution ) ,
129138 hasErrors : true ,
130139 }
131140 renderingError = e ;
@@ -143,7 +152,7 @@ as a renderFunction and not a simple React Function Component.`);
143152
144153 const result = {
145154 html : renderResult ,
146- consoleReplayScript : buildConsoleReplay ( ) ,
155+ consoleReplayScript : buildConsoleReplay ( consoleHistoryAfterSyncExecution ) ,
147156 hasErrors,
148157 } as RenderResult ;
149158
@@ -161,11 +170,9 @@ const serverRenderReactComponent: typeof serverRenderReactComponentInternal = (o
161170 } finally {
162171 // Reset console history after each render.
163172 // See `RubyEmbeddedJavaScript.console_polyfill` for initialization.
164- if ( result && isPromise ( result ) ) {
165- result . finally ( ( ) => {
166- console . history = [ ] ;
167- } ) ;
168- } else {
173+ // We don't need to clear the console history if the result is a promise
174+ // Promises only supported in node renderer and node renderer takes care of cleanining console history
175+ if ( typeof result === 'string' ) {
169176 console . history = [ ] ;
170177 }
171178 }
0 commit comments