Search code examples
gwtgwt-rpcgwt-2.7gwt-2.8

GWT RPC fails when launching app from different urls


I'm using urls to navigate to different screens inside my GWT app. For example:

http://127.0.0.1/home
http://127.0.0.1/info/contact-us
http://127.0.0.1/app/index.html

I have a servlet that serves the html containing the required script element for GWT (my GWT module name is "app"):

<script type="text/javascript" language="javascript" src="/app/app.nocache.html">
</script>

This is working great with GWT 2.6.1. In the browser dev tools it's possible to see that RPC calls are made to my RemoteService at http://127.0.0.1/app/rpc

The problem is when I upgraded to GWT 2.8, my app's RPC call endpoint is now different and wrong, depending on the URL used. For example:

http://127.0.0.1/home            -> http://127.0.0.1/rpc
http://127.0.0.1/info/contact-us -> http://127.0.0.1/info/rpc
http://127.0.0.1/app/index.html  -> http://127.0.0.1/app/rpc

For the above URLs the module is always correctly loaded and executed, however RPCs fail in the first two cases. only the last URL allows my app to make RPC calls.

The RPC endpoint can be set by casting the client-side service proxy to ServiceDefTarget and using setServiceEntryPoint(). As follows:

ourInstance = (MyRemoteServiceAsync)GWT.create(MyRemoteService.class);
ServiceDefTarget serviceDefTarget = (ServiceDefTarget) ourInstance;
serviceDefTarget.setServiceEntryPoint("/app/rpc");

However, the request payload still contains a reference to the incorrect module base. The http headers sent on the RPC request have incorrect values also:

X-GWT-Module-Base:http://127.0.0.1/foo/bar/

Is there a way to force the client's RPC mechanism to use the correct RPC URL /app/rpc? Or perhaps a way to set the module-base correctly?

UPDATE 1

Seeing the same behaviour in GWT 2.7.

Also, when deployed in a WAR the <module-hash>.cache.js file doesn't load because it is also requested relative to the url. This is very bad because it means that the module code won't be cached, since this url is different every time. The fix needs to be made in the selector <module>.nocache.js. Is anyone actually using GWT with url linking in the real world?


Solution

  • By specifying a <meta> element in the <head> element of the html document, the bootstrap nocache.js selector javascript will choose the correct module baseUrl. The baseUrl must be a fully-specified absolute url, and end with a /.

    For my example, the exact element was:

    <head>
       ...
       <meta name="gwt:property" content="baseUrl=http://127.0.0.1/app/" />
       ...
    </head>