From 4372524f85d407d5bc6b521b62ed0d1d3479e5f9 Mon Sep 17 00:00:00 2001 From: Soroosh Taefi Date: Wed, 15 May 2024 11:20:43 +0300 Subject: [PATCH] fix: ignore absence of file-routes.json at startup in dev mode (#2423) * 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 --------- Co-authored-by: Zhe Sun <31067185+ZheSun88@users.noreply.github.com> Co-authored-by: Artur --- .../hilla/route/ClientRouteRegistry.java | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/java/endpoint/src/main/java/com/vaadin/hilla/route/ClientRouteRegistry.java b/packages/java/endpoint/src/main/java/com/vaadin/hilla/route/ClientRouteRegistry.java index 7ac199d79a..801c59421a 100644 --- a/packages/java/endpoint/src/main/java/com/vaadin/hilla/route/ClientRouteRegistry.java +++ b/packages/java/endpoint/src/main/java/com/vaadin/hilla/route/ClientRouteRegistry.java @@ -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; @@ -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, @@ -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, @@ -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'"); + } } } @@ -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); }