Skip to content

Commit d8fba2d

Browse files
authored
feat: Open tab in dev mode less often (#19270)
* 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
1 parent e333cf6 commit d8fba2d

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

flow-server/src/main/java/com/vaadin/flow/server/InitParameters.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,12 @@ public class InitParameters implements Serializable {
271271
* Configuration name for cleaning or leaving frontend files in build.
272272
*/
273273
public static final String CLEAN_BUILD_FRONTEND_FILES = "vaadin.clean.build.frontend.files";
274+
275+
/**
276+
* Configuration name for how long since last browser open before we open a
277+
* new tab for the application in development mode.
278+
*
279+
* Time is given in minutes.
280+
*/
281+
public static final String LAUNCH_BROWSER_DELAY = "launch-browser-delay";
274282
}

vaadin-dev-server/src/main/java/com/vaadin/base/devserver/BrowserLauncher.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package com.vaadin.base.devserver;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.util.concurrent.TimeUnit;
7+
38
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
510

611
import com.vaadin.flow.server.VaadinContext;
712
import com.vaadin.flow.server.startup.ApplicationConfiguration;
813
import com.vaadin.open.Open;
914

15+
import static com.vaadin.flow.server.InitParameters.LAUNCH_BROWSER_DELAY;
16+
1017
/**
1118
* Util for launching a browser instance.
1219
*/
@@ -52,11 +59,43 @@ private boolean isProductionMode() {
5259
return applicationConfiguration.isProductionMode();
5360
}
5461

55-
private static boolean isLaunched() {
62+
private boolean isLaunched() {
63+
File launchFile = getLaunchFile();
64+
ApplicationConfiguration applicationConfiguration = ApplicationConfiguration
65+
.get(context);
66+
int lastModifiedDelay = Integer.parseInt(applicationConfiguration
67+
.getStringProperty(LAUNCH_BROWSER_DELAY, "30"));
68+
// If launch file exists and is younger than time update file content
69+
// and modified stamp
70+
if (launchFile.exists()
71+
&& TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis()
72+
- launchFile.lastModified()) < lastModifiedDelay) {
73+
writeLaunchFile(launchFile);
74+
return true;
75+
}
5676
return LAUNCHED_VALUE.equals(System.getProperty(LAUNCH_TRACKER));
5777
}
5878

59-
private static void setLaunched() {
79+
private void writeLaunchFile(File launchFile) {
80+
try {
81+
Files.writeString(launchFile.toPath(),
82+
Long.toString(System.currentTimeMillis()));
83+
} catch (IOException e) {
84+
getLogger().debug("Failed to write browser launched file.", e);
85+
}
86+
}
87+
88+
private File getLaunchFile() {
89+
ApplicationConfiguration applicationConfiguration = ApplicationConfiguration
90+
.get(context);
91+
File buildFolder = new File(applicationConfiguration.getProjectFolder(),
92+
applicationConfiguration.getBuildFolder());
93+
return new File(buildFolder, "tab.launch");
94+
}
95+
96+
private void setLaunched() {
97+
// write launch file and update modified timestamp.
98+
writeLaunchFile(getLaunchFile());
6099
System.setProperty(LAUNCH_TRACKER, LAUNCHED_VALUE);
61100
}
62101

vaadin-spring/src/main/java/com/vaadin/flow/spring/VaadinConfigurationProperties.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ public static List<String> getExcludedUrls(Environment environment) {
121121
*/
122122
private boolean launchBrowser = false;
123123

124+
/**
125+
* Timeout until a new browser should be launched on startup when in
126+
* development mode.
127+
*/
128+
private int launchBrowserDelay = 30;
129+
124130
/**
125131
* URL patterns that should not be handled by the Vaadin servlet when mapped
126132
* to the context root.
@@ -399,6 +405,30 @@ public void setLaunchBrowser(boolean launchBrowser) {
399405
this.launchBrowser = launchBrowser;
400406
}
401407

408+
/**
409+
* Returns timout after which a browser should be launched again on startup
410+
* when in development mode.
411+
* <p>
412+
*
413+
* @return if a browser should be launched on startup when in development
414+
* mode
415+
*/
416+
public int getLaunchBrowserDelay() {
417+
return launchBrowserDelay;
418+
}
419+
420+
/**
421+
* Sets timout for launching a new browser on startup when in development
422+
* mode.
423+
*
424+
* @param launchBrowser
425+
* {@code true} to launch a browser on startup when in
426+
* development mode, {@code false} otherwise
427+
*/
428+
public void setLaunchBrowserDelay(int launchBrowser) {
429+
this.launchBrowserDelay = launchBrowser;
430+
}
431+
402432
/**
403433
* Returns whether class scan caching between reloads when using Spring Boot
404434
* DevTools should be enabled.

0 commit comments

Comments
 (0)