Skip to content

Commit 6d1bfca

Browse files
make consoleReplay function accept the list of logs to be added to the script
1 parent 71daa00 commit 6d1bfca

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

node_package/src/ReactOnRails.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ ctx.ReactOnRails = {
257257
* Used by Rails server rendering to replay console messages.
258258
*/
259259
buildConsoleReplay(): string {
260-
return buildConsoleReplay();
260+
return buildConsoleReplay(console.history);
261261
},
262262

263263
/**

node_package/src/buildConsoleReplay.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ declare global {
1111
}
1212
}
1313

14-
export function consoleReplay(): string {
14+
export function consoleReplay(consoleHistory: typeof console['history']): string {
1515
// console.history is a global polyfill used in server rendering.
1616
// Must use Array.isArray instead of instanceof Array the history array is defined outside the vm if node renderer is used.
1717
// In this case, the Array prototype used to define the array is not the same as the one in the global scope inside the vm.
1818
// $FlowFixMe
19-
if (!(Array.isArray(console.history))) {
19+
if (!(Array.isArray(consoleHistory))) {
2020
return '';
2121
}
2222

23-
const lines = console.history.map(msg => {
23+
const lines = consoleHistory.map(msg => {
2424
const stringifiedList = msg.arguments.map(arg => {
2525
let val;
2626
try {
@@ -41,6 +41,6 @@ export function consoleReplay(): string {
4141
return lines.join('\n');
4242
}
4343

44-
export default function buildConsoleReplay(): string {
45-
return RenderUtils.wrapInScriptTags('consoleReplayLog', consoleReplay());
44+
export default function buildConsoleReplay(consoleHistory: typeof console['history']): string {
45+
return RenderUtils.wrapInScriptTags('consoleReplayLog', consoleReplay(consoleHistory));
4646
}

node_package/src/serverRenderReactComponent.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)