From 78c050c0939bb3f8ab058e789b5f15663264f9f8 Mon Sep 17 00:00:00 2001 From: Abanoub Ghadban Date: Mon, 30 Sep 2024 10:55:55 +0300 Subject: [PATCH] make consoleHistory argument of consoleReplay optional --- node_package/src/ReactOnRails.ts | 2 +- node_package/src/buildConsoleReplay.ts | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/node_package/src/ReactOnRails.ts b/node_package/src/ReactOnRails.ts index d9bb16095..440ab3784 100644 --- a/node_package/src/ReactOnRails.ts +++ b/node_package/src/ReactOnRails.ts @@ -257,7 +257,7 @@ ctx.ReactOnRails = { * Used by Rails server rendering to replay console messages. */ buildConsoleReplay(): string { - return buildConsoleReplay(console.history); + return buildConsoleReplay(); }, /** diff --git a/node_package/src/buildConsoleReplay.ts b/node_package/src/buildConsoleReplay.ts index 2ab875b58..369efb81b 100644 --- a/node_package/src/buildConsoleReplay.ts +++ b/node_package/src/buildConsoleReplay.ts @@ -11,7 +11,9 @@ declare global { } } -export function consoleReplay(consoleHistory: typeof console['history']): string { +export function consoleReplay(customConsoleHistory: typeof console['history'] | undefined = undefined): string { + const consoleHistory = customConsoleHistory ?? console.history; + // console.history is a global polyfill used in server rendering. // Must use Array.isArray instead of instanceof Array the history array is defined outside the vm if node renderer is used. // 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. @@ -41,6 +43,6 @@ export function consoleReplay(consoleHistory: typeof console['history']): string return lines.join('\n'); } -export default function buildConsoleReplay(consoleHistory: typeof console['history']): string { - return RenderUtils.wrapInScriptTags('consoleReplayLog', consoleReplay(consoleHistory)); +export default function buildConsoleReplay(customConsoleHistory: typeof console['history'] | undefined = undefined): string { + return RenderUtils.wrapInScriptTags('consoleReplayLog', consoleReplay(customConsoleHistory)); }