Skip to content

Instantly share code, notes, and snippets.

@tompipe
Created August 11, 2014 12:34
Show Gist options
  • Save tompipe/4717f6354b92534a3b45 to your computer and use it in GitHub Desktop.
Save tompipe/4717f6354b92534a3b45 to your computer and use it in GitHub Desktop.
Enable HTML Minification
public class Global : UmbracoApplication
{
protected override void OnApplicationStarting(object sender, EventArgs e)
{
GlobalFilters.Filters.Add(new RemoveHtmlWhitespaceFilterAttribute());
base.OnApplicationStarting(sender, e);
}
}
public class RemoveHtmlWhitespaceFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var response = filterContext.HttpContext.Response;
response.Filter = new ModifyOutputFilter(response.Filter, s =>
{
if (I2GSettings.Instance.EnableMinification)
{
s = Regex.Replace(s, @"\s+", " ");
s = Regex.Replace(s, @"\s*\n\s*", "\n");
s = Regex.Replace(s, @"\s*\>\s*\<\s*", "><");
s = Regex.Replace(s, @"<!--(.*?)-->", ""); //Remove comments
// single-line doctype must be preserved
var firstEndBracketPosition = s.IndexOf(">", StringComparison.Ordinal);
if (firstEndBracketPosition >= 0)
{
s = s.Remove(firstEndBracketPosition, 1);
s = s.Insert(firstEndBracketPosition, ">");
}
}
return s;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment