diff --git a/src/React.AspNet/HtmlHelperExtensions.cs b/src/React.AspNet/HtmlHelperExtensions.cs index 08336eefd..d83088d2e 100644 --- a/src/React.AspNet/HtmlHelperExtensions.cs +++ b/src/React.AspNet/HtmlHelperExtensions.cs @@ -6,6 +6,7 @@ */ using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; @@ -193,13 +194,28 @@ public static IHtmlString ReactGetScriptPaths(this IHtmlHelper htmlHelper, IUrlH /// /// /// Optional IUrlHelper instance. Enables the use of tilde/relative (~/) paths inside the expose-components.js file. + /// Specifies if lazy style load technique should be used /// - public static IHtmlString ReactGetStylePaths(this IHtmlHelper htmlHelper, IUrlHelper urlHelper = null) + public static IHtmlString ReactGetStylePaths(this IHtmlHelper htmlHelper, IUrlHelper urlHelper = null, bool lazy = false) { - return new HtmlString(string.Join("", Environment.GetStylePaths() + return lazy + ? GetStylePathsLazy(urlHelper) + : GetStylePaths(urlHelper); + } + + private static IHtmlString GetStylePaths(IUrlHelper urlHelper = null) + { + return new HtmlString(string.Concat(Environment.GetStylePaths() .Select(stylePath => $""))); } + private static IHtmlString GetStylePathsLazy(IUrlHelper urlHelper = null) + { + var elements = Environment.GetStylePaths() + .Select(stylePath => $"createStyleElem('{(urlHelper == null ? stylePath : urlHelper.Content(stylePath))}');"); + return new HtmlString($""); + } + private static IHtmlString RenderToString(Action withWriter) { var stringWriter = _sharedStringWriter; diff --git a/tests/React.Tests/Mvc/HtmlHelperExtensionsTests.cs b/tests/React.Tests/Mvc/HtmlHelperExtensionsTests.cs index 964b26178..01399a8f9 100644 --- a/tests/React.Tests/Mvc/HtmlHelperExtensionsTests.cs +++ b/tests/React.Tests/Mvc/HtmlHelperExtensionsTests.cs @@ -315,5 +315,17 @@ public void ReactGetStylePaths() Assert.Equal("", result.ToHtmlString()); } + + [Fact] + public void ReactGetStylePathsLazy() + { + var environment = ConfigureMockEnvironment(); + + environment.Setup(x => x.GetStylePaths()).Returns(new[] { "/dist/vendor.css", "/dist/app.css" }); + + var result = HtmlHelperExtensions.ReactGetStylePaths(null, lazy: true); + + Assert.Equal("", result.ToHtmlString()); + } } }