Reputation: 382
For the moment I load googleAds scripts from Omnifaces CDNResourceHandler which generates
<script type="text/javascript" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
Now if I try to use Omnifaces deferredScript with CDN resource handler to load the same library then the browser displays message
Access to Script at 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js' from origin 'https://xxxx' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Generated html
<script type="text/javascript">OmniFaces.DeferredScript.add('https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js');</script>
Could you explain how to make it work? It's possible to combine deferredScript and CDNResourceHandler utilities but it seems it generates CORS problems. How to solve it?
Upvotes: 1
Views: 182
Reputation: 1109532
The <o:deferredScript>
was never intented for external resources.
Your technical problem is caused because this tag explicitly sets crossorigin="anonymous"
attribute in order to support script error reporting (so that JavaScript errors can be sent to the server side for logging). Your concrete problem will be solved when that attribute is not set.
You could report an issue to OmniFaces guys and tell them to skip that attribute when the script's src is external. For now, just keep using a plain <script>
element. You can use #{resource}
to let JSF generate the desired URL based on library and name.
<script src="#{resource['cdn:adsbygoogle.js']}">
This allows room for manually setting async="true"
attribute on the element.
Upvotes: 2