Skip to content

Commit 6b2884a

Browse files
committed
Do not load precompiled user scripts when server side render disabled.
1 parent 929dc5b commit 6b2884a

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/React.Core/JavaScriptEngineFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ private void LoadResource(IJsEngine engine, string resourceName, Assembly assemb
171171
/// <param name="engine">Engine to load scripts into</param>
172172
private void LoadUserScripts(IJsEngine engine)
173173
{
174+
// Do not execute user scripts if rendering is disabled.
175+
if (!_config.UseServerSideRendering)
176+
{
177+
return;
178+
}
174179
foreach (var file in _config.ScriptsWithoutTransform)
175180
{
176181
try

tests/React.Tests/Core/JavaScriptEngineFactoryTest.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public void ShouldLoadResourcesWithoutPrecompilation()
122122

123123
var config = new Mock<IReactSiteConfiguration>();
124124
config.Setup(x => x.AllowJavaScriptPrecompilation).Returns(false);
125+
config.Setup(x => x.UseServerSideRendering).Returns(true);
125126
config.Setup(x => x.LoadReact).Returns(true);
126127

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

148149
var config = new Mock<IReactSiteConfiguration>();
149150
config.Setup(x => x.AllowJavaScriptPrecompilation).Returns(true);
151+
config.Setup(x => x.UseServerSideRendering).Returns(true);
150152
config.Setup(x => x.LoadReact).Returns(true);
151153

152154
var cache = new NullCache();
@@ -180,6 +182,7 @@ public void ShouldLoadResourcesWithPrecompilationAndEmptyCache()
180182

181183
var config = new Mock<IReactSiteConfiguration>();
182184
config.Setup(x => x.AllowJavaScriptPrecompilation).Returns(true);
185+
config.Setup(x => x.UseServerSideRendering).Returns(true);
183186
config.Setup(x => x.LoadReact).Returns(true);
184187

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

204207
var config = new Mock<IReactSiteConfiguration>();
205208
config.Setup(x => x.AllowJavaScriptPrecompilation).Returns(true);
209+
config.Setup(x => x.UseServerSideRendering).Returns(true);
206210
config.Setup(x => x.LoadReact).Returns(true);
207211

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

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

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

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

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

351+
[Fact]
352+
public void ShouldNotLoadFilesThatDoNotRequireTransformWhenServerSideRenderingIsDisabled()
353+
{
354+
var jsEngine = new Mock<IJsEngine>();
355+
jsEngine.Setup(x => x.SupportsScriptPrecompilation).Returns(true);
356+
jsEngine.Setup(x => x.Evaluate<int>("1 + 1")).Returns(2);
357+
358+
var config = new Mock<IReactSiteConfiguration>();
359+
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> { "First.js", "Second.js" });
360+
config.Setup(x => x.AllowJavaScriptPrecompilation).Returns(false);
361+
config.Setup(x => x.UseServerSideRendering).Returns(false);
362+
config.Setup(x => x.LoadReact).Returns(true);
363+
364+
var cache = new Mock<ICache>();
365+
366+
var fileSystem = new Mock<IFileSystem>();
367+
fileSystem.Setup(x => x.ReadAsString(It.IsAny<string>())).Returns<string>(path => "CONTENTS_" + path);
368+
369+
var factory = CreateFactory(config, cache, fileSystem, () => jsEngine.Object);
370+
371+
factory.GetEngineForCurrentThread();
372+
373+
jsEngine.Verify(x => x.Execute("CONTENTS_First.js", "First.js"), Times.Never);
374+
jsEngine.Verify(x => x.Execute("CONTENTS_Second.js", "Second.js"), Times.Never);
375+
}
376+
343377
[Fact]
344378
public void ShouldHandleLoadingExternalReactVersion()
345379
{
@@ -382,6 +416,7 @@ public void FileLockExceptionShouldBeWrapped()
382416
{
383417
var config = new Mock<IReactSiteConfiguration>();
384418
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> { "foo.js" });
419+
config.Setup(x => x.UseServerSideRendering).Returns(true);
385420
config.Setup(x => x.LoadReact).Returns(false);
386421
var cache = new Mock<ICache>();
387422
var fileSystem = new Mock<IFileSystem>();
@@ -402,6 +437,7 @@ public void ShouldThrowScriptErrorIfReactFails()
402437
var config = new Mock<IReactSiteConfiguration>();
403438
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> {"foo.js"});
404439
config.Setup(x => x.LoadReact).Returns(false);
440+
config.Setup(x => x.UseServerSideRendering).Returns(true);
405441
var cache = new Mock<ICache>();
406442
var fileSystem = new Mock<IFileSystem>();
407443
fileSystem.Setup(x => x.ReadAsString("foo.js")).Returns("FAIL PLZ");
@@ -424,6 +460,7 @@ public void ShouldCatchErrorsWhileLoadingScripts()
424460
{
425461
var config = new Mock<IReactSiteConfiguration>();
426462
config.Setup(x => x.ScriptsWithoutTransform).Returns(new List<string> {"foo.js"});
463+
config.Setup(x => x.UseServerSideRendering).Returns(true);
427464
config.Setup(x => x.LoadReact).Returns(true);
428465
var cache = new Mock<ICache>();
429466
var fileSystem = new Mock<IFileSystem>();

0 commit comments

Comments
 (0)