如何从CDN服务GWT静态文件 [英] How to serve GWT static files from a CDN
问题描述
我开始使用这个程式码
<!DOCTYPE html>
< html lang =en>
< head>
< meta charset =utf-8/>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/>
< meta name =robotscontent =no-index,no-follow/>
< meta name =viewportcontent =width = device-width,initial-scale = 1.0/>
< title>我的网站< / title>
< script type =text / javascriptsrc =/ bcmjs / bcmjs.nocache.js>< / script>
< / head>
< body>
< div id =gwt_div>
< / div>
< / body>
< / html>
并修改javascript import以使用CDN网址,从这里:
< script type =text / javascriptsrc =/ bcmjs / bcmjs.nocache.js>< / script&
到此:
< script type =text / javascriptsrc =https://uoo9w.cloudfront.net/bcmjs/bcmjs.nocache.js>< / script>
Hoorah!
问题似乎是 GWT.getModuleBase
返回Javascript的网址(uoo9w.cloudfront.net),而不是主机页面(mysite.example.com),它会破坏像用于RPC请求的URL。
GWT.getModuleBaseForStaticFiles
似乎是一个允许CDN使用的方法,但我找不到关于它的文档。评论)
有没有人知道正确的方法来设置GWT从CDN提供服务,并向主机页面的域发送RPC请求?
注:
由于CDN有不同的域名和路径,我担心会出现问题涉及同源策略,但是由于GWT主机页面在与所有RPC请求相同的域上提供,这不是问题。 (即Window.Location是与RPC相同的域)
GWT设置RPC的URL指向<$ modulename.nocache.js
脚本标记而不是位置的
属性。
修复它的常见方法是更改 rpc
service to point to the location
of your html file
GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
((ServiceDefTarget)greetingService)
.setServiceEntryPoint(http:// hostname_of_your_document / modulename / greet);
注意:在这种情况下,您不会在 crossdomain c $ c> .html 和您的服务
在同一个主机中。
可选地,如果 services
不在 .html
文件所在的主机中,跨域,因此您可以配置您的servlet以支持 CORS 。最好的方法是在 web.xml
< filter>
< filter-name> corsFilter< / filter-name>
< filter-class> com.example.server.CORSFilter< / filter-class>
< / filter>
< filter-mapping>
< filter-name> corsFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>
对于过滤器类,你可以使用我为 gwtquery Ajax文档
I am trying to figure out how to serve my GWT files from a CDN instead of from a tomcat server.
I started with this code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="robots" content="no-index, no-follow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My Site</title>
<script type="text/javascript" src="/bcmjs/bcmjs.nocache.js"></script>
</head>
<body>
<div id="gwt_div">
</div>
</body>
</html>
And modified the javascript import to use the CDN url, from this:
<script type="text/javascript" src="/bcmjs/bcmjs.nocache.js"></script>
To this:
<script type="text/javascript" src="https://uoo9w.cloudfront.net/bcmjs/bcmjs.nocache.js"></script>
Hoorah! The js downloads, UIBinder widgets are visible, but RPC fails.
The problem seems to be that GWT.getModuleBase
returns the URL of the Javascript (uoo9w.cloudfront.net) instead of the host page (mysite.example.com), which breaks things like the URL used for RPC requests.
(See comments)GWT.getModuleBaseForStaticFiles
seems to be a method built allow CDN use, but I can't find documentation about it.
Does anyone know of the correct way to set up GWT to serve from a CDN and send RPC request to the host page's domain?
Side Note:
Since the CDN has a different domain name and path, I was worried there were going to be problems involving the Same Origin Policy, but since the GWT host page is served on the same domain as all the RPC requests, this is not a problem. (ie. Window.Location is the same domain as the RPC)
GWT sets the url of RPCs pointing to the src
attribute of the modulename.nocache.js
script-tag instead of the location
of your document.
The normal way to fix it is that you change the base url of your rpc
service to point to the location
of your html file
GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
((ServiceDefTarget)greetingService)
.setServiceEntryPoint("http://hostname_of_your_document/modulename/greet");
NOTE: in this case you are not doing crossdomain since your .html
and your services
are in the same host.
OPTIONALLY, if the services
weren't in the same host than the .html
file, you were doing crossdomain, so you might configure your servlet to support CORS. The best way is to configure a filter in your web.xml
<filter>
<filter-name>corsFilter</filter-name>
<filter-class>com.example.server.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>corsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
For the filter class you can take the example I did for the gwtquery Ajax documentation
这篇关于如何从CDN服务GWT静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!