1

I'm currently working on adding HTML5 offline support to my web application, and am mostly following the same approach of mgwt:

  1. Generate manifest files per permutation at compulation time
  2. Using a servlet to serve the manifest file, based on the user-agent of the browser.

My question involves step 2: In my servlet, I want to detect which permutation to serve for a certain request. The way I do this now is:

  • Get the user agent string from the request
  • Map this (using simple string operations (e.g. userAgent.contains("safari")) to the 'agent id', which I can map using a mapping file to the permutation strong name. In other words, map Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1) to ie6, which I can map (using the generated mapping file, see example below) to 15B454D690F2CCAD57F1DD809429BF42.

    <permutation name="15B454D690F2CCAD57F1DD809429BF42">
      <user.agent>ie6</user.agent>
    </permutation>
    

The problem I'm facing: I want to use the same method of linking a user agent string to a permutation as GWT uses (i.e. map Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1) to ie6). This way I don't have to fix my code whenever my GWT gets updated with other permutations/browser versions. In other words, I don't like my current solution of naively matching the complete user agent string in the servlet with the user agent 'id' (in my example 'ie6') in my permutation mapping. The solutions I thought about were:

  • after page load, detect the loaded permutation, and pass that as argument to the servlet. Then after fetching the manifest for that permutation, insert that as an attribute of the html tag using javascript. However, inserting this dynamically does not seem to work properly. (See Dynamically Trigger HTML5 Cache Manifest file? ) (the same post explains a workaround of doing this dynamically using an iframe, but I prefer a cleaner solution)
  • somehow use the client-side to map the complete user agent string to a permutation, in my servlet. I'd prefer this solution, but havent found a way to do achieve this... GWT uses javascript to achieve this (see UserAgentPropertyGenerator). I could execute this in my servlet, but this javascript method not only uses the user agent, but uses the dom document as well... Are there other solutions I am overlooking? Surely others must have had this issue as well when creating HTML5 manifest files...
Community
  • 1
  • 1
Laurens Rietveld
  • 958
  • 1
  • 11
  • 21
  • I switched to the iframe solution, meaning I didnt have to use a servlet after all. You can find the explanation on how to do this here: http://stackoverflow.com/questions/18721922/how-to-dynamically-serve-manifest-for-gwt-permutations – Laurens Rietveld Sep 10 '13 at 14:42

1 Answers1

0

You can take advantage of the HTML5Manifest solution provided with mgwt. They have a linker which produces a file which can be read by the servlet they provide, and return the appropriate list of files to cache by the browser based on the user-agent header.

If you want to do it by yourself, you can figure out the most suitable permutation per browser, based on the http user-agent header, and on the compilation-mappings.txt file which is generated by the gwt compiler if you are using xsiframe linker.

Manolo Carrasco Moñino
  • 9,723
  • 1
  • 22
  • 27
  • My approach is already largely based on mgwt. However, the servlet they provide uses the same relatively simple string operations on the user agent which I would like to avoid. I don't understand the xsiframe linker approach though: afaik this does not allow me to link the complete user agent string to the user agent 'id' (e.g. 'ie6'), and to the permutation strongname. The compilation-mappings.txt file is similar to the xml file I already generate using the mgwt approach. However the problem I'm facing is how to link the user agent string to this agent 'id' in a future-proof way – Laurens Rietveld Sep 09 '13 at 19:04
  • Ok, so you are doing in the right way. You cannot reuse the client code from the `UserAgentPropertyGenerator` since it is written in pure js because it has to be included in the `module.nocache.js` file. The only way to generate the html5 manifest to include the suitable permutation/s, is based on the user-agent, but you have to rewrite the detection in your java code (or reuse mgwt servlet). You have to be aware that using the user-agent you can figure out permutations based on it: user.agent, mgwt.os, mobile.user.agent, but not other like phonegap.env although you can assume 'no' for it. – Manolo Carrasco Moñino Sep 09 '13 at 20:43
  • hmm, I was afraid of this. And what do you think about using the https://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/user/rebind/UserAgentPropertyGenerator.java?r=9701 in the servlet? (via jsni). I'd have to do a simple operation on the javascript to set the user agent string (instead of retrieving it from the nav object). Only problem is the reference to the dom doc. (this approach is ugly as well, though it would avoid duplicate code in my project) – Laurens Rietveld Sep 10 '13 at 08:23
  • I dont understand well your point, but as I said in my last comment you CANNOT reuse `UserAgentPropertyGenerator` in the servlet, nor you can use JSNI in it. The HTML manifest file is asked by the browser without using javascript, so there is nothing you can do in client side. You only can handle this request in server side using the standard HTTP headers set by the browser (user-agent). – Manolo Carrasco Moñino Sep 10 '13 at 08:34
  • fair enough. This means I have to choose the lesser of two evils: (1) duplicate the gwt javascript code in my servlet, or (2) insert the manifest file client-side (i.e. without a servlet) using javascript and iframes. I'll opt for the second one, as I'd consider this more future-proof. Thanks for your help though: it helped me reduce my number of options. – Laurens Rietveld Sep 10 '13 at 11:00
  • Yeah, those are the two options. I prefer option one though, because once you import mgwt, the solution works out-of-the-box. – Manolo Carrasco Moñino Sep 10 '13 at 14:27