简体   繁体   中英

GWT ScriptInjector VS adding script tag to index.html page

Why use ScriptInjector class.

What is the difference between using ScriptInjector to inject a js file and adding the link with a <script> tag directly to the index.html file. Is there any advantage of using the ScriptInjector over adding the script tag in the index.html file.

One thing that comes to my mind is that the initial load may be a lot more if you add a ton of scripts in the index.html file using <script> which are not going to be used until later. ScriptInjector will cut down on the initial load by loading the script asynchronously only when required.

The main advantage of using ScriptInjector is that you can leverage the Java language to inject JS files (or JS code) in your page. In the context of a GWT application, this is cleaner than using JSNI.

You're right, adding the <script> tags up front in the HTML file might lead to performance issues.

Advantages of Using ScriptInjector are

  • ScriptInjector Dynamically create script tag and attach it to DOM meaning based on the condition you can inject script(js).
  • If we include lot of Script tag on host page, which will take lot of time to load the script tag(also it will block untill its get executed).

You already described one of the key advantages.

Another advantage is that some scripts may never be used. For example, you may have a script that integrates Google Wallet payments or Facebook login, but not every user needs these features every time. So there is no reason to load these scripts until actually needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM