--- title: Bundle and minify static assets in ASP.NET Core author: rick-anderson description: Learn how to optimize static resources in an ASP.NET Core web application by applying bundling and minification techniques. ms.author: scaddie ms.custom: mvc ms.date: 03/14/2021 uid: client-side/bundling-and-minification --- # Bundle and minify static assets in ASP.NET Core By [Scott Addie](https://twitter.com/Scott_Addie) and [David Pine](https://twitter.com/davidpine7) This article explains the benefits of applying bundling and minification, including how these features can be used with ASP.NET Core web apps. ## What is bundling and minification 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. Bundling and minification primarily improve the first page request load time. Once a web page has been requested, the browser caches the static assets (JavaScript, CSS, and images). So, bundling and minification don't improve performance when requesting the same page, or pages, on the same site requesting the same assets. If the expires header isn't set correctly on the assets and if bundling and minification isn't used, the browser's freshness heuristics mark the assets stale after a few days. Additionally, the browser requires a validation request for each asset. In this case, bundling and minification provide a performance improvement even after the first page request. ### Bundling Bundling combines multiple files into a single file. Bundling reduces the number of server requests that are necessary to render a web asset, such as a web page. You can create any number of individual bundles specifically for CSS, JavaScript, etc. Fewer files mean fewer HTTP requests from the browser to the server or from the service providing your application. This results in improved first page load performance. ### Minification Minification removes unnecessary characters from code without altering functionality. The result is a significant size reduction in requested assets (such as CSS, images, and JavaScript files). Common side effects of minification include shortening variable names to one character and removing comments and unnecessary whitespace. Consider the following JavaScript function: ```javascript AddAltToImg = function (imageTagAndImageID, imageContext) { /// /// Adds an alt tab to the image // //The image selector. //The image context. /// var imageElement = $(imageTagAndImageID, imageContext); imageElement.attr('alt', imageElement.attr('id').replace(/ID/, '')); } ``` Minification reduces the function to the following: ```javascript AddAltToImg=function(t,a){var r=$(t,a);r.attr("alt",r.attr("id").replace(/ID/,""))}; ``` In addition to removing the comments and unnecessary whitespace, the following parameter and variable names were renamed as follows: Original | Renamed --- | :---: `imageTagAndImageID` | `t` `imageContext` | `a` `imageElement` | `r` ## Impact of bundling and minification The following table outlines differences between individually loading assets and using bundling and minification for a typical web app. Action | Without B/M | With B/M | Reduction --- | :---: | :---: | :---: File Requests | 18 | 7 | 61% Bytes Transferred (KB) | 265 | 156 | 41% Load Time (ms) | 2360 | 885 | 63% The load time improved, but this example ran locally. Greater performance gains are realized when using bundling and minification with assets transferred over a network. The test app used to generate the figures in the preceding table demonstrates typical improvements that might not apply to a given app. We recommend testing an app to determine if bundling and minification yields an improved load time. ## Choose a bundling and minification strategy ASP.NET Core is compatible with WebOptimizer, an open-source bundling and minification solution. For set up instructions and sample projects, see [WebOptimizer](https://github.com/ligershark/WebOptimizer). ASP.NET Core doesn't provide a native bundling and minification solution. Third-party tools, such as [Gulp](https://gulpjs.com) and [Webpack](https://webpack.js.org), provide workflow automation for bundling and minification, as well as linting and image optimization. By using bundling and minification, the minified files are created prior to the app's deployment. Bundling and minifying before deployment provides the advantage of reduced server load. However, it's important to recognize that bundling and minification increases build complexity and only works with static files. ## Environment-based bundling and minification As a best practice, the bundled and minified files of your app should be used in a production environment. During development, the original files make for easier debugging of the app. Specify which files to include in your pages by using the [Environment Tag Helper](xref:mvc/views/tag-helpers/builtin-th/environment-tag-helper) in your views. The Environment Tag Helper only renders its contents when running in specific [environments](xref:fundamentals/environments). The following `environment` tag renders the unprocessed CSS files when running in the `Development` environment: :::moniker range=">= aspnetcore-2.0" ```cshtml ``` :::moniker-end :::moniker range="<= aspnetcore-1.1" ```cshtml ``` :::moniker-end The following `environment` tag renders the bundled and minified CSS files when running in an environment other than `Development`. For example, running in `Production` or `Staging` triggers the rendering of these stylesheets: :::moniker range=">= aspnetcore-2.0" ```cshtml ``` :::moniker-end :::moniker range="<= aspnetcore-1.1" ```cshtml ``` :::moniker-end ## Additional resources * [Use multiple environments](xref:fundamentals/environments) * [Tag Helpers](xref:mvc/views/tag-helpers/intro)