简体   繁体   中英

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

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!

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, eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM