18
18
import java .io .IOException ;
19
19
import java .io .InputStream ;
20
20
import java .io .UncheckedIOException ;
21
+ import java .util .Optional ;
21
22
import java .util .regex .Pattern ;
22
23
23
24
import org .jsoup .nodes .Document ;
24
25
import org .jsoup .nodes .Element ;
25
26
import org .slf4j .Logger ;
26
27
import org .slf4j .LoggerFactory ;
28
+ import org .slf4j .event .Level ;
27
29
28
30
import com .vaadin .flow .server .ServiceInitEvent ;
31
+ import com .vaadin .flow .server .VaadinRequest ;
29
32
import com .vaadin .flow .server .VaadinServiceInitListener ;
30
33
import com .vaadin .flow .server .communication .IndexHtmlRequestListener ;
31
34
@@ -36,7 +39,8 @@ public class XHRReloadVaadinServiceInitListener implements VaadinServiceInitList
36
39
{
37
40
private static final Logger LOG = LoggerFactory .getLogger (XHRReloadVaadinServiceInitListener .class );
38
41
39
- protected final String scriptContents ;
42
+ protected final Element scriptElement ;
43
+ protected boolean scriptAttachErrorAlreadyLogged ;
40
44
41
45
public XHRReloadVaadinServiceInitListener (final XHRReloadConfig config )
42
46
{
@@ -48,11 +52,21 @@ public XHRReloadVaadinServiceInitListener(final XHRReloadConfig config)
48
52
}
49
53
50
54
// Remove comments
51
- this . scriptContents = Pattern .compile (
55
+ final String scriptContents = Pattern .compile (
52
56
"\\ /\\ *[\\ s\\ S]*?\\ *\\ /|(?<=[^:])\\ /\\ /.*|^\\ /\\ /.*" )
53
57
.matcher (new String (is .readAllBytes ()))
54
58
.replaceAll ("" )
55
59
.trim ();
60
+
61
+ if (scriptContents .isBlank ())
62
+ {
63
+ LOG .error ("Script loaded from {} is empty" , config .getResourceLocation ());
64
+ }
65
+
66
+ this .scriptElement = new Element ("script" )
67
+ .attr ("type" , "text/javascript" )
68
+ .html (scriptContents );
69
+ LOG .trace ("Built scriptElement: {}" , this .scriptElement );
56
70
}
57
71
catch (final IOException e )
58
72
{
@@ -65,11 +79,32 @@ public void serviceInit(final ServiceInitEvent event)
65
79
{
66
80
event .addIndexHtmlRequestListener ((IndexHtmlRequestListener )resp -> {
67
81
final Document document = resp .getDocument ();
68
-
69
82
final Element body = document .body ();
70
- body .prependElement ("script" )
71
- .attr ("type" , "text/javascript" )
72
- .html (this .scriptContents );
83
+
84
+ try
85
+ {
86
+ body .prependChild (this .scriptElement .clone ());
87
+ }
88
+ catch (final Exception ex )
89
+ {
90
+ LOG .atLevel (this .scriptAttachErrorAlreadyLogged
91
+ ? Level .DEBUG
92
+ : Level .WARN )
93
+ .setMessage (
94
+ "Failed to attach XHRReloadScript. {}"
95
+ + "Details: path={} body={}" )
96
+ .addArgument (!this .scriptAttachErrorAlreadyLogged
97
+ ? "Subsequent errors will be logged at DEBUG. "
98
+ : "" )
99
+ .addArgument (() ->
100
+ Optional .ofNullable (resp .getVaadinRequest ())
101
+ .map (VaadinRequest ::getPathInfo )
102
+ .orElse ("?" ))
103
+ .addArgument (body )
104
+ .addArgument (ex )
105
+ .log ();
106
+ this .scriptAttachErrorAlreadyLogged = true ;
107
+ }
73
108
});
74
109
75
110
LOG .debug ("Applied serviceInit" );
0 commit comments