如何动态地为GWT排列提供清单 [英] How to dynamically serve manifest for GWT permutations

查看:86
本文介绍了如何动态地为GWT排列提供清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下问题。您想为您的GWT项目提供脱机清单/应用程序缓存文件。在这种情况下,有两个问题:


  1. GWT会生成js文件的不同排列(取决于浏览器版本)。在加载应用程序时,一些GWT JavaScript代码使用您的用户代理属性来包含相应的代码。您将需要为这些排列中的每一个生成不同的清单文件,因为您不想缓存不会使用的文件(并且这些文件每个排列可能大约为0.5MB)。此问题由 MGWT清单链接器涵盖,在编译过程中生成不同的清单文件

  2. 在浏览器中加载webapp时提供适当的清单文件

这个问题与问题2有关。我们如何以强大的方式动态地为这个清单提供服务? MGWT使用 servlet ,它根据请求中的用户代理提供清单。
您需要将您的用户代理字符串(例如 Mozilla / 4.0(兼容; MSIE 6.0b; Windows NT 5.1))映射到'user-agent id '(例如 ie6 )。使用由MGWT链接器创建的映射文件,您可以找到要提供给客户端的清单文件。一个主要的缺点是你需要做一些简单的字符串操作来将完整的用户代理字符串映射到这个用户代理标识符,并且有一些天真的字符串匹配。您将无法重新使用客户端GWT代码进行此类映射。 (这是所有讨论在这个话题)。因此,每当GWT收到更改排列数的更新和/或受支持的浏览器时,您也需要更改您的servlet代码。换句话说,这不是一个可靠的解决方案。问题是:我们可以以不同的方式为这些GWT排列提供清单服务:通过动态提供这些文件客户端?

解决方案

是的,但是以一种四舍五入的方式。无法通过javascript动态更改html'manifest'属性。解决方法是通过javascript生成一个iframe,它引用一个空html页面,并在其中包含某个清单属性(参见此主题)。为了在GWT中工作,你需要:


  1. 更改MGWT链接器,所以对于每个排列,将参考这个排列清单创建一个空的html页面。例如:

      toReturn.add(emitString(
    logger,
    < html manifest = \\ \\+ permutation +.manifest \>< head>< / head>< body>< / body>< / html>,
    permutation +.manifest.html )
    );


  2. 在您的GWT客户端代码中,在模块加载中:检索您的排列强名称,使用此插入此排列的iframe。这看起来像这样:


在您的入门级别:

  public void onModuleLoad(){
appendManifestIframe(GWT.getPermutationStrongName()+.manifest.html);
}

public static native appendManifestIframe(String manifestIframe)/ * - {
var ifrm = document.createElement(iframe);
ifrm.setAttribute(src,manifestIframe);
ifrm.style.width = 0+px;
ifrm.style.height = 0+px;
$ doc.body.appendChild(ifrm);
} - * /;

请注意, GWT.getPermutationStrongName 返回'HostedMode '当你处于开发模式时。也就是说,你将无法在开发模式下使用这种方法(或者你应该确保你为 HostedMode 编写单独清单/ iframe)


Consider the following problem. You'd like to serve an offline manifest/appcache file for your GWT project. In such a case, there are two issues:

  1. GWT generates different permutations of js files (depending on browser version). When loading the application, some GWT javascript code uses your user-agent properties to include the appropriate one. You'll want to generate a different manifest file for each of these permutations, as you don't want to cache files you won't use (and these files can be around 0.5MB per permutation). This issue is covered by the MGWT Manifest Linker, which generates different manifest files during the compilation process
  2. Serving the appropriate manifest file when loading the webapp in your browser

This question relates to issue 2. How can we serve this manifest dynamically, in a robust way? MGWT uses a servlet which serves the manifest, depending on the user agent from the request. You would need to map your user agent string (e.g. Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)) to a 'user-agent id' (e.g. ie6). Using a mapping file created by the MGWT linker, you can find the manifest file to serve to the client. A major downside is that you'll need to do some simple string operations to map the complete user agent string to this user agent id with some naive string matching. You won't be able to re-use the client-side GWT code for such a mapping. (this is all discussed in this topic). As a result, whenever GWT receives an update which changes the number of permutations, and/or the supported browsers, you'll need to change your servlet code as well. In other words: this is not a robust solution.

The question is: can we serve the manifest in a different way for these GWT permutations, by serving these file dynamically on the client side?

解决方案

Yes, however in a roundabit way. It is not possible to change the html 'manifest' attribute dynamically via javascript. A workaround is generating an iframe via javascript, which references an empty html page with a certain manifest attribute in it (see this topic). For this to work in GWT, you'll need to:

  1. Change the MGWT linker, so for each permutation, you'll create an empty html page with a reference to this permutations manifest. Something like:

    toReturn.add(emitString(
            logger, 
            "<html manifest=\"" + permutation + ".manifest\"><head></head><body></body></html>", 
            permutation + ".manifest.html")
    );
    

  2. In your GWT client code, on module load: retrieve your permutation strong-name, and use this to insert the iframe for this permutation. This would look like this:

In your entry class:

public void onModuleLoad() {
    appendManifestIframe(GWT.getPermutationStrongName() + ".manifest.html");
}

public static native void appendManifestIframe(String manifestIframe) /*-{
        var ifrm = document.createElement("iframe"); 
        ifrm.setAttribute("src", manifestIframe); 
        ifrm.style.width = 0+"px"; 
        ifrm.style.height = 0+"px"; 
        $doc.body.appendChild(ifrm); 
    }-*/;

Note that GWT.getPermutationStrongName returns 'HostedMode' when you are in dev mode. I.e., you won't be able to use this approach in dev mode (or you should make sure you write a separete manifest/iframe for HostedMode as well)

这篇关于如何动态地为GWT排列提供清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆