While the accepted answer is the correct one to my question, I found a solution that I want to share with others and possibly my future self.
Since in Vaadin 8 it is not possible to directly add JavaScript into the generated HTML output (see here and here) it was easier for me to change the application slightly.
So in the application I added code like this. I am aware it does not check if the request was going to localhost - yet it checks that the client is on the same machine as the server, which essentially means the same.
public class App extends UI implements View {
@Override
protected void init(VaadinRequest request) {
if ("127.0.0.1".equals(request.getRemoteAddr()) || "localhost".equals(request.getRemoteAddr())) {
this.addStyleName("dev-environment");
}
}
}
Now as there is a new CSS class name in use, a simple tweak on the CSS did the trick:
// emphasize we are on localhost. The application sets the class name 'dev-environment' in this case
.dev-environment {
background-color: #EEF0FF;
}