Jump to Section
Introduction#
Website speed is a key factor for both user satisfaction and search engine rankings.
In Umbraco, optimizing JavaScript and CSS resources through bundling and minification can significantly enhance your site's performance.
In this guide, we’ll show you how to implement this technique using Microsoft.AspNet.Web.Optimization library.
💡Pro tip: Before proceeding, ensure you've grasped the basics of bundling and minification in ASP .NET from our previous article, Optimizing ASP.NET with Bundling and Minification
We also covered how to fix browser caching issues by appending unique version tokens to bundles. This technique ensures that browsers always load the latest versions of your scripts and styles after updates.
Setting Up Bundling and Minification in Umbraco#
To streamline script and style optimization in Umbraco, follow these steps:
- Install the Optimization Package: Add the Microsoft.AspNet.Web.Optimization library to your project using NuGet.
- Define Your Bundles: Create a BundleConfig.cs file and specify your JavaScript and CSS bundles.
- Force Optimization: Enable optimization even in development mode.
- Integrate with Umbraco: Register the bundling engine at application startup using Umbraco components and composers.
Once configured, you can include optimized bundles directly in your Razor Views for efficient rendering.
Registering Bundles in Umbraco#
In Umbraco, the best practice is to use the Composing and Components pattern for initialization.
Step-by-Step Guide
- Create a Compose Folder: Add a new Compose folder to your project.
- Create a Component: Inside the folder, create a class named BundleComponent. This class will handle the bundling process.
- Initialize Bundling: Add the bundling logic to your component's Initialize method.
Here’s an example:
If you’re new to components and composers, refer to this in-depth tutorial.
Next, wire up the component using a composer:
Crafting the BundleConfig#
The BundleConfig class manages your JavaScript and CSS bundles.
Place it in the App_Start folder (create one if it doesn’t exist):
This setup groups and minimizes your scripts and styles, reducing the number of HTTP requests and improving load times.
Enhancing with Dependency Injection Logging#
Umbraco’s Dependency Injection makes it easy to log events for better monitoring.
Here’s how to add logging to your BundleComponent:
This ensures you can track when bundling is initialized, making debugging easier.
Using Bundles in Razor Views
Include your bundled files in Razor Views using the @Scripts.Render and @Styles.Render helpers.
Update your ~/Views/Web.config to include the System.Web.Optimization namespace:
Then, render your bundles in Razor:
Key Benefits of Umbraco Bundling and Minification#
- Faster Page Loads: Fewer HTTP requests and smaller file sizes.
- Improved SEO: Speed is a ranking factor for search engines.
- Simplified Maintenance: Centralized script and style management.
Conclusion#
Efficient script optimization is a cornerstone of high-performing websites.
By leveraging the Microsoft.AspNet.Web.Optimization library in Umbraco, you can:
- Boost page load speed.
- Enhance user experience.
- Simplify asset management.
What's Next?
Do you have questions or need help with your Umbraco site?
Contact us or explore more tutorials on our blog.