GWT: deferred loading of external JS resources

后端 未结 2 1997
夕颜
夕颜 2021-02-03 10:48

I have a widget depending on some external JS files, and I\'d like to lazy load all these external resources. I\'ve already used code splitting to lazy load the GWT code that co

相关标签:
2条回答
  • 2021-02-03 11:20

    I think you'll want to take a look at the com.google.gwt.core.client.ScriptInjector class. From the javadocs:

    Dynamically create a script tag and attach it to the DOM.

    ...

    Usage with script loaded as URL:

       ScriptInjector.fromUrl("http://example.com/foo.js").setCallback(
         new Callback<Void, Exception>() {
            public void onFailure(Exception reason) {
              Window.alert("Script load failed.");
            }
            public void onSuccess(Void result) {
              Window.alert("Script load success.");
            }
         }).inject();
    

    This code can of course be invoked from within your split points, or indeed anywhere in your code.

    0 讨论(0)
  • 2021-02-03 11:33

    ScriptInjector is quite portable. It doesn't have any external dependencies, so you should be able to backport it into your 2.3 application without much problem.

    0 讨论(0)
提交回复
热议问题