Skip to content

Commit 19d18b1

Browse files
Skip server-side JS if no components were rendered
Fixes #821
1 parent 98f75f1 commit 19d18b1

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/React.Core/ReactEnvironment.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public virtual string GetInitJavaScript(bool clientOnly = false)
332332
public virtual void GetInitJavaScript(TextWriter writer, bool clientOnly = false)
333333
{
334334
// Propagate any server-side console.log calls to corresponding client-side calls.
335-
if (!clientOnly)
335+
if (!clientOnly && _components.Count != 0)
336336
{
337337
var consoleCalls = Execute<string>("console.getCalls()");
338338
writer.Write(consoleCalls);

tests/React.Tests/Core/ReactEnvironmentTest.cs

+18
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ public void ServerSideOnlyComponentRendersNoJavaScript()
136136
Assert.Equal(string.Empty, environment.GetInitJavaScript());
137137
}
138138

139+
[Theory]
140+
[InlineData(false, 0)]
141+
[InlineData(true, 1)]
142+
public void SSRInitSkippedIfNoComponents(bool renderComponent, int ssrTimes)
143+
{
144+
var mocks = new Mocks();
145+
var environment = mocks.CreateReactEnvironment();
146+
147+
if (renderComponent)
148+
{
149+
environment.CreateComponent("HelloWorld", new { name = "Daniel" }, clientOnly: true).RenderHtml();
150+
}
151+
152+
environment.GetInitJavaScript();
153+
154+
mocks.Engine.Verify(x => x.Evaluate<string>("console.getCalls()"), Times.Exactly(ssrTimes));
155+
}
156+
139157
public class Mocks
140158
{
141159
public Mock<PooledJsEngine> Engine { get; private set; }

0 commit comments

Comments
 (0)