One of the challenges I faced when leveraging lazy loading feature is finding a location for the common styles (css or scss) that are specific to a module, which can then be lazy loaded at the same time as the feature module gets loaded.
One of the solutions I found from a bit of Googling was to lazy load the scss files by configuring them in angular.json and building it as a separate file. This then has to be injected to the DOM from one of the component under the feature module. You can find more about this approach in here.
This solution would work, but going through the other features provided by Angular, I found a much easier way to lazy load these styles only when the feature is loaded.
ViewEncapsulation is the feature I used additionally to lazy load the styles. It has three encapsulation strategies and None is the encapsulation strategy I used.
ViewEncapsulation is a setting provided to the component which decides whether the template and style defined within the component itself can affect the whole application or not.
Angular provides three active encapsulation strategies as follows:
- Emulated : Styles defined in the component are scoped only to that component without conflicting with other components. This is the default option.
- None : Do not provide any kind of encapsulation.
- ShadowDom : Uses a Shadow DOM to encapsulate styles. You can find more about ShadowDom in here.
If you are not familiar with ViewEncapuslation, you can find more about it in Angular documentation.
In order to lazy load the styles with the feature module, we can have a dumb root component where it will have the child components. If you have more than one routing in the feature module you can have that component as a base component to others.
Lets see my project structure:
According to the above project structure, I have two feature modules.
In user-management module I have two components, users and roles. I have common styles which will be used in these two components.
I then have two separate routing for these two components in the feature module. Therefore I will be using a base component to have the common styles.
Let see the routing files:
Place all the common styles specific to that feature in the relevant scss file associated with that base component.
As you can see, the html file only contains the router outlet. This will be used as a base component in the feature module.
Set the encapsulation property of that base component to ViewEncapuslation.None.
And that sums it up! 😊
Following are some of my findings from using this approach:
- There’s no need to duplicate the common styles.
- Common styles specific to the feature module is loaded only when the feature is loaded.
- Less number of configurations.
- No need to manually inject the styling to the DOM.
With ViewEncapuslation set to None, these styles will be available globally once the module is lazy loaded and it might conflict with the global styles. This will happen whether you follow this way to lazy load the styles or by injecting to DOM dynamically. Hence, we need to make sure that those common style classes specific to the module are uniquely named or you can just easily wrap all the common styles specific to the module under a unique selector.
To avoid conflicts, you can also use the ShadowDom encapsulation strategy to create a separate shadow DOM. This entirely separates the component styles from the global styles but global styles which need to be applied to the feature modules won’t be applied.
With all these new strategies, you can choose the best option according to your requirements.
To lazy load the common styles specific to a feature module, you can use the ViewEncapuslation feature without manually injecting the styles to DOM. You can set the encapsulation strategy as None for the base component or root component of the feature and place all the common styles in a stylesheet linked to that component. This will make sure that styles are loaded only when the feature module is loaded.
Source code for the demo project can be found in here.
Thank you for reading and hope this would come in handy to you!
Happy lazy loading. 😍
THIS AD MAKES CONTENT FREE