|
29 | 29 | import java.util.function.Function; |
30 | 30 | import java.util.regex.Pattern; |
31 | 31 |
|
| 32 | +import org.jsoup.Jsoup; |
32 | 33 | import org.jsoup.nodes.Attribute; |
| 34 | +import org.jsoup.nodes.DataNode; |
33 | 35 | import org.jsoup.nodes.Document; |
34 | 36 | import org.jsoup.nodes.Element; |
| 37 | +import org.jsoup.parser.Tag; |
35 | 38 |
|
| 39 | +import com.vaadin.experimental.FeatureFlags; |
36 | 40 | import com.vaadin.flow.component.PushConfiguration; |
37 | 41 | import com.vaadin.flow.component.UI; |
38 | 42 | import com.vaadin.flow.component.webcomponent.WebComponentUI; |
39 | 43 | import com.vaadin.flow.dom.ElementUtil; |
40 | 44 | import com.vaadin.flow.internal.JsonUtils; |
| 45 | +import com.vaadin.flow.server.BootstrapException; |
41 | 46 | import com.vaadin.flow.server.BootstrapHandler; |
42 | 47 | import com.vaadin.flow.server.Constants; |
43 | 48 | import com.vaadin.flow.server.HandlerHelper; |
44 | 49 | import com.vaadin.flow.server.PwaRegistry; |
45 | 50 | import com.vaadin.flow.server.VaadinRequest; |
46 | 51 | import com.vaadin.flow.server.VaadinResponse; |
| 52 | +import com.vaadin.flow.server.VaadinService; |
47 | 53 | import com.vaadin.flow.server.VaadinServletRequest; |
48 | 54 | import com.vaadin.flow.server.VaadinSession; |
| 55 | +import com.vaadin.flow.server.frontend.FrontendUtils; |
49 | 56 | import com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry; |
50 | 57 | import com.vaadin.flow.shared.ApplicationConstants; |
51 | 58 |
|
@@ -97,6 +104,48 @@ protected Optional<PwaRegistry> getPwaRegistry() { |
97 | 104 |
|
98 | 105 | private static class WebComponentBootstrapPageBuilder |
99 | 106 | extends BootstrapPageBuilder { |
| 107 | + @Override |
| 108 | + public Document getBootstrapPage(BootstrapContext context) { |
| 109 | + VaadinService service = context.getSession().getService(); |
| 110 | + |
| 111 | + if (FeatureFlags.get(service.getContext()) |
| 112 | + .isEnabled(FeatureFlags.VITE)) { |
| 113 | + try { |
| 114 | + Document document = Jsoup.parse( |
| 115 | + FrontendUtils.getWebComponentHtmlContent(service)); |
| 116 | + Element head = document.head(); |
| 117 | + |
| 118 | + // Specify the application ID for scripts of the |
| 119 | + // web-component.html |
| 120 | + head.select("script[src]").attr("data-app-id", |
| 121 | + context.getUI().getInternals().getAppId()); |
| 122 | + |
| 123 | + // Add `crossorigin` to fix basic auth in Safari #6560 |
| 124 | + head.select("script[src], link[href]").attr("crossorigin", |
| 125 | + "true"); |
| 126 | + |
| 127 | + JsonObject initialUIDL = getInitialUidl(context.getUI()); |
| 128 | + |
| 129 | + head.prependChild(createInlineJavaScriptElement( |
| 130 | + "window.JSCompiler_renameProperty = function(a) { return a; }")); |
| 131 | + |
| 132 | + head.prependChild(getBootstrapScript(initialUIDL, context)); |
| 133 | + |
| 134 | + if (context.getPushMode().isEnabled()) { |
| 135 | + head.prependChild(createJavaScriptModuleElement( |
| 136 | + getPushScript(context), true)); |
| 137 | + } |
| 138 | + |
| 139 | + return document; |
| 140 | + } catch (IOException e) { |
| 141 | + throw new BootstrapException( |
| 142 | + "Unable to read the web-component.html file.", e); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return super.getBootstrapPage(context); |
| 147 | + } |
| 148 | + |
100 | 149 | @Override |
101 | 150 | protected List<String> getChunkKeys(JsonObject chunks) { |
102 | 151 | if (chunks.hasKey(EXPORT_CHUNK)) { |
@@ -395,8 +444,9 @@ private void transferAttribute(Writer writer, String elementRef, |
395 | 444 | if (attribute.getValue() == null) { |
396 | 445 | writer.append("''"); |
397 | 446 | } else { |
| 447 | + String name = attribute.getKey(); |
398 | 448 | String path = attribute.getValue(); |
399 | | - if ("src".equals(attribute.getKey())) { |
| 449 | + if (name.matches("^(src|href)$")) { |
400 | 450 | path = modifyPath(basePath, path); |
401 | 451 | } |
402 | 452 | writer.append("\"").append(path).append("\""); |
|
0 commit comments