Created
April 27, 2017 05:39
-
-
Save dejanstojanovic/3c954f23141182d745708e4fd2eade03 to your computer and use it in GitHub Desktop.
Inline minification and caching in razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Web; | |
using System.Web.Mvc; | |
using Yahoo.Yui.Compressor; | |
namespace Mvc.Razor.Helpers | |
{ | |
public static class Extensions | |
{ | |
public static MvcHtmlString InlineScriptBlock(this HtmlHelper htmlHelper, String[] paths, bool minify = true) | |
{ | |
var builder = new TagBuilder("script"); | |
builder.Attributes.Add("type", "text/javascript"); | |
return GetHtmlTag(htmlHelper, builder, paths, minify); | |
} | |
public static MvcHtmlString InlineScriptBlock(this HtmlHelper htmlHelper, String path, bool minify = true) | |
{ | |
return InlineScriptBlock(htmlHelper, new String[] { path }, minify); | |
} | |
public static MvcHtmlString InlineStyleBlock(this HtmlHelper htmlHelper, String[] paths, bool minify = true) | |
{ | |
var builder = new TagBuilder("style"); | |
builder.Attributes.Add("type", "text/css"); | |
return GetHtmlTag(htmlHelper, builder, paths, minify); | |
} | |
public static MvcHtmlString InlineStyleBlock(this HtmlHelper htmlHelper, String path, bool minify = true) | |
{ | |
return InlineStyleBlock(htmlHelper, new String[] { path }, minify); | |
} | |
private static MvcHtmlString GetHtmlTag(HtmlHelper htmlHelper, TagBuilder builder, String[] paths, bool minify) | |
{ | |
var context = HttpContext.Current; | |
List<String> physicalPaths = new List<string>(); | |
String cacheKey = string.Join("-", paths.Select(p => String.Concat(p.ToLower(), "_", minify.ToString()))); | |
String cachedContent = context.Cache.Get(cacheKey) as String; | |
if (cachedContent == null) | |
{ | |
foreach (String path in paths) | |
{ | |
var physicalPath = htmlHelper.ViewContext.RequestContext.HttpContext.Server.MapPath(path); | |
physicalPaths.Add(physicalPath); | |
if (File.Exists(physicalPath)) | |
{ | |
using (var fileStream = new FileStream(physicalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))//No lock on a file | |
{ | |
using (var streamReader = new StreamReader(fileStream)) | |
{ | |
cachedContent = String.Concat(cachedContent, streamReader.ReadToEnd(), System.Environment.NewLine); | |
} | |
} | |
} | |
} | |
if (minify) | |
{ | |
switch (builder.TagName.ToLower().Trim()) | |
{ | |
case "style": | |
cachedContent = new CssCompressor().Compress(cachedContent); | |
break; | |
case "script": | |
cachedContent = new JavaScriptCompressor().Compress(cachedContent); | |
break; | |
default: | |
throw new ArgumentException(String.Format("Unknown resource type {0}", builder.TagName)); | |
} | |
} | |
context.Cache.Insert(cacheKey, cachedContent, new System.Web.Caching.CacheDependency(physicalPaths.ToArray()), DateTime.MaxValue, TimeSpan.FromHours(1)); | |
} | |
builder.InnerHtml = cachedContent; | |
return MvcHtmlString.Create(builder.ToString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment