Add Js and Css only if component is used in the theme

If I use addJs and AddCss in the onRun function, it’s always added to the layout, even if the markup component is not used. {% component 'demo' %} in the theme.

Is there a way to use the JS and CSS injection only if the markup component is used in the theme?

Thanks in advance

Hi @sanPuerquitoPrograma

The demo theme has something like this via the resources component. It only injects CSS and JS when the partial is used

[resources]
less[] = "blocks/team-leaders.less"
js[] = "blocks/team-leaders.js"
==
<div class="block-team-leaders">...</div>

It’s not quite the same but it could give some ideas.

1 Like