Skip to content

Commit f570561

Browse files
vaadin-botArtur-
andauthored
fix!: Always load static files from context root (#13795) (#13840)
Fixes #13769 Co-authored-by: Artur <artur@vaadin.com>
1 parent e10a7b7 commit f570561

14 files changed

Lines changed: 102 additions & 26 deletions

File tree

‎flow-client/src/main/java/com/vaadin/client/communication/AtmospherePushConnection.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ private void runWhenAtmosphereLoaded(final Command command) {
727727
Console.log("Loading " + pushJs);
728728
ResourceLoader loader = registry.getResourceLoader();
729729
String pushScriptUrl = registry.getApplicationConfiguration()
730-
.getServiceUrl() + pushJs;
730+
.getContextRootUrl() + pushJs;
731731
ResourceLoadListener loadListener = new ResourceLoadListener() {
732732
@Override
733733
public void onLoad(ResourceLoadEvent event) {

‎flow-server/src/main/java/com/vaadin/flow/internal/UrlUtil.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import java.net.URLEncoder;
2121
import java.nio.charset.StandardCharsets;
2222

23+
import javax.servlet.http.HttpServletRequest;
24+
25+
import com.vaadin.flow.server.VaadinRequest;
26+
2327
/**
2428
* Internal utility class for URL handling.
2529
* <p>
@@ -102,4 +106,42 @@ public static String encodeURIComponent(String path) {
102106
}
103107
}
104108

109+
/**
110+
* Gets the path info for a /VAADIN/something request.
111+
*
112+
* @param request
113+
* the servlet request
114+
* @return the path info starting with /VAADIN/ or <code>null</code> if no
115+
* path information is available
116+
*/
117+
public static String getStaticVaadinPathInfo(VaadinRequest request) {
118+
if (request instanceof HttpServletRequest) {
119+
return getStaticVaadinPathInfo(((HttpServletRequest) request));
120+
}
121+
122+
return request.getPathInfo();
123+
}
124+
125+
/**
126+
* Gets the path info for a /VAADIN/something request.
127+
*
128+
* @param request
129+
* the servlet request
130+
* @return the path info starting with /VAADIN/ or <code>null</code> if no
131+
* path information is available
132+
*/
133+
public static String getStaticVaadinPathInfo(HttpServletRequest request) {
134+
String pathInfo = request.getPathInfo();
135+
if (pathInfo == null) {
136+
return null;
137+
}
138+
139+
if ("/VAADIN".equals(request.getServletPath())) {
140+
// If the servlet is deployed with a separate /VAADIN/* mapping, we
141+
// need to add the missing /VAADIN/
142+
String prefix = pathInfo.startsWith("/") ? "/VAADIN" : "/VAADIN/";
143+
pathInfo = prefix + pathInfo;
144+
}
145+
return pathInfo;
146+
}
105147
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,8 +1009,10 @@ private void appendNpmBundle(Element head, VaadinService service,
10091009
.compile("src=\\\"VAADIN\\/build\\/(.*\\.js)\\\"")
10101010
.matcher(index);
10111011
while (scriptMatcher.find()) {
1012-
Element script = createJavaScriptModuleElement(
1013-
"VAADIN/build/" + scriptMatcher.group(1), false);
1012+
Element script = createJavaScriptModuleElement(context
1013+
.getUriResolver().resolveVaadinUri("context://"
1014+
+ "VAADIN/build/" + scriptMatcher.group(1)),
1015+
false);
10141016
head.appendChild(script.attr("async", true)
10151017
// Fixes basic auth in Safari #6560
10161018
.attr("crossorigin", true));
@@ -1021,8 +1023,9 @@ private void appendNpmBundle(Element head, VaadinService service,
10211023
.compile("href=\\\"VAADIN\\/build\\/(.*\\.css)\\\"")
10221024
.matcher(index);
10231025
while (cssMatcher.find()) {
1024-
Element link = createStylesheetElement(
1025-
"VAADIN/build/" + cssMatcher.group(1));
1026+
Element link = createStylesheetElement(context
1027+
.getUriResolver().resolveVaadinUri("context://"
1028+
+ "VAADIN/build/" + cssMatcher.group(1)));
10261029
head.appendChild(link);
10271030
}
10281031
return;
@@ -1670,7 +1673,8 @@ protected static String getPushScript(BootstrapContext context) {
16701673
// Parameter appended to JS to bypass caches after version upgrade.
16711674
String versionQueryParam = "?v=" + Version.getFullVersion();
16721675
// Load client-side dependencies for push support
1673-
String pushJSPath = BootstrapHandlerHelper.getServiceUrl(request) + "/";
1676+
String pushJSPath = context.getService()
1677+
.getContextRootRelativePath(request);
16741678

16751679
if (request.getService().getDeploymentConfiguration()
16761680
.isProductionMode()) {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public boolean synchronizedHandleRequest(VaadinSession session,
8383
: getIndexHtmlDocument(service);
8484

8585
prependBaseHref(request, indexDocument);
86+
String contextRootRelativePath = request.getService()
87+
.getContextRootRelativePath(request);
88+
rewriteBundleImportToContextRoot(indexDocument,
89+
contextRootRelativePath);
8690

8791
JsonObject initialJson = Json.createObject();
8892

@@ -280,6 +284,16 @@ private static void prependBaseHref(VaadinRequest request,
280284
}
281285
}
282286

287+
private static void rewriteBundleImportToContextRoot(Document indexDocument,
288+
String contextRootRelativePath) {
289+
Elements bundleScripts = indexDocument.head()
290+
.getElementsByAttributeValueStarting("src", "VAADIN/");
291+
for (Element bundleScript : bundleScripts) {
292+
bundleScript.attr("src", bundleScript.attr("src").replaceFirst(
293+
"VAADIN", contextRootRelativePath + "VAADIN"));
294+
}
295+
}
296+
283297
private static Document getCachedIndexHtmlDocument(VaadinService service) {
284298
return service.getContext().getAttribute(IndexHtmlHolder.class,
285299
() -> new IndexHtmlHolder(service)).getDocument();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ protected StreamRequestHandler(StreamReceiverHandler receiverHandler) {
7070
@Override
7171
public boolean handleRequest(VaadinSession session, VaadinRequest request,
7272
VaadinResponse response) throws IOException {
73-
74-
String pathInfo = request.getPathInfo();
73+
String pathInfo = UrlUtil.getStaticVaadinPathInfo(request);
7574
if (pathInfo == null) {
7675
return false;
7776
}

‎flow-server/src/test/java/com/vaadin/flow/server/BootstrapHandlerTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,11 +1773,11 @@ public void runViteFeatureProdMode_bundleAddedToHead() throws IOException {
17731773
Assert.assertTrue(
17741774
"Bundle should be gotten from index and added to bootstrap page",
17751775
bootstrapPage.head().toString()
1776-
.contains("src=\"VAADIN/build/main.d253dd35.js\""));
1776+
.contains("src=\"./VAADIN/build/main.d253dd35.js\""));
17771777
Assert.assertTrue(
17781778
"Bundled css should be gotten from index and added to bootstrap page",
17791779
bootstrapPage.head().toString()
1780-
.contains("href=\"VAADIN/build/main.688a5538.css\""));
1780+
.contains("href=\"./VAADIN/build/main.688a5538.css\""));
17811781
}
17821782

17831783
private void enableViteFeature(boolean productionMode) throws IOException {

‎flow-server/src/test/java/com/vaadin/flow/server/communication/JavaScriptBootstrapHandlerTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public void should_respondPushScript_when_nonRootServletPath()
220220

221221
// Using regex, because version depends on the build
222222
Assert.assertTrue(json.getString("pushScript").matches(
223-
"^\\./VAADIN/static/push/vaadinPush\\.js\\?v=[\\w\\.\\-]+$"));
223+
"^\\./\\.\\./VAADIN/static/push/vaadinPush\\.js\\?v=[\\w\\.\\-]+$"));
224224
}
225225

226226
@Test

‎flow-tests/test-common/src/main/java/com/vaadin/flow/uitest/servlet/ViewTestServlet.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
import com.vaadin.flow.server.VaadinServlet;
2424

25-
@WebServlet(asyncSupported = true, urlPatterns = {
26-
"/view/*" }, initParams = @WebInitParam(name = "productionMode", value = "false"))
25+
@WebServlet(asyncSupported = true, urlPatterns = { "/view/*",
26+
"/VAADIN/*" }, initParams = @WebInitParam(name = "productionMode", value = "false"))
2727
public class ViewTestServlet extends VaadinServlet {
2828

2929
private static ViewClassLocator viewLocator;

‎flow-tests/test-frontend/vite-production/src/test/java/com/vaadin/viteapp/CompressionIT.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void resourcesAvailableAsBrotli() throws Exception {
4040
private String getJsBundleName() throws Exception {
4141
String indexHtml = IOUtils.toString(
4242
new URL(getRootURL() + "/index.html"), StandardCharsets.UTF_8);
43-
Pattern p = Pattern.compile(".* src=\"VAADIN/build/([^\"]*).*",
43+
Pattern p = Pattern.compile(".* src=\"./VAADIN/build/([^\"]*).*",
4444
Pattern.DOTALL);
4545

4646
Matcher m = p.matcher(indexHtml);

‎flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/VaadinPushScriptIT.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void pushScriptURL_urlMapping_fromJavascriptBootstrapHandler() {
3434
getDriver().get(
3535
getRootURL() + "/view/" + PushSettingsView.class.getName());
3636
waitForDevServer();
37-
assertThatPushScriptUrlIsRelativeToUrlMapping(
37+
assertThatPushScriptUrlIsRelativeToContextRoot(
3838
findElement(By.tagName("body")));
3939
}
4040

@@ -46,11 +46,11 @@ public void pushScriptURL_urlMapping_fromClientSide() {
4646
getRootURL() + "/view/" + ActivatePushView.class.getName());
4747
waitForDevServer();
4848

49-
assertThatPushScriptUrlIsRelativeToUrlMapping(
49+
assertThatPushScriptUrlIsRelativeToContextRoot(
5050
findElement(By.tagName("head")));
5151
}
5252

53-
private void assertThatPushScriptUrlIsRelativeToUrlMapping(
53+
private void assertThatPushScriptUrlIsRelativeToContextRoot(
5454
WebElement scriptContainer) {
5555
String pushScriptUrl = scriptContainer
5656
.findElements(By.tagName("script")).stream()
@@ -61,9 +61,10 @@ private void assertThatPushScriptUrlIsRelativeToUrlMapping(
6161

6262
Assert.assertNotNull(ApplicationConstants.VAADIN_PUSH_DEBUG_JS
6363
+ " script not loaded by page", pushScriptUrl);
64-
Assert.assertTrue("Push script not relative to Vaadin servlet mapping",
64+
Assert.assertTrue(
65+
"Push script not relative to context root: " + pushScriptUrl,
6566
pushScriptUrl.contains(
66-
"/view/" + ApplicationConstants.VAADIN_PUSH_DEBUG_JS));
67+
":8888/" + ApplicationConstants.VAADIN_PUSH_DEBUG_JS));
6768
}
6869

6970
}

0 commit comments

Comments
 (0)