Detect if GWT *.nocache.js file loaded properly

2019-08-22 02:34发布

In Javascript, what is the preferred way to validate if a GWT *.nocache.js file has loaded properly?

Background My GWT application loads an *.nocache.js file within a simple shell .html page.

If a user visits the page with a stale auth cookie, the .html file loads perfectly from browser cache, but the *.nocache.js file fails to load, because the user needs a fresh auth token. Since the .js file fails to load, it fails silently. The user sees a blank .html page with no indication that they need to refresh the page.

(note the particular failure here is that the .js file does not load due to wrong mime type. The auth layer handles stale auth cookies by redirecting to the login page. This page is a text/html document so the browser rejects loading it in a tag. Assume for this question that I cannot change this behavior in the application :)

What's the best way to detect this circumstance and, for example, force a refresh of the page.

Note a hard refresh will force a fetch of the .html page from the server, which will be redirect to the login. One approach would be to tell the browser to not cache the .html file, but I'd prefer another solution that lets the .html file be cached.

2条回答
做个烂人
2楼-- · 2019-08-22 03:10

Typically, a GWT app loads first, then you do authentication. You can use a split point, if you want, to load only the login page. Then, after the authentication is confirmed, you load the other parts of your app.

I have never seen a scenario where authentication is done before a page loads. Maybe you can explain why you did it this way.

As for your question, you need a JavaScript to detect if another JavaScript was successfully loaded, but this solution adds an unnecessary level of complexity.

查看更多
劫难
3楼-- · 2019-08-22 03:25

Given that you cannot change things in your application, I will answer just to your question.

You need some javascript in your page.html in order to check whether the gwt script has been loaded after a fixed time:

  <head>
      <script>
        setTimeout(function() {
          if (!document.getElementById("my_module_name")) {
              window.location.reload();
          }  
        }, 4000)
      </script>
      <script language="javascript" src="my_module_name.nocache.js"></script>
  </head>

In the case you use an iframe based linker (standard, xsiframe), the .nocache.js creates an iframe to load the appropriate permutation, and gives it the name of the module, so checking for the presence of that element after a while is enough to know whether the app was loaded.

You could also check for the presence of especial properties which gwt sets to the window like window.__gwt_activeModules

查看更多
登录 后发表回答