Skip to content

Commit 709d7c3

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 709d7c3

File tree

4 files changed

+56
-44
lines changed

4 files changed

+56
-44
lines changed

src/React.AspNet/HtmlHelperExtensions.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@
55
* This source code is licensed under the BSD-style license found in the
66
* LICENSE file in the root directory of this source tree. An additional grant
77
* of patent rights can be found in the PATENTS file in the same directory.
8-
*/
9-
8+
*/
9+
1010
using React.Exceptions;
11-
using React.TinyIoC;
12-
11+
using React.TinyIoC;
12+
1313
#if LEGACYASPNET
1414
using System.Web;
1515
using System.Web.Mvc;
16-
using IHtmlHelper = System.Web.Mvc.HtmlHelper;
16+
using IHtmlHelper = System.Web.Mvc.HtmlHelper;
1717
#else
1818
using Microsoft.AspNet.Mvc.Rendering;
1919
using IHtmlString = Microsoft.AspNet.Html.Abstractions.IHtmlContent;
2020
#endif
21-
21+
2222
#if LEGACYASPNET
23-
namespace React.Web.Mvc
23+
namespace React.Web.Mvc
2424
#else
2525
namespace React.AspNet
2626
#endif
27-
{
27+
{
2828
/// <summary>
2929
/// HTML Helpers for utilising React from an ASP.NET MVC application.
3030
/// </summary>
@@ -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: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
* This source code is licensed under the BSD-style license found in the
66
* LICENSE file in the root directory of this source tree. An additional grant
77
* of patent rights can be found in the PATENTS file in the same directory.
8-
*/
9-
10-
using System;
11-
8+
*/
9+
10+
1211
namespace React
13-
{
12+
{
1413
/// <summary>
1514
/// Request-specific ReactJS.NET environment. This is unique to the individual request and is
1615
/// not shared.
@@ -71,24 +70,26 @@ public interface IReactEnvironment
7170
/// </summary>
7271
/// <param name="name">Name of the variable</param>
7372
/// <returns><c>true</c> if the variable exists; <c>false</c> otherwise</returns>
74-
bool HasVariable(string name);
75-
73+
bool HasVariable(string name);
74+
7675
/// <summary>
7776
/// Creates an instance of the specified React JavaScript component.
7877
/// </summary>
7978
/// <typeparam name="T">Type of the props</typeparam>
8079
/// <param name="componentName">Name of the component</param>
8180
/// <param name="props">Props to use</param>
8281
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
82+
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
8383
/// <returns>The component</returns>
84-
IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null);
85-
84+
IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false);
85+
8686
/// <summary>
8787
/// Renders the JavaScript required to initialise all components client-side. This will
8888
/// attach event handlers to the server-rendered HTML.
8989
/// </summary>
90+
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
9091
/// <returns>JavaScript for all components</returns>
91-
string GetInitJavaScript();
92+
string GetInitJavaScript(bool clientOnly = false);
9293

9394
/// <summary>
9495
/// Gets the JSX Transformer for this environment.

src/React.Core/ReactComponent.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* This source code is licensed under the BSD-style license found in the
66
* LICENSE file in the root directory of this source tree. An additional grant
77
* of patent rights can be found in the PATENTS file in the same directory.
8-
*/
9-
8+
*/
9+
1010
using System;
1111
using System.Linq;
1212
using System.Text.RegularExpressions;
@@ -15,7 +15,7 @@
1515
using React.Exceptions;
1616

1717
namespace React
18-
{
18+
{
1919
/// <summary>
2020
/// Represents a React JavaScript component.
2121
/// </summary>
@@ -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: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@
55
* This source code is licensed under the BSD-style license found in the
66
* LICENSE file in the root directory of this source tree. An additional grant
77
* of patent rights can be found in the PATENTS file in the same directory.
8-
*/
9-
8+
*/
9+
1010
using System;
1111
using System.Collections.Generic;
1212
using System.Diagnostics;
1313
using System.Reflection;
1414
using System.Text;
1515
using System.Threading;
1616
using JavaScriptEngineSwitcher.Core;
17-
using JavaScriptEngineSwitcher.Core.Helpers;
18-
using Newtonsoft.Json;
1917
using React.Exceptions;
2018

2119
namespace React
22-
{
20+
{
2321
/// <summary>
2422
/// Request-specific ReactJS.NET environment. This is unique to the individual request and is
2523
/// not shared.
@@ -247,39 +245,48 @@ public virtual bool HasVariable(string name)
247245
{
248246
throw WrapJavaScriptRuntimeException(ex);
249247
}
250-
}
251-
248+
}
249+
252250
/// <summary>
253251
/// Creates an instance of the specified React JavaScript component.
254252
/// </summary>
255253
/// <typeparam name="T">Type of the props</typeparam>
256254
/// <param name="componentName">Name of the component</param>
257255
/// <param name="props">Props to use</param>
258256
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
257+
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
259258
/// <returns>The component</returns>
260-
public virtual IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null)
261-
{
262-
EnsureUserScriptsLoaded();
259+
public virtual IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false)
260+
{
261+
if (!clientOnly)
262+
{
263+
EnsureUserScriptsLoaded();
264+
}
265+
263266
var component = new ReactComponent(this, _config, componentName, containerId)
264267
{
265268
Props = props
266269
};
267270
_components.Add(component);
268271
return component;
269-
}
270-
272+
}
273+
271274
/// <summary>
272275
/// Renders the JavaScript required to initialise all components client-side. This will
273276
/// attach event handlers to the server-rendered HTML.
274277
/// </summary>
278+
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
275279
/// <returns>JavaScript for all components</returns>
276-
public virtual string GetInitJavaScript()
280+
public virtual string GetInitJavaScript(bool clientOnly = false)
277281
{
278-
var fullScript = new StringBuilder();
279-
280-
// Propagate any server-side console.log calls to corresponding client-side calls.
281-
var consoleCalls = Execute<string>("console.getCalls()");
282-
fullScript.Append(consoleCalls);
282+
var fullScript = new StringBuilder();
283+
284+
// Propagate any server-side console.log calls to corresponding client-side calls.
285+
if (!clientOnly)
286+
{
287+
var consoleCalls = Execute<string>("console.getCalls()");
288+
fullScript.Append(consoleCalls);
289+
}
283290

284291
foreach (var component in _components)
285292
{

0 commit comments

Comments
 (0)