Skip to content

Commit fb92e69

Browse files
authored
fix: Serve correct bootstrap (#17478)
When having a project with application and WebComponents the served bootstrap should be served by the correct bootstrap handler and not by what is available. Fixes #17346
1 parent f80536c commit fb92e69

5 files changed

Lines changed: 129 additions & 33 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/BootstrapHandler.java‎

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public class BootstrapHandler extends SynchronizedRequestHandler {
132132
private static final String DEFER_ATTRIBUTE = "defer";
133133
static final String VIEWPORT = "viewport";
134134
private static final String META_TAG = "meta";
135-
private static final String SCRIPT_TAG = "script";
135+
protected static final String SCRIPT_TAG = "script";
136136

137137
/**
138138
* Location of client nocache file, relative to the context root.
@@ -1595,15 +1595,14 @@ protected static void setupPwa(Document document, VaadinService service) {
15951595
setupPwa(document, service.getPwaRegistry());
15961596
}
15971597

1598-
protected static void addGeneratedIndexContent(
1599-
DeploymentConfiguration config, Document targetDocument)
1598+
protected static JsonObject getStatsJson(DeploymentConfiguration config)
16001599
throws IOException {
16011600
String statsJson = DevBundleUtils
16021601
.findBundleStatsJson(config.getProjectFolder());
16031602
Objects.requireNonNull(statsJson,
16041603
"Frontend development bundle is expected to be in the project"
16051604
+ " or on the classpath, but not found.");
1606-
addGeneratedIndexContent(targetDocument, Json.parse(statsJson));
1605+
return Json.parse(statsJson);
16071606
}
16081607

16091608
/**
@@ -1674,33 +1673,6 @@ private static Element getStyleTag(String themeName, String fileName,
16741673
return element;
16751674
}
16761675

1677-
private static void addGeneratedIndexContent(Document targetDocument,
1678-
JsonObject statsJson) {
1679-
JsonArray indexHtmlGeneratedRows = statsJson
1680-
.getArray("indexHtmlGenerated");
1681-
List<String> toAdd = new ArrayList<>();
1682-
1683-
Optional<String> webComponentScript = JsonUtils
1684-
.stream(statsJson.getArray("entryScripts"))
1685-
.map(value -> value.asString())
1686-
.filter(script -> script.contains("webcomponenthtml"))
1687-
.findFirst();
1688-
1689-
if (webComponentScript.isPresent()) {
1690-
Element elm = new Element(SCRIPT_TAG);
1691-
elm.attr("type", "module");
1692-
elm.attr("src", webComponentScript.get());
1693-
toAdd.add(elm.outerHtml());
1694-
} else {
1695-
toAdd.addAll(JsonUtils.stream(indexHtmlGeneratedRows)
1696-
.map(value -> value.asString()).toList());
1697-
}
1698-
1699-
for (String row : toAdd) {
1700-
targetDocument.head().append(row);
1701-
}
1702-
}
1703-
17041676
private static void setupPwa(Document document, PwaRegistry registry) {
17051677
if (registry == null) {
17061678
return;

‎flow-server/src/main/java/com/vaadin/flow/server/communication/IndexHtmlRequestHandler.java‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.io.IOException;
1919
import java.io.Serializable;
2020
import java.io.UncheckedIOException;
21+
import java.util.ArrayList;
22+
import java.util.List;
2123
import java.util.Locale;
2224
import java.util.Optional;
2325

@@ -35,6 +37,7 @@
3537
import com.vaadin.flow.internal.BootstrapHandlerHelper;
3638
import com.vaadin.flow.internal.BrowserLiveReload;
3739
import com.vaadin.flow.internal.BrowserLiveReloadAccessor;
40+
import com.vaadin.flow.internal.JsonUtils;
3841
import com.vaadin.flow.internal.LocaleUtil;
3942
import com.vaadin.flow.internal.UsageStatisticsExporter;
4043
import com.vaadin.flow.internal.springcsrf.SpringCsrfTokenUtil;
@@ -54,6 +57,7 @@
5457
import com.vaadin.flow.server.startup.ApplicationConfiguration;
5558

5659
import elemental.json.Json;
60+
import elemental.json.JsonArray;
5761
import elemental.json.JsonObject;
5862
import elemental.json.impl.JsonUtil;
5963

@@ -436,12 +440,27 @@ private static Document getIndexHtmlDocument(VaadinService service)
436440
// When running without a frontend server, the index.html comes
437441
// directly from the frontend folder and the JS entrypoint(s) need
438442
// to be added
439-
addGeneratedIndexContent(config, indexHtmlDocument);
443+
addGeneratedIndexContent(indexHtmlDocument, getStatsJson(config));
440444
}
441445
modifyIndexHtmlForVite(indexHtmlDocument);
442446
return indexHtmlDocument;
443447
}
444448

449+
protected static void addGeneratedIndexContent(Document targetDocument,
450+
JsonObject statsJson) {
451+
452+
JsonArray indexHtmlGeneratedRows = statsJson
453+
.getArray("indexHtmlGenerated");
454+
List<String> toAdd = new ArrayList<>();
455+
456+
toAdd.addAll(JsonUtils.stream(indexHtmlGeneratedRows)
457+
.map(value -> value.asString()).toList());
458+
459+
for (String row : toAdd) {
460+
targetDocument.head().append(row);
461+
}
462+
}
463+
445464
private static void modifyIndexHtmlForVite(Document indexHtmlDocument) {
446465
// Workaround for https://github.com/vitejs/vite/issues/5142
447466
indexHtmlDocument.head().prepend(

‎flow-server/src/main/java/com/vaadin/flow/server/communication/WebComponentBootstrapHandler.java‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.ArrayList;
2929
import java.util.Collections;
3030
import java.util.List;
31+
import java.util.Objects;
3132
import java.util.Optional;
3233
import java.util.function.Function;
3334
import java.util.regex.Pattern;
@@ -59,6 +60,7 @@
5960
import com.vaadin.flow.server.VaadinServletRequest;
6061
import com.vaadin.flow.server.VaadinSession;
6162
import com.vaadin.flow.server.frontend.BundleUtils;
63+
import com.vaadin.flow.server.frontend.DevBundleUtils;
6264
import com.vaadin.flow.server.frontend.FrontendUtils;
6365
import com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry;
6466
import com.vaadin.flow.shared.ApplicationConstants;
@@ -135,7 +137,8 @@ public Document getBootstrapPage(BootstrapContext context) {
135137
// directly from the frontend folder and the JS
136138
// entrypoint(s) need
137139
// to be added
138-
addGeneratedIndexContent(deploymentConfiguration, document);
140+
addGeneratedIndexContent(document,
141+
getStatsJson(deploymentConfiguration));
139142
}
140143

141144
// Specify the application ID for scripts of the
@@ -178,6 +181,28 @@ protected List<String> getChunkKeys(JsonObject chunks) {
178181
}
179182
}
180183

184+
protected static void addGeneratedIndexContent(Document targetDocument,
185+
JsonObject statsJson) {
186+
List<String> toAdd = new ArrayList<>();
187+
188+
Optional<String> webComponentScript = JsonUtils
189+
.stream(statsJson.getArray("entryScripts"))
190+
.map(value -> value.asString())
191+
.filter(script -> script.contains("webcomponenthtml"))
192+
.findFirst();
193+
194+
if (webComponentScript.isPresent()) {
195+
Element elm = new Element(SCRIPT_TAG);
196+
elm.attr("type", "module");
197+
elm.attr("src", webComponentScript.get());
198+
toAdd.add(elm.outerHtml());
199+
}
200+
201+
for (String row : toAdd) {
202+
targetDocument.head().append(row);
203+
}
204+
}
205+
181206
/**
182207
* Creates a new bootstrap handler with default page builder.
183208
*/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2000-2023 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.vaadin.flow.webcomponent;
18+
19+
import com.vaadin.flow.component.html.Div;
20+
import com.vaadin.flow.component.html.Span;
21+
import com.vaadin.flow.router.Route;
22+
23+
@Route("com.vaadin.flow.webcomponent.MyView")
24+
public class MyView extends Div {
25+
26+
static final String APP_TEXT_ID = "app_text";
27+
28+
public MyView() {
29+
final Span textSpan = new Span("This is the application view");
30+
textSpan.setId(APP_TEXT_ID);
31+
add(textSpan);
32+
}
33+
34+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2000-2023 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.webcomponent;
17+
18+
import org.junit.Assert;
19+
import org.junit.Test;
20+
21+
import com.vaadin.flow.component.html.testbench.SpanElement;
22+
import com.vaadin.flow.testutil.ChromeBrowserTest;
23+
24+
import static com.vaadin.flow.webcomponent.MyView.APP_TEXT_ID;
25+
26+
public class MyViewIT extends ChromeBrowserTest {
27+
28+
@Override
29+
protected String getTestPath() {
30+
return "/com.vaadin.flow.webcomponent.MyView";
31+
}
32+
33+
@Test
34+
public void applicationOpens_withEmbeddedComponents() {
35+
open();
36+
37+
$(SpanElement.class).waitForFirst();
38+
SpanElement spanWithText = $(SpanElement.class).id(APP_TEXT_ID);
39+
40+
Assert.assertTrue(
41+
spanWithText.getText().equals("This is the application view"));
42+
43+
checkLogsForErrors();
44+
}
45+
46+
}

0 commit comments

Comments
 (0)