检测GWT * .nocache.js文件是否正确加载 [英] Detect if GWT *.nocache.js file loaded properly

查看:133
本文介绍了检测GWT * .nocache.js文件是否正确加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Javascript中,验证GWT * .nocache.js文件是否已正确加载的首选方法是什么?



背景
我的GWT应用程序加载一个* .nocache.js文件在一个简单的shell .html页面中。



如果用户使用陈旧的auth cookie访问页面,那么.html文件可以完美地从浏览器缓存,但* .nocache.js文件无法加载,因为用户需要一个新的身份验证令牌。
由于.js文件无法加载,因此它会自动失败。用户看到一个空白的.html页面,但没有显示他们需要刷新页面。



(注意这里的特定失败是.js文件没有加载到期到错误的mime类型auth层通过重定向到登录页面来处理陈旧的auth cookies这个页面是一个text / html文档,因此浏览器拒绝将它加载到一个标签中假设这个问题我不能在应用程序中改变这种行为:)

检测这种情况的最好方法是什么,例如强制刷新页面。



<请注意,硬刷新将强制从服务器获取.html页面,该页面将重定向到登录页面。
一种方法是告诉浏览器不要缓存.html文件,但我更喜欢另一个让.html文件被缓存的解决方案。

解决方案

鉴于您无法在应用程序中更改内容,我将回答您的问题。



page.html 中需要一些javascript以检查gwt脚本是否已经在固定时间:

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

在使用基于iframe的链接器(标准,xsiframe)的情况下, .nocache.js 创建一个iframe来加载适当的排列,并给它一个模块名称,因此在一段时间后检查该元素是否足以知道该应用程序是否已加载。

你也可以检查gwt设置窗口的特殊属性,比如 window .__ gwt_activeModules


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.

解决方案

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

这篇关于检测GWT * .nocache.js文件是否正确加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆