Blazor Tip - Upgrade Bootstrap


 

The current Blazor templates ship with the Bootstrap version v5.1.0, the current version is v5.3.  It may not seem like a big difference, but it is nice to get the latest version when you start a new project. You could use NPM or Nuget to upgrade your bootstrap, but there is an easier way.  

We will be using the CDN link for our bootstrap.  This has several advantages that will help us.

1. Loads faster
2. Loads even faster if it is already cached in the browser.
3. Remove the download of the files from your server.
4. You can delete the old bootstrap files from your project.

There are a couple of simple steps to do this upgrade.


2. Find the "Include via CDN" link and copy it.
3. In the Blazor app replace this line:

    <link rel="stylesheet" href="bootstrap/bootstrap.min.css" />

with:

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

Note: The location of the link will depend on which Blazor project you are creating.

4. Test to ensure everything looks good.
    a. upgrading from 5.2 to 5.3 causes the icons on the nav to move to the left.  To fix this you just need to add ps-3 to the nav link

        <NavLink class="nav-link ps-3" href="" Match="NavLinkMatch.All">

5.  Remove the old Bootstrap folder from under the wwwroot folder.


It needs to be noted that you should be careful when upgrading to any major version.  Most major versions have breaking changes within them.  Make sure you do your research first.


Summary

Doing this simple change can ensure you are starting your next project with the most current Bootstrap version and it will allow you to take advantage of the Bootstrap CDN saving your resources.

Comments

Popular posts from this blog

Yes, Blazor Server can scale!

Blazor new and improved Search Box

Blazor Wizard Step Component