GWT cache.js 太大 -- 应用需要时间才能显示

GWT cache.js too big -- take time for the app to show up

我们在 GWT 之上使用 JBoss Errai 框架来构建 Web 应用程序。我们遇到的问题是,在使用优化编译时,应用程序的编译版本大小已经约为 10 兆字节。

有没有办法让 GWT/Errai 应用程序在加载 while cache.js 文件之前拆分或以某种方式显示初始页面?

您可以使用Code Splitting机制。

To split your code, simply insert calls to the method GWT.runAsync at the places where you want the program to be able to pause for downloading more code. These locations are called split points.

例如:

public class Hello implements EntryPoint {
  public void onModuleLoad() {
    Button b = new Button("Click me", new ClickHandler() {
      public void onClick(ClickEvent event) {
        GWT.runAsync(new RunAsyncCallback() {
          public void onFailure(Throwable caught) {
            Window.alert("Code download failed");
          }

          public void onSuccess() {
            Window.alert("Hello, AJAX");
          }
        });
      }
    });

    RootPanel.get().add(b);
  }
}

... the code initially downloaded does not include the string "Hello, AJAX" nor any code necessary to implement Window.alert. Once the button is clicked, the call to GWT.runAsync will be reached, and that code will start downloading. Assuming it downloads successfully, the onSuccess method will be called; since the necessary code has downloaded, that call will succeed. If there is a failure to download the code, then onFailure will be invoked.

您将在文档中找到详细信息:http://www.gwtproject.org/doc/latest/DevGuideCodeSplitting.html

如果您不想使用拆分点来实际减小初始负载大小,您可以在开始时显示一些加载屏幕,这样用户至少知道应用程序正在启动。

这可以通过以下方式轻松完成:

  • 添加 div 显示加载动画 (example) 到您的主页
  • 应该在 <script type="text/javascript" src="app.nochache.js"></script> 标签之前添加 div
  • div 一个 ID,例如:loading-icon
  • onModuleLoad方法中,加载*.cache.js后,将div设置为display:none;移除and/or DOM

我遇到了同样的问题。我的应用程序有 8Mo 大,我设法通过更改 GWT 编译选项之一将其减少到 800ko:Output style 从 Detailed 到 Obfuscated.