Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET bundling and minification. Runs once per request?

Tags:

asp.net-mvc

I am wondering how may times the bundling and minification runs on the server?

Is it once per HTML request? Once per browser session? Once every time the app gets deployed?

like image 711
IGIT Avatar asked Sep 14 '15 14:09

IGIT


People also ask

What is true about bundling and minification in asp net core?

Bundling and minification are two distinct performance optimizations you can apply in a web app. Used together, bundling and minification improve performance by reducing the number of server requests and reducing the size of the requested static assets.

How bundling and minification works in MVC?

Bundling and Minification are two performance improvement techniques that improves the request load time of the application. Most of the current major browsers limit the number of simultaneous connections per hostname to six. It means that at a time, all the additional requests will be queued by the browser.

What must be done to enable bundling and minification?

Bundling and minification is enabled or disabled by setting the value of the debug attribute in the compilation Element in the Web. config file. In the following XML, debug is set to true so bundling and minification is disabled. To enable bundling and minification, set the debug value to "false".

What is difference between bundling and minification?

Both bundling and minification are the two separate techniques to reduce the load time. The bundling reduces the number of requests to the Server, while the minification reduces the size of the requested assets.


2 Answers

Bundles are created when the application is deployed or restarted, when the Application_Start method is called in Global.asax.cs. Inside Application_Start, BundleConfig.RegisterBundles is invoked, which is actually where the magic happens.

public class MvcApplication : System.Web.HttpApplication
{
    // this method is called on application start
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas(); // registers areas
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); // registers filters
        RouteConfig.RegisterRoutes(RouteTable.Routes); // registers routes
        BundleConfig.RegisterBundles(BundleTable.Bundles); // this generates the bundles
    }
}

And in your BundleConfig.cs file, the RegisterBundles method is the method that is called to create said bundles.

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        // this is actually what's creating the bundles
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));
        // etc...
    }
}

They are then cached in memory, can be accessed at /bundles/bundlename?v=versionId, and served for each HTTP request, but the actual process of bundling and minification only happens once.

like image 92
johnnyRose Avatar answered Oct 20 '22 01:10

johnnyRose


bundling and minification registered with Application_Start runs once and result is cached on server. Cached version provides for other request. Aplication_Start fires when first resource requested.

like image 32
MstfAsan Avatar answered Oct 20 '22 01:10

MstfAsan