Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: register RouteUnifyingIndexHtmlRequestListener only when React is enabled #2264

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading