Modifying components and Optimization in Bootstrap
Overview
Website optimization is a cluster of techniques, both server-side and client-side, which aim at speeding up websites’ loading time and rendering in the browser for a better user experience. Today, visitors are used to the immediacy and reactivity typical of native apps. They expect a web page to load within 1000ms. If it takes much longer than this, they’re likely to leave the site. Bootstrap has often been criticized for adding unnecessary bloat to websites, so if you use this popular front-end UI library in your project, make sure you pay extra attention to page weight and page speed.
What is Component Optimization in Bootstrap?
Bootstrap is a large front-end CSS framework that offers varieties of functionalities. But most websites don’t need all of those functionalities that come with the Bootstrap bundle. As a result, a big bundle loads every time someone visits your website and most of the bundle remains unused.
The solution is simple, download the source code and compile the part of Bootstrap code that is being used in your project. But that brings two problems.
- Bootstrap source code contains a large number of files. A beginner can easily be confused.
- We need to maintain two separate repositories. One is for the application itself, the other is for Bootstrap.
Why is Component Optimization Important?
As Bootstrap is a vast library, integrating the complete library in our code may affect the performance of our application in various ways. To avoid this, component optimization in Bootstrap helps us optimize. Instead of importing the entire library, which translates into a reduction in efficiency, we can choose only the modules that are used in the project. Thanks to this, we significantly reduce the amount of KB downloaded by the application. It can be reduced even several times!
Ways to Optimize Components in Bootstrap
Some ways to optimize components in Bootstrap are:
Only Download the Bootstrap Package You Need:
If you decide to work with the precompiled download package of Bootstrap, you should seriously think about which parts of the library you need.
The download folder contains both the complete CSS library (bootstrap.css and bootstrap.min.css) and the JavaScript components library with all its dependencies except for jQuery (bootstrap.bundle.js and bootstrap.bundle.min.js), as well as several standalone CSS files containing the code necessary for specific parts of this popular UI kit.
If you just need a nice CSS reset for your project, simply use bootstrap-reboot.min.css. If you only need a flexible and easy-to-use grid system, then go for bootstrap-grid.min.css. There’s no need to download the entire framework. If, on the other hand, you know you’re going to use everything in the library, at least make sure you include the minified version.
Similarly with the JavaScript code. If you know you’re not going to have dropdowns, popovers and tooltips in your project, then use bootstrap.min.js rather than bootstrap.bundle.min.js because you won’t need to include Popper.js.
Opt for the Source Rather Than the Precompiled Download Package:
As much as the latest release of Bootstrap lets you select parts of it to include in your project, the precompiled files might still contain stuff that you might not need.
Browsers still have to download and process unused code, which could impact on website performance, especially on slow network connections.
A better idea would be to download the Bootstrap source code, because:
- you’ll be able to include exactly the components you need in your project
- customizing any part of the library becomes cleaner and more efficient, with no need to override styles over and over
- The stylesheet that ends up in production is usually leaner.
Make Use of Proven Client-side Optimization Techniques:
Apart from the points made above, optimizing a website built on top of Bootstrap for performance still has to incorporate front-end performance techniques just like any other website.
Minify and Concatenate CSS and JavaScript Code:
An important optimization step is to limit the number of HTTP requests a website has to make to render its content. Each round trip to the server and back to fetch resources takes time, thereby negatively impacting the user experience.
Minifying (that is, removing comments and white space from your document) and concatenating CSS and JavaScript files have now become a consolidated practice that aims to keep file size small and decrease the number of HTTP requests.
Watch Out for Your Images File Size:
The weightiest part of a web page is often represented by image files, but also audio and video files play their part. Optimizing visual assets is therefore crucial to website performance.
Doing so involves two aspects:
- Make sure you use the right image format for the job at hand.
- Squeezing excess bytes from your assets before uploading them for production. There are great tools out there that can help you. Check out online tools like TinyPNG for raster images (PNG, JPG, etc.) and Jake Archibald’s SSVGOMG for SVG optimization. Also, consider tools you can install locally like your favourite task runner (Grunt, Gulp, etc.).
Lean Sass Imports
When using Sass in your asset pipeline, optimize Bootstrap by only @importing the necessary components. Your largest optimizations will likely come from our bootstrap's Layout & Components section.scss.If you’re not using a component, comment it out or delete it entirely. , For example,, if you’re not using the carousel, remove that import to save some file size in your compiled CSS. Keep in mind some dependencies across Sass imports may make it more difficult to omit a file.
Lean JavaScript
Bootstrap’s JavaScript includes every component in our primary dist files (bootstrap.js and bootstrap.min.js), and even our primary dependency (Popper) with our bundle files (bootstrap.bundle.js and bootstrap.bundle.min.js). While you’re customizing via Sass, be sure to remove related JavaScript.For instance, assuming you’re using your own JavaScript bundler like Webpack or Rollup, you’d only import the JavaScript you plan on using. This way, you’re not including any JavaScript you don’t intend to use for components like buttons, carousels, and tooltips. If you’re importing dropdowns, tooltips or popovers, be sure to list the Popper dependency in your package.json file.
Autoprefixer .browserslistrc
Bootstrap depends on Autoprefixer to automatically add browser prefixes to certain CSS properties. Prefixes are dictated by our .browserslistrc file, found in the root of the Bootstrap repo. Customizing this list of browsers and recompiling the Sass will automatically remove some CSS from your compiled CSS if there are vendor prefixes unique to that browser or version.
Minify and Gzip
Whenever possible, be sure to compress all the code you serve to your visitors. If you’re using Bootstrap dist files, try to stick to the minified versions (indicated by the .min.css and .min.js extensions). If you’re building Bootstrap from the source with your build system, be sure to implement your minifiers for HTML, CSS, and JS.
Nonblocking Files
While minifying and using compression might seem like enough, making your files nonblocking is also a big step in making your site well-optimized and fast enough.
If you are using a Lighthouse plugin in Google Chrome, you may have stumbled over FCP. The First Contentful Paint metric measures the time from when the page starts loading to when any part of the page’s content is rendered on the screen.
You can improve FCP by deferring non-critical JavaScript or CSS. What does that mean? Simply, JavaScript or stylesheets that don’t need to be present on the first paint of your page should be marked with async or defer attributes.
Always Use HTTPS
Your website should only be available over HTTPS connections in production. HTTPS improves the security, privacy, and availability of all sites, and there is no such thing as non-sensitive web traffic. The steps to configure your website to be served exclusively over HTTPS vary widely depending on your architecture and web hosting provider, and thus are beyond the scope of these docs.
Sites served over HTTPS should also access all stylesheets, scripts, and other assets over HTTPS connections. Otherwise, you’ll be sending users mixed active content, leading to potential vulnerabilities where a site can be compromised by altering a dependency. This can lead to security issues and in-browser warnings displayed to users. Whether you’re getting Bootstrap from a CDN or serving it yourself, ensure that you only access it over HTTPS connections.
Conclusion
- Bootstrap is a large front-end CSS framework that offers varieties of functionalities.
- But most websites don’t need all of those functionalities that come with the Bootstrap bundle.
- Website optimization is a cluster of techniques, both server-side and client-side, which aim at speeding up websites’ loading time and rendering in the browser for a better user experience.
- Instead of importing the entire library, which translates into a reduction in efficiency, we can choose only the modules that are used in the project.
- Some ways to optimize components in Bootstrap include: minify and concatenate css and js, Lean js, Lean CSS, Always using HTTPS, minify and gzip.