How to dynamically serve manifest for GWT permutations

放肆的年华 提交于 2019-12-06 05:59:19
Laurens Rietveld

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)

I'm not positive about the approach of computing the manifest file to use, in client side. Let me explain:

The manifest attribute tells to the browser that this page and all assets included in the manifest and used by this page must be cached and gotten from the cache.

If you don't set the manifest attribute in the index.html, the page would not be cached and it would not use any resource from cache.

Using the iframe approach, you would load an iframe.html with the manifest attribute set, and this manifest would include the index.html and all its assets.

I have not tested this, but I think that although the browser would cache and get the index.html from the offline storage, it would not get any asset included in it since index.html does not have the manifest attribute set, so your module.nocache.js would never be loaded if the device is offline.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!