Skip to content

Commit

Permalink
fix: ignore absence of file-routes.json at startup in dev mode (#2423)
Browse files Browse the repository at this point in the history
* fix: ignore absence of file-routes.json at startup in dev mode

Fixes #2408

* apply suggestions from code review

* Update packages/java/endpoint/src/main/java/com/vaadin/hilla/route/ClientRouteRegistry.java

Co-authored-by: Artur <artur@vaadin.com>

---------

Co-authored-by: Zhe Sun <31067185+ZheSun88@users.noreply.github.com>
Co-authored-by: Artur <artur@vaadin.com>
  • Loading branch information
3 people authored May 15, 2024
1 parent fc28395 commit 4372524
Showing 1 changed file with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.server.frontend.FrontendUtils;
import com.vaadin.hilla.route.records.ClientViewConfig;

import org.slf4j.Logger;
Expand Down Expand Up @@ -144,9 +145,9 @@ private String removeTrailingSlash(String path) {
public synchronized void registerClientRoutes(
DeploymentConfiguration deploymentConfiguration,
LocalDateTime lastUpdated) {
var viewsJsonAsResource = getViewsJsonAsResource(
var fileRoutesJsonAsResource = getFileRoutesJsonAsResource(
deploymentConfiguration);
if (viewsJsonAsResource == null) {
if (fileRoutesJsonAsResource == null) {
LOGGER.debug(
"No {} found under {} directory. Skipping client route registration.",
FILE_ROUTES_JSON_NAME,
Expand All @@ -155,7 +156,7 @@ public synchronized void registerClientRoutes(
: "'frontend/generated'");
return;
}
try (var source = viewsJsonAsResource.openStream()) {
try (var source = fileRoutesJsonAsResource.openStream()) {
if (source != null) {
clearRoutes();
mapper.readValue(source,
Expand All @@ -165,8 +166,16 @@ public synchronized void registerClientRoutes(
this.lastUpdated = lastUpdated;
}
} catch (IOException e) {
LOGGER.warn("Failed load {} from {}", FILE_ROUTES_JSON_NAME,
viewsJsonAsResource.getPath(), e);
if (deploymentConfiguration.isProductionMode()) {
// The file should be available in production mode:
LOGGER.error("Failed to load {} from {}", FILE_ROUTES_JSON_NAME,
fileRoutesJsonAsResource.getPath(), e);
} else {
LOGGER.debug(
"Failed to load {} from {}. Skipping client route registration. "
+ "There might be a problem with the contents of the file-routes.json file.",
FILE_ROUTES_JSON_NAME, "'frontend/generated'");
}
}
}

Expand All @@ -190,18 +199,25 @@ public synchronized void loadLatestDevModeFileRoutesJsonIfNeeded(
}
}

private URL getViewsJsonAsResource(
private URL getFileRoutesJsonAsResource(
DeploymentConfiguration deploymentConfiguration) {
var isProductionMode = deploymentConfiguration.isProductionMode();
if (isProductionMode) {
return getClass().getResource(FILE_ROUTES_JSON_PROD_PATH);
}
try {
return deploymentConfiguration.getFrontendFolder().toPath()
.resolve("generated").resolve(FILE_ROUTES_JSON_NAME).toUri()
.toURL();
var fileRoutesJson = FrontendUtils
.getFrontendGeneratedFolder(
deploymentConfiguration.getFrontendFolder())
.toPath().resolve(FILE_ROUTES_JSON_NAME).toFile();
if (!fileRoutesJson.exists()) {
return null;
} else {
return fileRoutesJson.toURI().toURL();
}
} catch (MalformedURLException e) {
LOGGER.warn("Failed to find {} under frontend/generated",
LOGGER.error(
"Unexpected error while getting {} as resource from 'frontend/generated'.",
FILE_ROUTES_JSON_NAME, e);
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 4372524

Please sign in to comment.