Skip to content

Commit e931f4e

Browse files
author
Dustin Masters
committed
Allow Javascript engines to be bypassed entirely.
If no server-side rendering will occur on a page, for instance if component render is called with clientOnly: true, it is pointless to initialize a Javascript engine on the server. This allows the consumer to easily disable all server side Javascript if the server is under heavy load.
1 parent fe79d81 commit e931f4e

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

src/React.AspNet/HtmlHelperExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static IHtmlString React<T>(
8181
string containerClass = null
8282
)
8383
{
84-
var reactComponent = Environment.CreateComponent(componentName, props, containerId);
84+
var reactComponent = Environment.CreateComponent(componentName, props, containerId, clientOnly);
8585
if (!string.IsNullOrEmpty(htmlTag))
8686
{
8787
reactComponent.ContainerTag = htmlTag;
@@ -118,7 +118,7 @@ public static IHtmlString ReactWithInit<T>(
118118
string containerClass = null
119119
)
120120
{
121-
var reactComponent = Environment.CreateComponent(componentName, props, containerId);
121+
var reactComponent = Environment.CreateComponent(componentName, props, containerId, clientOnly);
122122
if (!string.IsNullOrEmpty(htmlTag))
123123
{
124124
reactComponent.ContainerTag = htmlTag;
@@ -146,9 +146,9 @@ public static IHtmlString ReactWithInit<T>(
146146
/// attach event handlers to the server-rendered HTML.
147147
/// </summary>
148148
/// <returns>JavaScript for all components</returns>
149-
public static IHtmlString ReactInitJavaScript(this IHtmlHelper htmlHelper)
149+
public static IHtmlString ReactInitJavaScript(this IHtmlHelper htmlHelper, bool clientOnly = false)
150150
{
151-
var script = Environment.GetInitJavaScript();
151+
var script = Environment.GetInitJavaScript(clientOnly);
152152
#if LEGACYASPNET
153153
var tag = new TagBuilder("script")
154154
{

src/React.Core/IReactEnvironment.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,17 @@ public interface IReactEnvironment
8080
/// <param name="componentName">Name of the component</param>
8181
/// <param name="props">Props to use</param>
8282
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
83+
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
8384
/// <returns>The component</returns>
84-
IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null);
85+
IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false);
8586

8687
/// <summary>
8788
/// Renders the JavaScript required to initialise all components client-side. This will
8889
/// attach event handlers to the server-rendered HTML.
8990
/// </summary>
91+
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
9092
/// <returns>JavaScript for all components</returns>
91-
string GetInitJavaScript();
93+
string GetInitJavaScript(bool clientOnly = false);
9294

9395
/// <summary>
9496
/// Gets the JSX Transformer for this environment.

src/React.Core/ReactComponent.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ public ReactComponent(IReactEnvironment environment, IReactSiteConfiguration con
8989
/// <returns>HTML</returns>
9090
public virtual string RenderHtml(bool renderContainerOnly = false, bool renderServerOnly = false)
9191
{
92-
EnsureComponentExists();
92+
if (!renderContainerOnly)
93+
{
94+
EnsureComponentExists();
95+
}
96+
9397
try
9498
{
9599
var html = string.Empty;

src/React.Core/ReactEnvironment.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,15 @@ public virtual bool HasVariable(string name)
256256
/// <param name="componentName">Name of the component</param>
257257
/// <param name="props">Props to use</param>
258258
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
259+
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
259260
/// <returns>The component</returns>
260-
public virtual IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null)
261+
public virtual IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false)
261262
{
262-
EnsureUserScriptsLoaded();
263+
if (!clientOnly)
264+
{
265+
EnsureUserScriptsLoaded();
266+
}
267+
263268
var component = new ReactComponent(this, _config, componentName, containerId)
264269
{
265270
Props = props
@@ -272,14 +277,18 @@ public virtual IReactComponent CreateComponent<T>(string componentName, T props,
272277
/// Renders the JavaScript required to initialise all components client-side. This will
273278
/// attach event handlers to the server-rendered HTML.
274279
/// </summary>
280+
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
275281
/// <returns>JavaScript for all components</returns>
276-
public virtual string GetInitJavaScript()
282+
public virtual string GetInitJavaScript(bool clientOnly = false)
277283
{
278284
var fullScript = new StringBuilder();
279285

280286
// Propagate any server-side console.log calls to corresponding client-side calls.
281-
var consoleCalls = Execute<string>("console.getCalls()");
282-
fullScript.Append(consoleCalls);
287+
if (!clientOnly)
288+
{
289+
var consoleCalls = Execute<string>("console.getCalls()");
290+
fullScript.Append(consoleCalls);
291+
}
283292

284293
foreach (var component in _components)
285294
{

src/React.Tests/Mvc/HtmlHelperExtensionsTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public void ReactWithInitShouldReturnHtmlAndScript()
3939
environment.Setup(x => x.CreateComponent(
4040
"ComponentName",
4141
new {},
42-
null
42+
null,
43+
false
4344
)).Returns(component.Object);
4445

4546
var result = HtmlHelperExtensions.ReactWithInit(
@@ -64,7 +65,8 @@ public void ReactWithClientOnlyTrueShouldCallRenderHtmlWithTrue()
6465
environment.Setup(x => x.CreateComponent(
6566
"ComponentName",
6667
new {},
67-
null
68+
null,
69+
true
6870
)).Returns(component.Object);
6971

7072
var result = HtmlHelperExtensions.React(
@@ -86,7 +88,8 @@ public void ReactWithServerOnlyTrueShouldCallRenderHtmlWithTrue() {
8688
environment.Setup(x => x.CreateComponent(
8789
"ComponentName",
8890
new { },
89-
null
91+
null,
92+
true
9093
)).Returns(component.Object);
9194

9295
var result = HtmlHelperExtensions.React(

0 commit comments

Comments
 (0)