Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load script/style asynchronously. #10317

Merged
merged 3 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,23 @@ public class AbpBundlingOptions
/// </summary>
public BundlingMode Mode { get; set; } = BundlingMode.Auto;

public bool DeferScriptsByDefault { get; set; }

public List<string> DeferScripts { get; }

public bool PreloadStylesByDefault { get; set; }

public List<string> PreloadStyles { get; }

public AbpBundlingOptions()
{
StyleBundles = new BundleConfigurationCollection();
ScriptBundles = new BundleConfigurationCollection();
MinificationIgnoredFiles = new HashSet<string>();
DeferScriptsByDefault = false;
DeferScripts = new List<string>();
PreloadStylesByDefault = false;
PreloadStyles = new List<string>();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
{
await ResourceService.ProcessAsync(
TagHelper.ViewContext,
TagHelper,
context,
output,
new List<BundleTagHelperItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
{
await ResourceService.ProcessAsync(
TagHelper.ViewContext,
TagHelper,
context,
output,
await GetBundleItems(context, output),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
[HtmlTargetElement("abp-script-bundle", TagStructure = TagStructure.NormalOrSelfClosing)]
public class AbpScriptBundleTagHelper : AbpBundleTagHelper<AbpScriptBundleTagHelper, AbpScriptBundleTagHelperService>, IBundleTagHelper
{
[HtmlAttributeName("defer")]
public bool Defer { get; set; }

public AbpScriptBundleTagHelper(AbpScriptBundleTagHelperService service)
: base(service)
{

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
[HtmlTargetElement("abp-script", TagStructure = TagStructure.NormalOrSelfClosing)]
public class AbpScriptTagHelper : AbpBundleItemTagHelper<AbpScriptTagHelper, AbpScriptTagHelperService>, IBundleItemTagHelper
{
[HtmlAttributeName("defer")]
public bool Defer { get; set; }

public AbpScriptTagHelper(AbpScriptTagHelperService service)
: base(service)
{
Expand All @@ -16,4 +19,4 @@ protected override string GetFileExtension()
return "js";
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
[HtmlTargetElement("abp-style-bundle", TagStructure = TagStructure.NormalOrSelfClosing)]
public class AbpStyleBundleTagHelper : AbpBundleTagHelper<AbpStyleBundleTagHelper, AbpStyleBundleTagHelperService>, IBundleTagHelper
{
[HtmlAttributeName("preload")]
public bool Preload { get; set; }

public AbpStyleBundleTagHelper(AbpStyleBundleTagHelperService service)
: base(service)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
[HtmlTargetElement("abp-style", TagStructure = TagStructure.NormalOrSelfClosing)]
public class AbpStyleTagHelper : AbpBundleItemTagHelper<AbpStyleTagHelper, AbpStyleTagHelperService>, IBundleItemTagHelper
{
[HtmlAttributeName("preload")]
public bool Preload { get; set; }

public AbpStyleTagHelper(AbpStyleTagHelperService service)
: base(service)
{
Expand All @@ -16,4 +19,4 @@ protected override string GetFileExtension()
return "css";
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public abstract class AbpTagHelperResourceService : ITransientDependency

public virtual async Task ProcessAsync(
[NotNull] ViewContext viewContext,
[NotNull] TagHelper tagHelper,
[NotNull] TagHelperContext context,
[NotNull] TagHelperOutput output,
[NotNull] List<BundleTagHelperItem> bundleItems,
Expand Down Expand Up @@ -72,7 +73,7 @@ public abstract class AbpTagHelperResourceService : ITransientDependency

if (file.Length > 0)
{
AddHtmlTag(viewContext, context, output, bundleFile + "?_v=" + file.LastModified.UtcTicks);
AddHtmlTag(viewContext, tagHelper, context, output, bundleFile + "?_v=" + file.LastModified.UtcTicks);
}
}

Expand All @@ -84,7 +85,7 @@ public abstract class AbpTagHelperResourceService : ITransientDependency

protected abstract Task<IReadOnlyList<string>> GetBundleFilesAsync(string bundleName);

protected abstract void AddHtmlTag(ViewContext viewContext, TagHelperContext context, TagHelperOutput output, string file);
protected abstract void AddHtmlTag(ViewContext viewContext, TagHelper tagHelper, TagHelperContext context, TagHelperOutput output, string file);

protected virtual string GenerateBundleName(List<BundleTagHelperItem> bundleItems)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.VirtualFileSystem;

namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
{
Expand All @@ -19,8 +16,7 @@ public class AbpTagHelperScriptService : AbpTagHelperResourceService
public AbpTagHelperScriptService(
IBundleManager bundleManager,
IOptions<AbpBundlingOptions> options,
IWebHostEnvironment hostingEnvironment
) : base(
IWebHostEnvironment hostingEnvironment) : base(
bundleManager,
options,
hostingEnvironment)
Expand All @@ -41,9 +37,19 @@ protected override async Task<IReadOnlyList<string>> GetBundleFilesAsync(string
return await BundleManager.GetScriptBundleFilesAsync(bundleName);
}

protected override void AddHtmlTag(ViewContext viewContext, TagHelperContext context, TagHelperOutput output, string file)
protected override void AddHtmlTag(ViewContext viewContext, TagHelper tagHelper, TagHelperContext context, TagHelperOutput output, string file)
{
output.Content.AppendHtml($"<script src=\"{viewContext.GetUrlHelper().Content(file.EnsureStartsWith('~'))}\"></script>{Environment.NewLine}");
var defer = tagHelper switch
{
AbpScriptTagHelper scriptTagHelper => scriptTagHelper.Defer,
AbpScriptBundleTagHelper scriptBundleTagHelper => scriptBundleTagHelper.Defer,
_ => false
};

var deferText = (defer || Options.DeferScriptsByDefault || Options.DeferScripts.Any(x => file.StartsWith(x, StringComparison.OrdinalIgnoreCase)))
? "defer"
: string.Empty;
output.Content.AppendHtml($"<script {deferText} src=\"{viewContext.GetUrlHelper().Content(file.EnsureStartsWith('~'))}\"></script>{Environment.NewLine}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.VirtualFileSystem;

namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
{
Expand All @@ -19,8 +16,7 @@ public class AbpTagHelperStyleService : AbpTagHelperResourceService
public AbpTagHelperStyleService(
IBundleManager bundleManager,
IOptions<AbpBundlingOptions> options,
IWebHostEnvironment hostingEnvironment
) : base(
IWebHostEnvironment hostingEnvironment) : base(
bundleManager,
options,
hostingEnvironment)
Expand All @@ -41,9 +37,23 @@ protected override async Task<IReadOnlyList<string>> GetBundleFilesAsync(string
return await BundleManager.GetStyleBundleFilesAsync(bundleName);
}

protected override void AddHtmlTag(ViewContext viewContext, TagHelperContext context, TagHelperOutput output, string file)
protected override void AddHtmlTag(ViewContext viewContext, TagHelper tagHelper, TagHelperContext context, TagHelperOutput output, string file)
{
output.Content.AppendHtml($"<link rel=\"stylesheet\" href=\"{viewContext.GetUrlHelper().Content(file.EnsureStartsWith('~'))}\" />{Environment.NewLine}");
var preload = tagHelper switch
{
AbpStyleTagHelper styleTagHelper => styleTagHelper.Preload,
AbpStyleBundleTagHelper styleBundleTagHelper => styleBundleTagHelper.Preload,
_ => false
};

if (preload || Options.PreloadStylesByDefault || Options.PreloadStyles.Any(x => file.StartsWith(x, StringComparison.OrdinalIgnoreCase)))
{
output.Content.AppendHtml($"<link rel=\"preload\" href=\"{viewContext.GetUrlHelper().Content(file.EnsureStartsWith('~'))}\" as=\"style\" onload=\"this.rel='stylesheet'\" />{Environment.NewLine}");
}
else
{
output.Content.AppendHtml($"<link rel=\"stylesheet\" href=\"{viewContext.GetUrlHelper().Content(file.EnsureStartsWith('~'))}\" />{Environment.NewLine}");
}
}
}
}