Skip to content
Closed
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
5 changes: 5 additions & 0 deletions src/React.Core/JavaScriptEngineFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ private void LoadResource(IJsEngine engine, string resourceName, Assembly assemb
/// <param name="engine">Engine to load scripts into</param>
private void LoadUserScripts(IJsEngine engine)
{
// Do not execute user scripts if rendering is disabled.
if (!_config.UseServerSideRendering)
{
return;
}
foreach (var file in _config.ScriptsWithoutTransform)
{
try
Expand Down
37 changes: 37 additions & 0 deletions tests/React.Tests/Core/JavaScriptEngineFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public void ShouldLoadResourcesWithoutPrecompilation()

var config = new Mock<IReactSiteConfiguration>();
config.Setup(x => x.AllowJavaScriptPrecompilation).Returns(false);
config.Setup(x => x.UseServerSideRendering).Returns(true);
config.Setup(x => x.LoadReact).Returns(true);

var cache = new Mock<ICache>();
Expand All @@ -147,6 +148,7 @@ public void ShouldLoadResourcesWithPrecompilationAndWithoutCache()

var config = new Mock<IReactSiteConfiguration>();
config.Setup(x => x.AllowJavaScriptPrecompilation).Returns(true);
config.Setup(x => x.UseServerSideRendering).Returns(true);
config.Setup(x => x.LoadReact).Returns(true);

var cache = new NullCache();
Expand Down Expand Up @@ -180,6 +182,7 @@ public void ShouldLoadResourcesWithPrecompilationAndEmptyCache()

var config = new Mock<IReactSiteConfiguration>();
config.Setup(x => x.AllowJavaScriptPrecompilation).Returns(true);
config.Setup(x => x.UseServerSideRendering).Returns(true);
config.Setup(x => x.LoadReact).Returns(true);

var cache = new Mock<ICache>();
Expand All @@ -203,6 +206,7 @@ public void ShouldLoadResourcesWithPrecompilationAndNotEmptyCache()

var config = new Mock<IReactSiteConfiguration>();
config.Setup(x => x.AllowJavaScriptPrecompilation).Returns(true);
config.Setup(x => x.UseServerSideRendering).Returns(true);
config.Setup(x => x.LoadReact).Returns(true);

var shimsPrecompiledScript = new Mock<IPrecompiledScript>().Object;
Expand Down Expand Up @@ -236,6 +240,7 @@ public void ShouldLoadFilesThatDoNotRequireTransformWithoutPrecompilation()
var config = new Mock<IReactSiteConfiguration>();
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> { "First.js", "Second.js" });
config.Setup(x => x.AllowJavaScriptPrecompilation).Returns(false);
config.Setup(x => x.UseServerSideRendering).Returns(true);
config.Setup(x => x.LoadReact).Returns(true);

var cache = new Mock<ICache>();
Expand All @@ -261,6 +266,7 @@ public void ShouldLoadFilesThatDoNotRequireTransformWithPrecompilationAndWithout
var config = new Mock<IReactSiteConfiguration>();
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> { "First.js", "Second.js" });
config.Setup(x => x.AllowJavaScriptPrecompilation).Returns(true);
config.Setup(x => x.UseServerSideRendering).Returns(true);
config.Setup(x => x.LoadReact).Returns(true);

var cache = new NullCache();
Expand Down Expand Up @@ -291,6 +297,7 @@ public void ShouldLoadFilesThatDoNotRequireTransformWithPrecompilationAndEmptyCa
var config = new Mock<IReactSiteConfiguration>();
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> { "First.js", "Second.js" });
config.Setup(x => x.AllowJavaScriptPrecompilation).Returns(true);
config.Setup(x => x.UseServerSideRendering).Returns(true);
config.Setup(x => x.LoadReact).Returns(true);

var cache = new Mock<ICache>();
Expand All @@ -316,6 +323,7 @@ public void ShouldLoadFilesThatDoNotRequireTransformWithPrecompilationAndNotEmpt
var config = new Mock<IReactSiteConfiguration>();
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> { "First.js", "Second.js" });
config.Setup(x => x.AllowJavaScriptPrecompilation).Returns(true);
config.Setup(x => x.UseServerSideRendering).Returns(true);
config.Setup(x => x.LoadReact).Returns(true);

var firstPrecompiledScript = new Mock<IPrecompiledScript>().Object;
Expand All @@ -340,6 +348,32 @@ public void ShouldLoadFilesThatDoNotRequireTransformWithPrecompilationAndNotEmpt
jsEngine.Verify(x => x.Execute(secondPrecompiledScript));
}

[Fact]
public void ShouldNotLoadFilesThatDoNotRequireTransformWhenServerSideRenderingIsDisabled()
{
var jsEngine = new Mock<IJsEngine>();
jsEngine.Setup(x => x.SupportsScriptPrecompilation).Returns(true);
jsEngine.Setup(x => x.Evaluate<int>("1 + 1")).Returns(2);

var config = new Mock<IReactSiteConfiguration>();
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> { "First.js", "Second.js" });
config.Setup(x => x.AllowJavaScriptPrecompilation).Returns(false);
config.Setup(x => x.UseServerSideRendering).Returns(false);
config.Setup(x => x.LoadReact).Returns(true);

var cache = new Mock<ICache>();

var fileSystem = new Mock<IFileSystem>();
fileSystem.Setup(x => x.ReadAsString(It.IsAny<string>())).Returns<string>(path => "CONTENTS_" + path);

var factory = CreateFactory(config, cache, fileSystem, () => jsEngine.Object);

factory.GetEngineForCurrentThread();

jsEngine.Verify(x => x.Execute("CONTENTS_First.js", "First.js"), Times.Never);
jsEngine.Verify(x => x.Execute("CONTENTS_Second.js", "Second.js"), Times.Never);
}

[Fact]
public void ShouldHandleLoadingExternalReactVersion()
{
Expand Down Expand Up @@ -382,6 +416,7 @@ public void FileLockExceptionShouldBeWrapped()
{
var config = new Mock<IReactSiteConfiguration>();
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> { "foo.js" });
config.Setup(x => x.UseServerSideRendering).Returns(true);
config.Setup(x => x.LoadReact).Returns(false);
var cache = new Mock<ICache>();
var fileSystem = new Mock<IFileSystem>();
Expand All @@ -402,6 +437,7 @@ public void ShouldThrowScriptErrorIfReactFails()
var config = new Mock<IReactSiteConfiguration>();
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> {"foo.js"});
config.Setup(x => x.LoadReact).Returns(false);
config.Setup(x => x.UseServerSideRendering).Returns(true);
var cache = new Mock<ICache>();
var fileSystem = new Mock<IFileSystem>();
fileSystem.Setup(x => x.ReadAsString("foo.js")).Returns("FAIL PLZ");
Expand All @@ -424,6 +460,7 @@ public void ShouldCatchErrorsWhileLoadingScripts()
{
var config = new Mock<IReactSiteConfiguration>();
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> {"foo.js"});
config.Setup(x => x.UseServerSideRendering).Returns(true);
config.Setup(x => x.LoadReact).Returns(true);
var cache = new Mock<ICache>();
var fileSystem = new Mock<IFileSystem>();
Expand Down