# Include used material icons only

This article demonstrates a way to include just the material-design icons used. Since the material iconfont is about 44kB it is way too much to import the complete icon font for just a few icons. The other advantage is the control over the loading procedure. One is able to preload (or lazy load) each icon, or icon bunches, individually with the usage of service workers.
At the beginning your folder structure should be similar to the following example. In there all the regarding files are included, dropped all non-important files for this task.
At first we will have a look at the icons.module.ts. In here we import the HttpClientModule and add a forRoot() method to provide the service in the root component.
In the icon.ts (service) file inject the MatIconRegistry, the DomSanitizer and icons. For each icon we passed in in the application module the MatIconRegistry adds a keypair with an icon key (e.g. like account_circle or other mat icons) and a safeUrl.
Now its allmost done. Just import the IconsModule in the AppModule with the usage of the forRoot() function and pass in the icons and the assets folders path.
The icons variable is simply an array of strings that should match the pattern in between the filenames, e.g. baseline-account_circle-24px.svg. The icon config file app/config/icons.ts could look like
Then register the material icons where you need them, e.g. in the AppComponent.
Afterwards you can use the generated material icons using
In this page I included, at the stage of writing this article, 19 SVG material icons which a size range from 100 to 400 bytes. This results in a final output size s of
$1.9\text{kB} < s < 7.6\text{kB}$
The actual size is 4.4kB. The .woff2 material font is 44kB heavy. The library weights 1.6kB. The savings d are
$d= 44\text{kB} - 4.4\text{kB} - 1.2\text{kB} = 38.4\text{kB}$

## Conclusion

I think everybody has to dicide if 36kB are worth the effort for its own. Since I was interested for this website to include just necessary icons, I came up with this solution I wanted to share with you.