diff --git a/aspnetcore/mvc/views/tag-helpers/authoring.md b/aspnetcore/mvc/views/tag-helpers/authoring.md index 705ebfb604..d2ac1acc5a 100644 --- a/aspnetcore/mvc/views/tag-helpers/authoring.md +++ b/aspnetcore/mvc/views/tag-helpers/authoring.md @@ -307,3 +307,12 @@ The tag helpers provide several properties to retrieve content. [!code-csharp[](../../views/tag-helpers/authoring/sample/AuthoringTagHelpers/src/AuthoringTagHelpers/TagHelpers/z1AutoLinkerCopy.cs?highlight=5,6,10&range=8-21)] * Multiple calls to `GetChildContentAsync` returns the same value and doesn't re-execute the `TagHelper` body unless you pass in a false parameter indicating not to use the cached result. + +## Load minified partial view TagHelper + +In production environments, performance can be improved by loading minified partial views. To take advantage of minified partial view in production: + +* Create/set up a pre-build process that minifies partial views. +* Use the following code to load minified partial views in non-development environments. + +[!code-csharp[](authoring/sample/AuthoringTagHelpers/src/MinifiedVersionTagHelper.cs?name=snippet)] diff --git a/aspnetcore/mvc/views/tag-helpers/authoring/sample/AuthoringTagHelpers/src/MinifiedVersionTagHelper.cs b/aspnetcore/mvc/views/tag-helpers/authoring/sample/AuthoringTagHelpers/src/MinifiedVersionTagHelper.cs new file mode 100644 index 0000000000..d5fa7afc95 --- /dev/null +++ b/aspnetcore/mvc/views/tag-helpers/authoring/sample/AuthoringTagHelpers/src/MinifiedVersionTagHelper.cs @@ -0,0 +1,44 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.TagHelpers; +using Microsoft.AspNetCore.Mvc.ViewEngines; +using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal; +using Microsoft.AspNetCore.Razor.TagHelpers; +using System; +using System.Threading.Tasks; + +namespace AuthoringTagHelpers.TagHelpers +{ + /// + /// The minified-partial tag helper loads + /// minified partials depending on the value + /// of ASPNETCORE_ENVIRONMENT. + /// + #region snippet + public class MinifiedVersionPartialTagHelper : PartialTagHelper + { + public MinifiedVersionPartialTagHelper(ICompositeViewEngine viewEngine, + IViewBufferScope viewBufferScope) + : base(viewEngine, viewBufferScope) + { + + } + + public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) + { + // Append ".min" to load the minified partial view. + if (!IsDevelopment()) + { + Name += ".min"; + } + + return base.ProcessAsync(context, output); + } + + private bool IsDevelopment() + { + return Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") + == EnvironmentName.Development; + } + } + #endregion +} \ No newline at end of file