Clarification on Vaadin Flow Application Life Cycle

I am trying to understand the life cycle of Vaadin Flow applications. (I need this to think about some workaround for handling page reload).
I just added a Servlet and UI to the standard test application.
When I start the application from the IDE (I’m use IntelliJ IDEA with Jetty), UI is constructed 3 times. Every time I do a reload of the page, UI is getting created 2 times. Why it has to be like this?

package com.habibbank.syam;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.router.Route;

@Route("")
public class MainView extends Div {

    public MainView() {
        Button button = new Button("Click me", event -> Notification.show("Clicked!"));
        add(button);
    }
}
package com.habibbank.syam;

import com.vaadin.flow.server.VaadinServlet;
import com.vaadin.flow.server.VaadinServletConfiguration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

@WebServlet(urlPatterns = "/*", name = "TestServlet", loadOnStartup = 0)
@VaadinServletConfiguration(ui = TestUI.class, productionMode = false, closeIdleSessions = true)
public class TestServlet extends VaadinServlet {

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        System.err.println("Init happened");
    }
}
package com.habibbank.syam;

import com.vaadin.flow.component.UI;

public class TestUI extends UI {

    public TestUI() {
        System.err.println("UI created");
    }
}

Does this happen only ehen running inside IntelliJ?

I just tested it with

mvn jetty:run-exploded -Pproduction

after making production mode and it’s happening there too.

@VaadinServletConfiguration(ui = TestUI.class, productionMode = true, closeIdleSessions = true)

Is there any update on this?
The issue is still there in the latest Production release 12.0.1

Tested by pasting your code into https://vaadin.com/start/latest/project-base

Running as

mvn jetty:run

and opening the URL results in

UI created

Running as

mvn jetty:run-exploded -PproductionMode

and opening the URL results in

UI created

For every refresh, only one additional UI created is printed to the terminal

This is quite perplexing!!

I did the same but I am always getting “UI created” 3 times when the URL is opened for the first time and “UI created” 2 times whenever I refreshed it!!

I have the same pom.xml generated by the vaadin/starter with absolutely no changes!

What could be the possibilities for such a behaviour? Any clues?

Ok, here is an update on this issue:

This is happening in Chrome and it works as properly in Firefox. (I am using Linux and I don’t have access to other browsers now, so not tested on other browsers).

Any idea why it’s happening this way in Chrome?

Finally, I found the real issue:

I was accessing the application via localhost and Chrome doesn’t store cookies for localhost (It doesn’t store cookies if the domain doesn’t contain any dots). So, without cookies, Vaadin Flow was not able to keep track of the sessions properly.

The workaround is to use 0.0.0.0 or the local IP address while testing it in Chrome.