Skip to content

Commit

Permalink
fix: register RouteUnifyingIndexHtmlRequestListener only when React i…
Browse files Browse the repository at this point in the history
…s enabled (#2264)

fix: register RouteUnifyingIndexHtmlRequestListener when React is enabled

Also, this changes the behavior of client route registration
so that when the views.json is not found, it only logs the
absence of views.json and is gracefully skips client route
registration. This is necessary as an application might have
React enabled, but it is not necessarily using the FS Router.
  • Loading branch information
taefi authored Mar 27, 2024
1 parent d959da5 commit b9eb41e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -116,30 +117,43 @@ public ClientViewConfig getRouteByPath(String path) {
*/
public void registerClientRoutes(
DeploymentConfiguration deploymentConfiguration) {
try (var source = getViewsJsonAsResource(deploymentConfiguration)
.openStream()) {
var viewsJsonAsResource = getViewsJsonAsResource(
deploymentConfiguration);
if (viewsJsonAsResource == null || !Paths
.get(viewsJsonAsResource.getPath()).toFile().exists()) {
LOGGER.debug(
"No 'views.json' found either in the frontend/generated "
+ "folder or in the META-INF/VAADIN folder. Skipping client "
+ "route registration.");
return;
}
try (var source = viewsJsonAsResource.openStream()) {
if (source != null) {
clearRoutes();
registerAndRecurseChildren("",
mapper.readValue(source, new TypeReference<>() {
}));
} else {
LOGGER.warn("Failed to find views.json");
}
} catch (IOException e) {
LOGGER.warn("Failed extract client views from views.json", e);
LOGGER.warn("Failed load client views from {}",
viewsJsonAsResource.getPath(), e);
}
}

private URL getViewsJsonAsResource(
DeploymentConfiguration deploymentConfiguration)
throws MalformedURLException {
DeploymentConfiguration deploymentConfiguration) {
var isProductionMode = deploymentConfiguration.isProductionMode();
if (isProductionMode) {
return getClass().getResource("/META-INF/VAADIN/views.json");
}
return deploymentConfiguration.getFrontendFolder().toPath()
.resolve("generated").resolve("views.json").toUri().toURL();
try {
return deploymentConfiguration.getFrontendFolder().toPath()
.resolve("generated").resolve("views.json").toUri().toURL();
} catch (MalformedURLException e) {
LOGGER.warn("Failed to find views.json under frontend/generated",
e);
throw new RuntimeException(e);
}
}

private void registerAndRecurseChildren(String basePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ public RouteUnifyingServiceInitListener(
public void serviceInit(ServiceInitEvent event) {
var deploymentConfiguration = event.getSource()
.getDeploymentConfiguration();
var routeExtractionIndexHtmlRequestListener = new RouteUnifyingIndexHtmlRequestListener(
clientRouteRegistry, deploymentConfiguration);
var deploymentMode = deploymentConfiguration.isProductionMode()
? "PRODUCTION"
: "DEVELOPMENT";
event.addIndexHtmlRequestListener(
routeExtractionIndexHtmlRequestListener);
LOGGER.debug(
"{} mode: Registered RouteUnifyingIndexHtmlRequestListener.",
deploymentMode);
if (deploymentConfiguration.isReactEnabled()) {
var routeUnifyingIndexHtmlRequestListener = new RouteUnifyingIndexHtmlRequestListener(
clientRouteRegistry, deploymentConfiguration);
var deploymentMode = deploymentConfiguration.isProductionMode()
? "PRODUCTION"
: "DEVELOPMENT";
event.addIndexHtmlRequestListener(
routeUnifyingIndexHtmlRequestListener);
LOGGER.debug(
"{} mode: Registered RouteUnifyingIndexHtmlRequestListener.",
deploymentMode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,30 @@ public void setup() throws IOException {
}

@Test
public void should_addRouteIndexHtmlRequestListener() {
Assert.assertFalse("Unexpected RouteIndexHtmlRequestListener added",
eventHasAddedRouteIndexHtmlRequestListener(event));
public void should_addRouteIndexHtmlRequestListener_when_react_is_enabled() {
Mockito.when(mockDeploymentConfiguration.isReactEnabled())
.thenReturn(true);

Assert.assertFalse("Unexpected RouteUnifyingServiceInitListener added",
hasRouteUnifyingIndexHtmlRequestListenerAdded(event));
routeUnifyingServiceInitListener.serviceInit(event);
Assert.assertTrue(
"Expected event to have RouteIndexHtmlRequestListener added",
eventHasAddedRouteIndexHtmlRequestListener(event));
"Expected to have RouteUnifyingServiceInitListener added",
hasRouteUnifyingIndexHtmlRequestListenerAdded(event));
}

@Test
public void should_not_addRouteIndexHtmlRequestListener_when_react_is_not_enabled() {
Mockito.when(mockDeploymentConfiguration.isReactEnabled())
.thenReturn(false);

routeUnifyingServiceInitListener.serviceInit(event);
Assert.assertFalse(
"RouteIndexHtmlRequestListener added unexpectedly when React is not enabled",
hasRouteUnifyingIndexHtmlRequestListenerAdded(event));
}

private boolean eventHasAddedRouteIndexHtmlRequestListener(
private boolean hasRouteUnifyingIndexHtmlRequestListenerAdded(
ServiceInitEvent event) {
return event.getAddedIndexHtmlRequestListeners().anyMatch(
indexHtmlRequestListener -> indexHtmlRequestListener instanceof RouteUnifyingIndexHtmlRequestListener);
Expand Down

0 comments on commit b9eb41e

Please sign in to comment.