|
42 | 42 | @Component
|
43 | 43 | public class ClientRouteRegistry implements Serializable {
|
44 | 44 |
|
| 45 | + public static final String FILE_ROUTES_JSON_NAME = "file-routes.json"; |
| 46 | + public static final String FILE_ROUTES_JSON_PROD_PATH = "/META-INF/VAADIN/" |
| 47 | + + FILE_ROUTES_JSON_NAME; |
| 48 | + |
45 | 49 | /**
|
46 | 50 | * A map of registered routes and their corresponding client view
|
47 | 51 | * configurations with ordered insertion.
|
@@ -121,51 +125,59 @@ private String removeTrailingSlash(String path) {
|
121 | 125 | }
|
122 | 126 |
|
123 | 127 | /**
|
124 |
| - * Registers client routes from views.json file generated by the |
125 |
| - * file-router's Vite plugin. The views.json file is expected to be in the |
126 |
| - * frontend/generated folder in dev mode and in the META-INF/VAADIN folder |
127 |
| - * in production mode. |
| 128 | + * Registers client routes from file-routes.json file generated by the |
| 129 | + * file-router's Vite plugin. The file-routes.json file is expected to be in |
| 130 | + * the frontend/generated folder in dev mode and in the META-INF/VAADIN |
| 131 | + * folder in production mode. |
128 | 132 | *
|
129 | 133 | * @param deploymentConfiguration
|
130 | 134 | * the deployment configuration
|
| 135 | + * |
| 136 | + * @return {@code true} if the client routes were successfully registered, |
| 137 | + * {@code false} otherwise |
131 | 138 | */
|
132 |
| - public void registerClientRoutes( |
| 139 | + public boolean registerClientRoutes( |
133 | 140 | DeploymentConfiguration deploymentConfiguration) {
|
134 | 141 | var viewsJsonAsResource = getViewsJsonAsResource(
|
135 | 142 | deploymentConfiguration);
|
136 |
| - if (viewsJsonAsResource == null || !Paths |
137 |
| - .get(viewsJsonAsResource.getPath()).toFile().exists()) { |
| 143 | + if (viewsJsonAsResource == null) { |
138 | 144 | LOGGER.debug(
|
139 |
| - "No 'views.json' found either in the frontend/generated " |
140 |
| - + "folder or in the META-INF/VAADIN folder. Skipping client " |
141 |
| - + "route registration."); |
142 |
| - return; |
| 145 | + "No {} found under {} directory. Skipping client route registration.", |
| 146 | + FILE_ROUTES_JSON_NAME, |
| 147 | + deploymentConfiguration.isProductionMode() |
| 148 | + ? "'META-INF/VAADIN'" |
| 149 | + : "'frontend/generated'"); |
| 150 | + return false; |
143 | 151 | }
|
144 | 152 | try (var source = viewsJsonAsResource.openStream()) {
|
145 | 153 | if (source != null) {
|
146 | 154 | clearRoutes();
|
147 | 155 | registerAndRecurseChildren("",
|
148 | 156 | mapper.readValue(source, new TypeReference<>() {
|
149 | 157 | }));
|
| 158 | + return true; |
150 | 159 | }
|
| 160 | + return false; |
151 | 161 | } catch (IOException e) {
|
152 |
| - LOGGER.warn("Failed load client views from {}", |
| 162 | + LOGGER.warn("Failed load {} from {}", FILE_ROUTES_JSON_NAME, |
153 | 163 | viewsJsonAsResource.getPath(), e);
|
| 164 | + return false; |
154 | 165 | }
|
155 | 166 | }
|
156 | 167 |
|
157 | 168 | private URL getViewsJsonAsResource(
|
158 | 169 | DeploymentConfiguration deploymentConfiguration) {
|
159 | 170 | var isProductionMode = deploymentConfiguration.isProductionMode();
|
160 | 171 | if (isProductionMode) {
|
161 |
| - return getClass().getResource("/META-INF/VAADIN/views.json"); |
| 172 | + return getClass().getResource(FILE_ROUTES_JSON_PROD_PATH); |
162 | 173 | }
|
163 | 174 | try {
|
164 | 175 | return deploymentConfiguration.getFrontendFolder().toPath()
|
165 |
| - .resolve("generated").resolve("views.json").toUri().toURL(); |
| 176 | + .resolve("generated").resolve(FILE_ROUTES_JSON_NAME).toUri() |
| 177 | + .toURL(); |
166 | 178 | } catch (MalformedURLException e) {
|
167 |
| - LOGGER.warn("Failed to find views.json under frontend/generated", |
168 |
| - e); |
| 179 | + LOGGER.warn("Failed to find {} under frontend/generated", |
| 180 | + FILE_ROUTES_JSON_NAME, e); |
169 | 181 | throw new RuntimeException(e);
|
170 | 182 | }
|
171 | 183 | }
|
|
0 commit comments