Modifying how dependencies are loaded with DependencyFilters

As seen on the tutorials about using @JavaScript, @CssImport and @StyleSheet (see Importing JavaScript and Importing Style Sheets), you can use annotations or an imperative API to add resources (or dependencies) to your application when needed. But in some cases, a more fine control is needed: for example, when bundling resources into multiple different bundles, you may want to control the application to import the right bundle when some specific resource is requested.

To control how the dependencies are loaded, and which files are effectively added or removed from the loading process, you can use DependencyFilters.

Here is one example - it removes all dependencies and add one single bundle when running in production mode:

public class BundleFilter implements DependencyFilter {
    public List<Dependency> filter(List<Dependency> dependencies,
            VaadinService service) {

        if (service.getDeploymentConfiguration().isProductionMode()) {
            dependencies.add(new Dependency(Dependency.Type.STYLESHEET,
                        "my-style.css", LoadMode.EAGER));

        return dependencies;
You can also use the context:// and base:// protocols on dependencies returned by the DependencyFilter. These protocols are resolved after the filters are applied. The context:// protocol is resolved to the servlet context root and the base:// protocol is resolved to a base URI of the loaded page.

The DependencyFilters are called in two particular situations: when a LitTemplate is parsed for the first time, and when a set of dependencies are about to be sent to the client.

  • When a route changes and a new set of components are requested, all dependencies are gathered in a list and sent to the filters for evaluation. The filters can change, remove or add new dependencies as needed.

DependencyFilters allow you to change, add and remove any dependencies. You may leave your application in a broken state if you remove a required dependency for your project without providing a suitable replacement. With great power comes great responsibility.

With your DependencyFilter in place, you need to add it to a ServiceInitEvent which is sent when a Vaadin service is initialized. Take a look on the ServiceInitListener tutorial on how to configure it.