OmniFaces CombinedResourceHandler does not combine JavaScript resources

707 views Asked by At

I would like to use the OmniFaces CombinedResourceHandler to stream resources in one go.

I registered it in faces-config.xml without any additional configuration parameters as described in CombinedResourceHandler documentation.

While it works fine with CSS resources, it does nothing with JavaScript resources. Here are my tests:

<!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:f="http://java.sun.com /jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:o="http://omnifaces.org/ui">

<h:head>
    <title>CombinedResourceHandlerTest</title>
    <h:outputStylesheet name="css/bootstrap-3.3.5/bootstrap.css"/>
    <h:outputStylesheet name="css/main.css"  />
    <h:outputScript name="js/jquery/jquery.min.js"/>
    <h:outputScript name="js/bootstrap-3.3.5/bootstrap.min.js"/>    
</h:head>
<h:body>
  <f:view>
    <h2>CombinedResourceHandlerTest</h2>
   </f:view>
</h:body>    

Output:

<html xmlns="http://www.w3.org/1999/xhtml">
  <head id="j_idt2">        
    <title>CombinedResourceHandlerTest</title>
    <script type="text/javascript" src="/testApp/javax.faces.resource/js/jquery/jquery.min.js"></script>
    <script type="text/javascript" src="/testApp/javax.faces.resource/js/bootstrap-3.3.5/bootstrap.min.js"></script>
    <link type="text/css" rel="stylesheet" href="/testApp/javax.faces.resource/eNpLLi7WT8rPLykuKUos0DXWM9YzRfD1kouLa4BYPzcxMw_EAQCLpxEP.css?ln=omnifaces.combined&amp;v=1480321351184">
</head>

Tried with attribute target="head":

<h:head>
     <h:outputScript name="js/jquery/jquery.min.js" target="head"/>     
</h:head>
  ...

Output: (scripts are completly missing):

<html xmlns="http://www.w3.org/1999/xhtml">
   <head id="j_idt2">

     <title>CombinedResourceHandlerTest</title>
     <link type="text/css" rel="stylesheet" href="/testApp/javax.faces.resource/eNpLLi7WT8rPLykuKUos0DXWM9YzRfD1kouLa4BYPzcxMw_EAQCLpxEP.css?ln=omnifaces.combined&amp;v=1480321351184">
   </head>
   ...
</html>

The scripts are also missing when i move them on the top of the body:

 <h:body>
    <h:outputScript name="js/jquery/jquery.min.js" target="head"/>     
    ....
 </h:body>    

After a look into the source i also tried with

<o:deferredScript name="js/jquery/jquery.min.js"/>

After inspecting the output for this case, I saw that the combinend script only contains the first script in order and the console shows "ReferenceError: OmniFaces is not defined":

<body>
    <script type="text/javascript">OmniFaces.DeferredScript.add('/testApp/javax.faces.resource/eNpLL81JLE7OsMoq1s8qLE0tqoRSermZeXpZxQDDagwa.js?ln=omnifaces.combined&amp;v=0');</script>
</body>

And I noticed, that even jsf.js is not included when having the CombinedResourceHandler active. the browser console tells "mojarra is not defined".

What am I doing wrong? Thanks in advance!

My environment is: Mojarra 2.2.12, Omnifaces 2.5.1, Tomcat 8.

2

There are 2 answers

1
BalusC On BEST ANSWER

I reproduced a very similar issue last weekend. The cause boiled down to that Mojarra was initialized twice on a Tomcat 8 server and thus corrupted the one and other. You can confirm this by looking at the server log and notice that among others Mojarra version, OmniFaces version and PrimeFaces version are logged twice.

Please doubleverify if you have only one Mojarra instance and that you do not have the ConfigureListener entry in web.xml like below, as it's by default autoregistered already.

<!-- You MUST remove this one from web.xml! -->
<!-- This is actually a workaround for buggy GlassFish3 and Jetty servers. -->
<!-- When leaving this in and you're targeting Tomcat, you'll run into trouble. -->
<listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>

See also:

0
Melloware On

For anyone else who runs into this issue I was also running into this issue using Jboss EAP 7.1.

I accidentally had the OmniFacesCombinedResourceHandler declared in a faces-config.xml in a Web Fragment JAR and in the faces-config.xml of the web application itself. Having this declared twice caused the same symptoms as the issue listed above. Once I removed it from the webapp faces-config.xml it started working.

I raised the issue on the OmniFaces issues to possibly detect and raise an error if this situation occurs: https://github.com/omnifaces/omnifaces/issues/504