Search code examples
webformsbundling-and-minificationasp.net-optimization

Regarding js & css file bundle & minification


i never use this feature.so i am reading few article on it.some confusion created in my mind after reading a article on bundle & minification. so i would like to know & clear those confusion asking here.

1) i could understand what is bundle & minification but like to know that minifaction will be done on the fly everytime or do i need to minify any js file before saving into js folder?

2) what bundle.add() does ? does it minify first and then include the file in bundle ?

3) if the file is already minified and the name is like jquery.min.js then what will happen...any error occur ?

4) if minifaction will be done on the fly then does it occur every time when different client request any page or is it one time only process ?

5) after minification minified version will be cache by server to prevent minifaction all the time ?

6)

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                 "~/Scripts/jquery-{version}.js"));

    // Code removed for clarity.
    BundleTable.EnableOptimizations = true;
}

please have a look at this code
"~/Scripts/jquery-{version}.js"));
is it wild card use {}

7) i came to know that actaul version of js file will be served during debugging not minified version. is it true ? how could i see it my self when i will test my page from IDE?

looking for discussion. thanks


Solution

    1. There is no need to minify files yourself.

    2. bundles.Add in your example will do one of many things: In debug mode it will add the non-minified version of jquery (in this case, the scripts show up individually, not bundled). This is done for debugging purposes.

      In non-debug mode, it will use the minified version, if one exists. If one doesn't exist, it will minify it for you and put it in the bundle... either alphabetically by file name or in the order you specified. It will also put known libraries in the top of the bundle (like jQuery) as needed.

    3. No error, but in debug mode, .min files are not used.

    4. One time process.

    5. Same as 4. New bundle will be created when a file changes, with query string "v" to force client to download the new bundle. "The query string v has a value token that is a unique identifier used for caching. As long as the bundle doesn't change, the ASP.NET application will request the bundle using this token. If any file in the bundle changes [it] will generate a new token, guaranteeing that browser will get the latest bundle." source

    6. Yes it is a version wild card to automatically create a jQuery bundle with the appropriate version of jQuery in your Scripts folder. Allows you to update versions of scripts without having to change your bundling code.

    7. Debug mode will serve up the individual, non-minified files (and no .min files). You can test by setting BundleTable.EnableOptimizations = false; or by removing that line altogether and just running in Debug mode.