Philipp Hauer's Blog

Engineering Management, Java Ecosystem, Kotlin, Sociology of Software Development

Vaadin 10+: SASS Integration and CSS Refresh during Development

Posted on Apr 15, 2019. Updated on Apr 17, 2019

Since Vaadin 10, SASS is no longer supported out of the box. Fortunately, it’s really easy to integrate SASS in a Vaadin 10+ app and its Maven build. But how can we ensure a fast feedback cycle during the SASS development? Restarting the app is cumbersome. Even a page reload would reset the UI state, which leads to annoying clicking through the app. Luckily, there is an approach that automatically exchanges the changed CSS without any browser refresh or app restart.

Vaadin 10+: SASS Integration and CSS Refresh during Development


The effect of a SASS change is immediately visible without any page reload or restart.

The effect of a SASS change is immediately visible without any page reload or restart.

  • Integrating SASS in a Vaadin 10+ app and its Maven build is easy
  • After a change in a SASS file, we can automatically trigger the SASS compilation and replace the new CSS in the browser. That happens without any page reload or application restart. This way, we don’t lose our UI state and don’t have to click through our app again and again.

SASS Integration

Let’s assume we are using Vaadin in conjunction with Spring Boot.

First, we put some SASS files under /src/main/resources/META-INF/resources/frontend/styles.

│   └── resources
│       ├── frontend
│       │   ├── images
│       │   └── styles
│       │       ├── exampleView.scss
│       │       ├── main.scss
│       │       └── variables.scss
│       └── js
│           └── cssrefresh.js
├── static
└── templates

Second, we add the sass-maven-plugin to the pom.xml.


Now, we can trigger the SASS compilation with

mvn sass:update-stylesheets

This will create a main.css. Finally, we only have to point to this CSS file in our RouterLayout.

@Viewport("width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes")
public class MainLayout extends Div implements RouterLayout {
    public MainLayout() {

Moreover, it’s important to refer to the MainLayout in every view:

@Route(value = "", layout = MainLayout.class)
public class ExampleView extends VerticalLayout {
    public ExampleView(){

Voila, we successfully integrated SASS into our Vaadin 10+ application.

CSS Refresh Without a Page Reload During Development

The sass-maven-plugin provides the watch goal that watches our SASS files for changes and automatically recompiles them to CSS.

mvn sass:watch

But how can we update the changed CSS in our browser on-the-fly? This is where Tony Findeisen’s cssrefresh comes in handy. Put this script under /src/main/resources/META-INF/resources/js/cssrefresh.js.

(function() {
    function cssRefresh(link) {
        let href = link.href + '?x=' + Math.random();

        fetch(href, {
            method: 'HEAD'
        .then(response => {
            let lastModified = new Date(response.headers.get('last-modified'));
            if (response.ok && (lastModified > link.lastModified || !link.lastModified)) {
                link.lastModified = lastModified;
                link.element.setAttribute('href', href);

            setTimeout(() => {
            }, 1000);
        .filter(link => link.rel === 'stylesheet' && link.href)
        .map(link => {
            return {
                element: link,
                href: link.getAttribute('href').split('?')[0],
                lastModified: false
        .forEach(function(link) {

This script checks every second if a css file has been changed (using the last-modified header). If yes, it replaces the old CSS with the new one.

The best thing is that no page reload is required. This is especially useful because we don’t lose our UI state and can immediately see the effect of a SASS/CSS change. A browser refresh might force us to manually click through the UI again and again up to the point where the CSS changes are visible. That’s a huge relief.

The last step is to integrate cssrefresh in our Vaadin app. We want this script to be only active in the debug mode. It should be disabled in production mode. For this, just implement VaadinServiceInitListener and add cssrefresh to the initial DOM of the Vaadin application.

public class CustomVaadinServiceListener implements VaadinServiceInitListener {
    public void serviceInit(ServiceInitEvent event) {
        if (!event.getSource().getDeploymentConfiguration().isProductionMode()) {
            event.addBootstrapListener(new CustomBootstrapListener());
    static class CustomBootstrapListener implements BootstrapListener {
        public void modifyBootstrapPage(BootstrapPageResponse response) {
            Element head = response.getDocument().head();
            head.append("<script type=\"text/javascript\" src=\"/js/cssrefresh.js\"></script>");

Now we are ready to go.

  • Start the application
  • Run mvn sass:watch
  • Change a SASS file. Save changes.
  • Profit (= the CSS is automatically updated in the browser without any page refresh)


  • If you are using the @StyleSheet annotation on Vaadin components, the referred CSS files are only loaded at the point when the component is visible in the UI. Currently, cssrefresh won’t detect changes in those CSS files. But it’s possible to adapt the script to watch out for new <link> tags. However, we don’t use this annotation on components because we use a global CSS file which is included once and contains all styles for all components. Honestly, we don’t care about premature payload optimizations for internal tools. Moreover, we are still able to modularize our stylesheets with the capabilities of SASS.
  • Spring Security will block the path /js/cssrefresh.js on the login view because you are not logged in yet. Ignoring this path via web.ignoring().antMatchers("/js/cssrefresh.js") in your WebSecurityConfigurerAdapter.configure(WebSecurity) implementation does the trick. It makes sense to only apply this matcher when in debug mode.
  • If you are using IntelliJ IDEA, you may run into troubles where the changes in IDEA are not detected by the sass plugin. In this case, try Ctrl+Shift+F9 instead of Ctrl+S after changing the file. Or try to uncheck Use "safe write" under File > Settings: Appearance & Behavior > System Settings.

Source Code

The source code can be found in the project vaadin-10-sass-cssrefresh on GitHub. It uses Vaadin 13 and Spring Boot 2.1.


I like to thank my collegues