Skip to content

Commit

Permalink
Performance. Add support for "async" for javascript files
Browse files Browse the repository at this point in the history
  • Loading branch information
agametov committed Jun 8, 2016
1 parent b21dee8 commit 214997d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/Presentation/Nop.Web.Framework/UI/IPageHeadBuilder.cs
Expand Up @@ -19,8 +19,8 @@ public partial interface IPageHeadBuilder
void AppendMetaKeywordParts(string part);
string GenerateMetaKeywords();

void AddScriptParts(ResourceLocation location, string part, bool excludeFromBundle);
void AppendScriptParts(ResourceLocation location, string part, bool excludeFromBundle);
void AddScriptParts(ResourceLocation location, string part, bool excludeFromBundle, bool isAync);
void AppendScriptParts(ResourceLocation location, string part, bool excludeFromBundle, bool isAsync);
string GenerateScripts(UrlHelper urlHelper, ResourceLocation location, bool? bundleFiles = null);

void AddCssFileParts(ResourceLocation location, string part);
Expand Down
20 changes: 12 additions & 8 deletions src/Presentation/Nop.Web.Framework/UI/LayoutExtensions.cs
Expand Up @@ -117,9 +117,10 @@ public static MvcHtmlString NopMetaKeywords(this HtmlHelper html, string part =
/// <param name="html">HTML helper</param>
/// <param name="part">Script part</param>
/// <param name="excludeFromBundle">A value indicating whether to exclude this script from bundling</param>
public static void AddScriptParts(this HtmlHelper html, string part, bool excludeFromBundle = false)
/// <param name="isAsync">A value indicating whether to add an attribute "async" or not for js files</param>
public static void AddScriptParts(this HtmlHelper html, string part, bool excludeFromBundle = false, bool isAsync = false)
{
AddScriptParts(html, ResourceLocation.Head, part, excludeFromBundle);
AddScriptParts(html, ResourceLocation.Head, part, excludeFromBundle, isAsync);
}
/// <summary>
/// Add script element
Expand All @@ -128,20 +129,22 @@ public static void AddScriptParts(this HtmlHelper html, string part, bool exclud
/// <param name="location">A location of the script element</param>
/// <param name="part">Script part</param>
/// <param name="excludeFromBundle">A value indicating whether to exclude this script from bundling</param>
public static void AddScriptParts(this HtmlHelper html, ResourceLocation location, string part, bool excludeFromBundle = false)
/// <param name="isAsync">A value indicating whether to add an attribute "async" or not for js files</param>
public static void AddScriptParts(this HtmlHelper html, ResourceLocation location, string part, bool excludeFromBundle = false, bool isAsync = false)
{
var pageHeadBuilder = EngineContext.Current.Resolve<IPageHeadBuilder>();
pageHeadBuilder.AddScriptParts(location, part, excludeFromBundle);
pageHeadBuilder.AddScriptParts(location, part, excludeFromBundle, isAsync);
}
/// <summary>
/// Append script element
/// </summary>
/// <param name="html">HTML helper</param>
/// <param name="part">Script part</param>
/// <param name="excludeFromBundle">A value indicating whether to exclude this script from bundling</param>
public static void AppendScriptParts(this HtmlHelper html, string part, bool excludeFromBundle = false)
/// <param name="isAsync">A value indicating whether to add an attribute "async" or not for js files</param>
public static void AppendScriptParts(this HtmlHelper html, string part, bool excludeFromBundle = false, bool isAsync = false)
{
AppendScriptParts(html, ResourceLocation.Head, part, excludeFromBundle);
AppendScriptParts(html, ResourceLocation.Head, part, excludeFromBundle, isAsync);
}
/// <summary>
/// Append script element
Expand All @@ -150,10 +153,11 @@ public static void AppendScriptParts(this HtmlHelper html, string part, bool exc
/// <param name="location">A location of the script element</param>
/// <param name="part">Script part</param>
/// <param name="excludeFromBundle">A value indicating whether to exclude this script from bundling</param>
public static void AppendScriptParts(this HtmlHelper html, ResourceLocation location, string part, bool excludeFromBundle = false)
/// <param name="isAsync">A value indicating whether to add an attribute "async" or not for js files</param>
public static void AppendScriptParts(this HtmlHelper html, ResourceLocation location, string part, bool excludeFromBundle = false, bool isAsync = false)
{
var pageHeadBuilder = EngineContext.Current.Resolve<IPageHeadBuilder>();
pageHeadBuilder.AppendScriptParts(location, part, excludeFromBundle);
pageHeadBuilder.AppendScriptParts(location, part, excludeFromBundle, isAsync);
}
/// <summary>
/// Generate all script parts
Expand Down
18 changes: 11 additions & 7 deletions src/Presentation/Nop.Web.Framework/UI/PageHeadBuilder.cs
Expand Up @@ -195,7 +195,7 @@ public virtual string GenerateMetaKeywords()
}


public virtual void AddScriptParts(ResourceLocation location, string part, bool excludeFromBundle)
public virtual void AddScriptParts(ResourceLocation location, string part, bool excludeFromBundle, bool isAsync)
{
if (!_scriptParts.ContainsKey(location))
_scriptParts.Add(location, new List<ScriptReferenceMeta>());
Expand All @@ -206,10 +206,11 @@ public virtual void AddScriptParts(ResourceLocation location, string part, bool
_scriptParts[location].Add(new ScriptReferenceMeta
{
ExcludeFromBundle = excludeFromBundle,
IsAsync = isAsync,
Part = part
});
}
public virtual void AppendScriptParts(ResourceLocation location, string part, bool excludeFromBundle)
public virtual void AppendScriptParts(ResourceLocation location, string part, bool excludeFromBundle, bool isAsync)
{
if (!_scriptParts.ContainsKey(location))
_scriptParts.Add(location, new List<ScriptReferenceMeta>());
Expand All @@ -220,6 +221,7 @@ public virtual void AppendScriptParts(ResourceLocation location, string part, bo
_scriptParts[location].Insert(0, new ScriptReferenceMeta
{
ExcludeFromBundle = excludeFromBundle,
IsAsync = isAsync,
Part = part
});
}
Expand All @@ -245,7 +247,7 @@ public virtual string GenerateScripts(UrlHelper urlHelper, ResourceLocation loca
.ToArray();
var partsToDontBundle = _scriptParts[location]
.Where(x => x.ExcludeFromBundle)
.Select(x => x.Part)
.Select(x => new { x.Part, x.IsAsync})
.Distinct()
.ToArray();

Expand Down Expand Up @@ -278,9 +280,9 @@ public virtual string GenerateScripts(UrlHelper urlHelper, ResourceLocation loca
}

//parts to do not bundle
foreach (var path in partsToDontBundle)
foreach (var item in partsToDontBundle)
{
result.AppendFormat("<script src=\"{0}\" type=\"{1}\"></script>", urlHelper.Content(path), MimeTypes.TextJavascript);
result.AppendFormat("<script {2}src=\"{0}\" type=\"{1}\"></script>", urlHelper.Content(item.Part), MimeTypes.TextJavascript, item.IsAsync ? "async " : "");
result.Append(Environment.NewLine);
}
return result.ToString();
Expand All @@ -289,9 +291,9 @@ public virtual string GenerateScripts(UrlHelper urlHelper, ResourceLocation loca
{
//bundling is disabled
var result = new StringBuilder();
foreach (var path in _scriptParts[location].Select(x => x.Part).Distinct())
foreach (var item in _scriptParts[location].Select(x => new { x.Part, x.IsAsync}).Distinct())
{
result.AppendFormat("<script src=\"{0}\" type=\"{1}\"></script>", urlHelper.Content(path), MimeTypes.TextJavascript);
result.AppendFormat("<script {2}src=\"{0}\" type=\"{1}\"></script>", urlHelper.Content(item.Part), MimeTypes.TextJavascript, item.IsAsync ? "async ":"");
result.Append(Environment.NewLine);
}
return result.ToString();
Expand Down Expand Up @@ -506,6 +508,8 @@ private class ScriptReferenceMeta
{
public bool ExcludeFromBundle { get; set; }

public bool IsAsync { get; set; }

public string Part { get; set; }
}

Expand Down

0 comments on commit 214997d

Please sign in to comment.