Skip to content

Allow Javascript engines to be bypassed entirely. #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/React.AspNet/HtmlHelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static IHtmlString React<T>(
string containerClass = null
)
{
var reactComponent = Environment.CreateComponent(componentName, props, containerId);
var reactComponent = Environment.CreateComponent(componentName, props, containerId, clientOnly);
if (!string.IsNullOrEmpty(htmlTag))
{
reactComponent.ContainerTag = htmlTag;
Expand Down Expand Up @@ -118,7 +118,7 @@ public static IHtmlString ReactWithInit<T>(
string containerClass = null
)
{
var reactComponent = Environment.CreateComponent(componentName, props, containerId);
var reactComponent = Environment.CreateComponent(componentName, props, containerId, clientOnly);
if (!string.IsNullOrEmpty(htmlTag))
{
reactComponent.ContainerTag = htmlTag;
Expand Down Expand Up @@ -146,9 +146,9 @@ public static IHtmlString ReactWithInit<T>(
/// attach event handlers to the server-rendered HTML.
/// </summary>
/// <returns>JavaScript for all components</returns>
public static IHtmlString ReactInitJavaScript(this IHtmlHelper htmlHelper)
public static IHtmlString ReactInitJavaScript(this IHtmlHelper htmlHelper, bool clientOnly = false)
{
var script = Environment.GetInitJavaScript();
var script = Environment.GetInitJavaScript(clientOnly);
#if LEGACYASPNET
var tag = new TagBuilder("script")
{
Expand Down
6 changes: 4 additions & 2 deletions src/React.Core/IReactEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,17 @@ public interface IReactEnvironment
/// <param name="componentName">Name of the component</param>
/// <param name="props">Props to use</param>
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
/// <returns>The component</returns>
IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null);
IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false);

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

/// <summary>
/// Gets the JSX Transformer for this environment.
Expand Down
6 changes: 5 additions & 1 deletion src/React.Core/ReactComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ public ReactComponent(IReactEnvironment environment, IReactSiteConfiguration con
/// <returns>HTML</returns>
public virtual string RenderHtml(bool renderContainerOnly = false, bool renderServerOnly = false)
{
EnsureComponentExists();
if (!renderContainerOnly)
{
EnsureComponentExists();
}

try
{
var html = string.Empty;
Expand Down
19 changes: 14 additions & 5 deletions src/React.Core/ReactEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,15 @@ public virtual bool HasVariable(string name)
/// <param name="componentName">Name of the component</param>
/// <param name="props">Props to use</param>
/// <param name="containerId">ID to use for the container HTML tag. Defaults to an auto-generated ID</param>
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
/// <returns>The component</returns>
public virtual IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null)
public virtual IReactComponent CreateComponent<T>(string componentName, T props, string containerId = null, bool clientOnly = false)
{
EnsureUserScriptsLoaded();
if (!clientOnly)
{
EnsureUserScriptsLoaded();
}

var component = new ReactComponent(this, _config, componentName, containerId)
{
Props = props
Expand All @@ -272,14 +277,18 @@ public virtual IReactComponent CreateComponent<T>(string componentName, T props,
/// Renders the JavaScript required to initialise all components client-side. This will
/// attach event handlers to the server-rendered HTML.
/// </summary>
/// <param name="clientOnly">True if server-side rendering will be bypassed. Defaults to false.</param>
/// <returns>JavaScript for all components</returns>
public virtual string GetInitJavaScript()
public virtual string GetInitJavaScript(bool clientOnly = false)
{
var fullScript = new StringBuilder();

// Propagate any server-side console.log calls to corresponding client-side calls.
var consoleCalls = Execute<string>("console.getCalls()");
fullScript.Append(consoleCalls);
if (!clientOnly)
{
var consoleCalls = Execute<string>("console.getCalls()");
fullScript.Append(consoleCalls);
}

foreach (var component in _components)
{
Expand Down
9 changes: 6 additions & 3 deletions src/React.Tests/Mvc/HtmlHelperExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public void ReactWithInitShouldReturnHtmlAndScript()
environment.Setup(x => x.CreateComponent(
"ComponentName",
new {},
null
null,
false
)).Returns(component.Object);

var result = HtmlHelperExtensions.ReactWithInit(
Expand All @@ -64,7 +65,8 @@ public void ReactWithClientOnlyTrueShouldCallRenderHtmlWithTrue()
environment.Setup(x => x.CreateComponent(
"ComponentName",
new {},
null
null,
true
)).Returns(component.Object);

var result = HtmlHelperExtensions.React(
Expand All @@ -86,7 +88,8 @@ public void ReactWithServerOnlyTrueShouldCallRenderHtmlWithTrue() {
environment.Setup(x => x.CreateComponent(
"ComponentName",
new { },
null
null,
true
)).Returns(component.Object);

var result = HtmlHelperExtensions.React(
Expand Down