Actually, this suggests that another fix will work better, and will improve overall performance. Loading the exact same module twice onto the same page means loading the same nocache.js, which will try to trigger the same iframe to be loaded with the actual module's contents, which will then execute the exact same code – Why not just do this once, and write the module to expect to be loaded twice? This way, the module will expect to run multiple times, and will expect to have multiple elements to write content into, as well as matching resourceUrls.
Also: the default IFrameLinker that GWT uses is set up in such a way as to not load the iframe until everything else on the page has loaded. If you are using a different linker, there might be other issues here, but I believe that *.nocache.js is the IFrameLinker's way of doing things.
Something like this: in your page js, build up a map from divid to resource url
...
<div id='portletContainerId1'></div>
...
<div id='portletContainerId2'></div>
Then, in your java code, loop through all keys in $wnd.appResources, init'ing the app for each div and matching url. If there is additional info, the map can be string:object, with the object containing your other details.
Depending on how your velocity stuff is wired up, this can also be expressed:
var appResources = {};
...
<div id='portletContainerId1'></div>
...
<div id='portletContainerId2'></div>
Your java will look something like this then - instead of onModuleLoad being responsible for bringing up the portlet, let some other method or class deal with that, and let onModuleLoad figure out what has to be done to start the page:
public void onModuleLoad {
interface PortletHandler {
void startPortlet(String portletId, String resourceUrl);
}
class DemoPortletHandler implements PortletHandler {
public void startPortlet(String id, String url) {
Window.alert("Portlet in " + id + " started with url " + url);
}
}
forEachPortlet(new DemoPortletHandler());
}
private native void forEachPortlet(PortletHandler handler) /*-{
for (var id in $wnd.appResources) {
handler.@package.client.ThisModule.PortletHandler(Ljava/lang/String;Ljava/lang/String;)(id, $wnd.appResources[id]);
}
}-*/;
Please note that I am typing this all out freehand, so I might've messed up the jsni signatures, but this should provide a usable link between your page and your code.
Hope this helps,
Colin