diff --git a/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java b/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java index e33908e7485..bba841b87c8 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java @@ -219,23 +219,31 @@ public boolean synchronizedHandleRequest(VaadinSession session, private void initializeFeatureFlags(Document indexDocument, VaadinRequest request) { String script = featureFlagsInitializer(request); + if (script.isEmpty()) { + return; + } Element scriptElement = indexDocument.head().prependElement("script"); scriptElement.attr(SCRIPT_INITIAL, ""); scriptElement.appendChild(new DataNode(script)); } static String featureFlagsInitializer(VaadinRequest request) { - return FeatureFlags.get(request.getService().getContext()).getFeatures() - .stream().filter(Feature::isEnabled) - .map(feature -> String.format("activator(\"%s\");", - feature.getId())) - .collect(Collectors.joining("\n", - """ - window.Vaadin = window.Vaadin || {}; - window.Vaadin.featureFlagsUpdaters = window.Vaadin.featureFlagsUpdaters || []; - window.Vaadin.featureFlagsUpdaters.push((activator) => { - """, - "});")); + String activations = FeatureFlags + .get(request.getService().getContext()).getFeatures().stream() + .filter(Feature::isEnabled).map(feature -> String + .format("activator(\"%s\");", feature.getId())) + .collect(Collectors.joining("\n")); + + if (activations.isEmpty()) { + return ""; + } + + return """ + window.Vaadin = window.Vaadin || {}; + window.Vaadin.featureFlagsUpdaters = window.Vaadin.featureFlagsUpdaters || []; + window.Vaadin.featureFlagsUpdaters.push((activator) => { + """ + + activations + "\n});"; } private static void addDevBundleTheme(Document document, diff --git a/flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java b/flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java index 86c5dfff8cc..2b32a0b1ed6 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandlerTest.java @@ -218,9 +218,9 @@ public void serveIndexHtml_featureFlagsSetter_isPresent() indexHtmlRequestHandler.synchronizedHandleRequest(session, createVaadinRequest("/"), response); String indexHtml = responseOutput.toString(StandardCharsets.UTF_8); - assertTrue(indexHtml.contains( + assertFalse(indexHtml.contains( "window.Vaadin.featureFlagsUpdaters.push((activator) => {"), - "Response should have Feature Flags updater function"); + "Response should not have Feature Flags updater function when no flags are enabled"); } @Test diff --git a/flow-server/src/test/java/com/vaadin/flow/server/communication/WebComponentProviderTest.java b/flow-server/src/test/java/com/vaadin/flow/server/communication/WebComponentProviderTest.java index d9d0745a21d..cfa2e6bdf1e 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/communication/WebComponentProviderTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/communication/WebComponentProviderTest.java @@ -209,9 +209,9 @@ void webComponentGenerator_responseGetsResult() throws IOException { provider.synchronizedHandleRequest(session, request, response), "Provider should handle web-component request"); - assertTrue(out.toString().contains( + assertFalse(out.toString().contains( "window.Vaadin.featureFlagsUpdaters.push((activator) => {"), - "Response should have Feature Flags updater function"); + "Response should not have Feature Flags updater function when no flags are enabled"); Mockito.verify(response).getOutputStream(); Mockito.verify(out).write(Mockito.any(), Mockito.anyInt(),