Skip to content

Commit

Permalink
feat: Open tab in dev mode less often (#19270)
Browse files Browse the repository at this point in the history
* feat: Open tab in dev mode less often

As we can not know if a tab
for the application is open
we add a file that is checked
for modification time and after
default 30 min we open a new tab on
server restart.

If server is restarted again before
timeout file timestamp is updated.

Clearing build folder will also
cause new tab to open on next restart.

Closes #18393

* Unify duplicate code

* Add configuration property and update parameter to launch-browser-delay
  • Loading branch information
caalador committed Apr 29, 2024
1 parent e333cf6 commit d8fba2d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,12 @@ public class InitParameters implements Serializable {
* Configuration name for cleaning or leaving frontend files in build.
*/
public static final String CLEAN_BUILD_FRONTEND_FILES = "vaadin.clean.build.frontend.files";

/**
* Configuration name for how long since last browser open before we open a
* new tab for the application in development mode.
*
* Time is given in minutes.
*/
public static final String LAUNCH_BROWSER_DELAY = "launch-browser-delay";
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.vaadin.base.devserver;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.flow.server.VaadinContext;
import com.vaadin.flow.server.startup.ApplicationConfiguration;
import com.vaadin.open.Open;

import static com.vaadin.flow.server.InitParameters.LAUNCH_BROWSER_DELAY;

/**
* Util for launching a browser instance.
*/
Expand Down Expand Up @@ -52,11 +59,43 @@ private boolean isProductionMode() {
return applicationConfiguration.isProductionMode();
}

private static boolean isLaunched() {
private boolean isLaunched() {
File launchFile = getLaunchFile();
ApplicationConfiguration applicationConfiguration = ApplicationConfiguration
.get(context);
int lastModifiedDelay = Integer.parseInt(applicationConfiguration
.getStringProperty(LAUNCH_BROWSER_DELAY, "30"));
// If launch file exists and is younger than time update file content
// and modified stamp
if (launchFile.exists()
&& TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis()
- launchFile.lastModified()) < lastModifiedDelay) {
writeLaunchFile(launchFile);
return true;
}
return LAUNCHED_VALUE.equals(System.getProperty(LAUNCH_TRACKER));
}

private static void setLaunched() {
private void writeLaunchFile(File launchFile) {
try {
Files.writeString(launchFile.toPath(),
Long.toString(System.currentTimeMillis()));
} catch (IOException e) {
getLogger().debug("Failed to write browser launched file.", e);
}
}

private File getLaunchFile() {
ApplicationConfiguration applicationConfiguration = ApplicationConfiguration
.get(context);
File buildFolder = new File(applicationConfiguration.getProjectFolder(),
applicationConfiguration.getBuildFolder());
return new File(buildFolder, "tab.launch");
}

private void setLaunched() {
// write launch file and update modified timestamp.
writeLaunchFile(getLaunchFile());
System.setProperty(LAUNCH_TRACKER, LAUNCHED_VALUE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ public static List<String> getExcludedUrls(Environment environment) {
*/
private boolean launchBrowser = false;

/**
* Timeout until a new browser should be launched on startup when in
* development mode.
*/
private int launchBrowserDelay = 30;

/**
* URL patterns that should not be handled by the Vaadin servlet when mapped
* to the context root.
Expand Down Expand Up @@ -399,6 +405,30 @@ public void setLaunchBrowser(boolean launchBrowser) {
this.launchBrowser = launchBrowser;
}

/**
* Returns timout after which a browser should be launched again on startup
* when in development mode.
* <p>
*
* @return if a browser should be launched on startup when in development
* mode
*/
public int getLaunchBrowserDelay() {
return launchBrowserDelay;
}

/**
* Sets timout for launching a new browser on startup when in development
* mode.
*
* @param launchBrowser
* {@code true} to launch a browser on startup when in
* development mode, {@code false} otherwise
*/
public void setLaunchBrowserDelay(int launchBrowser) {
this.launchBrowserDelay = launchBrowser;
}

/**
* Returns whether class scan caching between reloads when using Spring Boot
* DevTools should be enabled.
Expand Down

0 comments on commit d8fba2d

Please sign in to comment.