ligershark / WebOptimizer

A bundler and minifier for ASP.NET Core
Apache License 2.0
750 stars 113 forks source link

Unable to load the files from cdn into a bundle in .net 6.0 #271

Open KunalSharma1999 opened 1 year ago

KunalSharma1999 commented 1 year ago

In ASP.Net MVC following was the way to load files from a cdn link and make it into a bundle:

public static void RegisterBundles(BundleCollection bundles)
{
     bundles.UseCdn = true;
     var version = System.Reflection.Assembly.GetAssembly(typeof(Controllers.HomeController)).GetName().Version.ToString();

    var cdnUrl = ConfigurationManager.AppSettings["cdnUrl"].ToString()+"/{0}?v=" + version;

    bundles.Add(new ScriptBundle("~/bundles/jquery", string.Format(cdnUrl, "bundles/jquery")).Include(
                "~/Scripts/jquery-{version}.js"));
    bundles.Add(new ScriptBundle("~/bundles/jqueryval", string.Format(cdnUrl, "bundles/jqueryval")).Include(
                "~/Scripts/jquery.validate*"));
    bundles.Add(new ScriptBundle("~/bundles/modernizr", string.Format(cdnUrl, "bundles/modernizr")).Include(
                  "~/Scripts/modernizr-*"));
    bundles.Add(new ScriptBundle("~/bundles/bootstrap", string.Format(cdnUrl, "bundles/bootstrap")).Include(
                "~/Scripts/bootstrap.js"));
 }

How can I achieve the same functionality using LigerShark.WebOptimizer in ASP.Net Core MVC project targeting .net 6.0?

I have already added the below mentioned configuration in my appsetting.json file and registered the tag helpers as mentioned in the documentation which states that it will prepend the cdn url but its not working:

"webOptimizer": {
"enableCaching": true,
"enableMemoryCache": true,
"enableDiskCache": true,
"cacheDirectory": "/var/temp/weboptimizercache",
"enableTagHelperBundling": true,
"cdnUrl": "https://mycdnurl.azureedge.net",
 "allowEmptyBundle": false
}

Here is the actual code I have used in my Program.cs file:

builder.Services.AddWebOptimizer(pipeline =>
{
    pipeline.AddJavaScriptBundle("/bundles/jquery.js", "Scripts/jquery-3.3.1.js");
    pipeline.AddJavaScriptBundle("/bundles/jqueryval.js", "Scripts/jquery.validate*");
    pipeline.AddJavaScriptBundle("/bundles/jqueryajax.js", "Scripts/jquery.unobtrusiveajax.min.js");
    pipeline.AddJavaScriptBundle("/bundles/modernizr.js", "Scripts/modernizr-*");
    pipeline.AddJavaScriptBundle("/bundles/bootstrap.js", "Scripts/bootstrap.js");
if (!isDevelopment)
{
     pipeline.MinifyCssFiles();
     pipeline.MinifyJsFiles();
}
},
option =>
{
    option.EnableCaching = true;
    option.EnableDiskCache = false;
    option.EnableMemoryCache = true;
    option.AllowEmptyBundle = false;
    option.EnableTagHelperBundling = true;
    option.CdnUrl = builder.Configuration.GetSection("webOptimizer")["cdnUrl"];
}
);

I also have multiple css files to be bundled and minified but I haven't added that to my code for better clarity.