How does GWT provide the correct Javascript code to every browser e.g. to carry out i18n and browser compatibility?

余生颓废 提交于 2020-01-17 11:34:27

问题


I am very new to GWT and I started to take a look at it after I jumped into the Vaadin world.

Reading this from the wiki: https://code.google.com/p/google-web-toolkit-doc-1-5/wiki/FAQ_DeferredBindingDefinition

When the GWT Compiler compiles your Java application, it determines all the different "idiosyncrasies" that it must support, and generates a separate, tightly streamlined version of the application for that specific configuration. For example, it generates a different version of the application file for Firefox than it does for Opera.

...

Another classic example of Deferred Binding is internationalization: the GWT Compiler uses Deferred Binding to generate a completely separate version of the application for each language. Why should an English speaker have to download the French text of your application?

GWT uses deferred binding and compiles the Java code to different Javascripts optimized for the target browsers and for each of them GWT can also generate a subset of JSs for every language to allow i18n.

Now, as of the explanation of deferred binding, this is all done at compile time, but actually at runtime, when there's an incoming request to the server, how does the application wrote with GWT know whether the browser is Chrome, Firefox or Opera? Does it parse the User-Agent header of the request? (I would doubt about it because it is not so reliable) Does it uses a sort of a Javascript 'bootstrap' client side code which quickly determines at runtime the browser of the user and then makes an async request to the server to download the proper optimized, language specific Javascript code for that browser with that locale?

How does the magic happen at all?

Thanks for the attention!


回答1:


Does it uses a sort of a Javascript 'bootstrap' client side code which quickly determines at runtime the browser of the user and then makes an async request to the server to download the proper optimized, language specific Javascript code for that browser with that locale?

That's exactly what happens. There is a very small bootstrap javascript file (yourmodulename.nocache.js) that you load from your html page (using the normal <script> tag). This script determines the browser type using navigator.userAgent, and from there it dynamically loads the 'browser dependent' part (which is supposed to be cached as it may be large).

At compile time, GWT determines how many permutations are required (combinations of external factors, e.g. browser model, resource languages etc). Then for each combination a resulting javascript module is generated.
While you would deploy all of these files, each client will download only the one that is relevant to him when he visits your site.



来源:https://stackoverflow.com/questions/28125954/how-does-gwt-provide-the-correct-javascript-code-to-every-browser-e-g-to-carry

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