• cdi
  • components
  • contexts
  • converters
  • eventlisteners
  • exceptionhandlers
  • facesviews
  • filters
  • functions
  • managedbeans
  • push
  • resourcehandlers
  • scripts
  • servlets
  • taghandlers
  • utils
  • validators
  • viewhandlers
-
  • CDNResourceHandler
  • CombinedResourceHandler
  • UnmappedResourceHandler

This ResourceHandler implementation allows the developer to provide external (CDN) URLs instead of the default local URLs for JSF resources. This also works on auto-included resources provided as ResourceDependency by the JSF implementation and/or component libraries. For example, JSF's own javax.faces:jsf.js resource or PrimeFaces' primefaces:jquery/jquery.js resource could be pointed to a CDN.

For non-JSF resources, you can just keep using plain HTML <script> and <link> elements referring the external URL.

Installation

To get it to run, this handler needs be registered as follows in faces-config.xml:

<application>
    <resource-handler>org.omnifaces.resourcehandler.CDNResourceHandler</resource-handler>
</application>

Standard configuration

To configure the CDN URLs, a "org.omnifaces.CDN_RESOURCE_HANDLER_URLS" context parameter has to be provided wherein the CDN resources are been specified as a comma separated string of libraryName:resourceName=http://cdn.example.com/url key=value pairs. The key represents the default JSF resource identifier and the value represents the full CDN URL, including the scheme. The CDN URL is not validated by this resource handler, so you need to make absolutely sure yourself that it is valid. Here's an example:

<context-param>
    <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
    <param-value>
        js/script1.js=http://cdn.example.com/js/script1.js,
        somelib:js/script2.js=http://cdn.example.com/somelib/js/script2.js,
        otherlib:style.css=http://cdn.example.com/otherlib/style.css,
        somelib:images/logo.png=http://cdn.example.com/somelib/logo.png
    </param-value>
</context-param>

With the above configuration, the following resources:

<h:outputScript name="js/script1.js" />
<h:outputScript library="somelib" name="js/script2.js" />
<h:outputStylesheet library="otherlib" name="style.css" />
<h:graphicImage library="somelib" name="images/logo.png" />

Will be rendered as:

<script type="text/javascript" src="http://cdn.example.com/js/script1.js"></script>
<script type="text/javascript" src="http://cdn.example.com/somelib/js/script2.js"></script>
<link type="text/css" rel="stylesheet" href="http://cdn.example.com/otherlib/style.css" />
<img src="http://cdn.example.com/logo.png" />

Wildcard configuration

You can also use the wildcard syntax to map every single resource of a specific library to a common CDN URL. To achieve that, just use * as the sole resource name and make sure that the CDN URL ends with /*. Here's an example:

<context-param>
    <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
    <param-value>jquery-cdn:*=http://code.jquery.com/*</param-value>
</context-param>
With the above configuration, the following resources:
<h:outputScript library="jquery-cdn" name="jquery-1.9.1.js" />
<h:outputScript library="jquery-cdn" name="ui/1.10.3/jquery-ui.js" />

Will be rendered as:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

EL expressions

The CDN resource handler supports evaluating EL expessions in the CDN URL. Here's an example:

<context-param>
    <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_URLS</param-name>
    <param-value>jquery-cdn:*=http://#{settings.jqueryCDN}/*</param-value>
</context-param>

The EL expression is resolved on a per-request basis.

Conditionally disable CDN resource handler

If you'd like to supply a context parameter which conditionally disables the CDN resource handler, then set the context parameter "org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED" accordingly.

<context-param>
    <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED</param-name>
    <param-value>true</param-value>
</context-param>
<!-- or -->
<context-param>
    <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED</param-name>
    <param-value>#{facesContext.application.projectStage eq 'Development'}</param-value>
</context-param>
<!-- or -->
<context-param>
    <param-name>org.omnifaces.CDN_RESOURCE_HANDLER_DISABLED</param-name>
    <param-value>#{someBean.someBooleanProperty}</param-value>
</context-param>

The EL expression is resolved on a per-request basis.

CombinedResourceHandler

If you're also using the CombinedResourceHandler, then you need to understand that CDN resources can simply not be combined, as that would defeat the CDN purpose. The CombinedResourceHandler will therefore automatically exclude all CDN resources from combining.