0

I'm migrating from JBoss EAP 7.1 to 7.3 at the moment and one of my problems is related to RichFaces – which is EOL with v4.5.17 for a very long time – I know. But nevertheless I need to cope with this at the moment.

Somehow the resources for RichFaces are now included multiple times within the generated HTML markup which delays the loading of the UI significantly. The browser now has to handle multiple unnecessary requests which takes several seconds while loading the page.

Is there any known solution to this problem other than ditching RichFaces completely?

Example of generated HTML

I could not find anything so far...

Daniel Bleisteiner
  • 3,190
  • 1
  • 33
  • 47
  • I assume you already checked if you included the RichFaces jar multiple times. Or try on a page without PrimeFaces or checked for multiple h:head or... – Kukeltje May 07 '20 at 06:14
  • Sure... this is handled by usage within the JSF context (`` with used components) – not manually. It doesn't happen with EAP 7.1.6 from which I'm migrating. – Daniel Bleisteiner May 07 '20 at 06:17
  • The number of includes seems to relate to the number of used components per page. – Daniel Bleisteiner May 07 '20 at 06:20
  • ...not 1:1 though... a single `` already triggers 10 includes. But the more components used to the higher the number. – Daniel Bleisteiner May 07 '20 at 06:27
  • Good investigation. Creating a [mcve] upfront would have shown this already. Effectively allowing you to create a better title (can you still improve it now) And JBoss EAP 7.3 uses which JSF? Maybe Richfaces is not 'forward compatible', maybe you can now even find a duplicate or some other reference on the internet – Kukeltje May 07 '20 at 06:40
  • 1
    https://stackoverflow.com/questions/23824219/exclude-richfaces-js-files-in-combinedresourcehandler (looks like richfaces did some strange things that might not work anymore in newer JSF versions) – Kukeltje May 07 '20 at 06:54

2 Answers2

0

I've implemented a Servlet Filter to fix the resulting HTML output before it is handed over to the client. It removes all duplicates of <link> und <script> elements at all – not only limited to these particular RichFaces embeds.

public class RichFacesDuplicatesFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        // Ignore resources...
        final String path = ((HttpServletRequest) request).getServletPath();
        if (path.startsWith("/javax.faces.resource")) {
            chain.doFilter(request, response);
            return;
        }

        // Wrapper via https://stackoverflow.com/a/23381235
        CapturingResponseWrapper wrapper = new CapturingResponseWrapper((HttpServletResponse) response);
        chain.doFilter(request, wrapper);

        String content = wrapper.getCaptureAsString();
        content = this.removeDuplicates(content, "<link [^>]+\\/>");
        content = this.removeDuplicates(content, "<script [^>]+><\\/script>");

        response.getWriter().write(content);
    }

    private String removeDuplicates(String content, String regex) {
        int     index   = 0;
        Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | Pattern.MULTILINE);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find(index)) {
            index = matcher.end();
            content = content.substring(0, index) 
                    + content.substring(index).replace(matcher.group(), "");
            matcher = pattern.matcher(content);
        }
        return content;
    }
}

This adds a (very) little overhead to each request and solves that problem for now. We hope to get rid of RichFaces at all in the future.

PS: Wrapper via https://stackoverflow.com/a/23381235

Daniel Bleisteiner
  • 3,190
  • 1
  • 33
  • 47
0

I noticed the same behavior in my "old" application after upgrading to JSF 2.3.9.

With Richfaces 4.5.17.Final it seems to be enough to deactivate the ressource optimization

(in web.xml)

<context-param>
    <param-name>org.richfaces.resourceOptimization.enabled</param-name>
    <param-value>false</param-value>
</context-param>