JSF2 Static Resource Management — Combined, Compressed

后端 未结 4 1458
一向
一向 2021-02-07 08:43

Is anyone aware of a method to dynamically combine/minify all the h:outputStylesheet resources and then combine/minify all h:outputScript resources in the render phase? The comi

相关标签:
4条回答
  • 2021-02-07 08:52

    You may want to evaluate JAWR before implementing your own solution. I've used it in couple of projects and it was a big success. It used in JSF 1.2 projects but I think it will be easy to extend it to work with JSF 2.0. Just give it a try.

    0 讨论(0)
  • 2021-02-07 09:06

    This answer doesn't cover minifying and compression. Minifying of individual CSS/JS resources is better to be delegated to build scripts like YUI Compressor Ant task. Manually doing it on every request is too expensive. Compression (I assume you mean GZIP?) is better to be delegated to the servlet container you're using. Manually doing it is overcomplicated. On Tomcat for example it's a matter of adding a compression="on" attribute to the <Connector> element in /conf/server.xml.


    The SystemEventListener is already a good first step (apart from some PhaseListener unnecessity). Next, you'd need to implement a custom ResourceHandler and Resource. That part is not exactly trivial. You'd need to reinvent pretty a lot if you want to be JSF implementation independent.

    First, in your SystemEventListener, you'd like to create new UIOutput component representing the combined resource so that you can add it using UIViewRoot#addComponentResource(). You need to set its library attribute to something unique which is understood by your custom resource handler. You need to store the combined resources in an application wide variable along an unique name based on the combination of the resources (a MD5 hash maybe?) and then set this key as name attribute of the component. Storing as an application wide variable has a caching advantage for both the server and the client.

    Something like this:

    String combinedResourceName = CombinedResourceInfo.createAndPutInCacheIfAbsent(resourceNames);
    UIOutput component = new UIOutput();
    component.setRendererType(rendererType);
    component.getAttributes().put(ATTRIBUTE_RESOURCE_LIBRARY, CombinedResourceHandler.RESOURCE_LIBRARY);
    component.getAttributes().put(ATTRIBUTE_RESOURCE_NAME, combinedResourceName + extension);
    context.getViewRoot().addComponentResource(context, component, TARGET_HEAD);
    

    Then, in your custom ResourceHandler implementation, you'd need to implement the createResource() method accordingly to create a custom Resource implementation whenever the library matches the desired value:

    @Override
    public Resource createResource(String resourceName, String libraryName) {
        if (RESOURCE_LIBRARY.equals(libraryName)) {
            return new CombinedResource(resourceName);
        } else {
            return super.createResource(resourceName, libraryName);
        }
    }
    

    The constructor of the custom Resource implementation should grab the combined resource info based on the name:

    public CombinedResource(String name) {
        setResourceName(name);
        setLibraryName(CombinedResourceHandler.RESOURCE_LIBRARY);
        setContentType(FacesContext.getCurrentInstance().getExternalContext().getMimeType(name));
        this.info = CombinedResourceInfo.getFromCache(name.split("\\.", 2)[0]);
    }
    

    This custom Resource implementation must provide a proper getRequestPath() method returning an URI which will then be included in the rendered <script> or <link> element:

    @Override
    public String getRequestPath() {
        FacesContext context = FacesContext.getCurrentInstance();
        String path = ResourceHandler.RESOURCE_IDENTIFIER + "/" + getResourceName();
        String mapping = getFacesMapping();
        path = isPrefixMapping(mapping) ? (mapping + path) : (path + mapping);
        return context.getExternalContext().getRequestContextPath()
            + path + "?ln=" + CombinedResourceHandler.RESOURCE_LIBRARY;
    }
    

    Now, the HTML rendering part should be fine. It'll look something like this:

    <link type="text/css" rel="stylesheet" href="/playground/javax.faces.resource/dd08b105bf94e3a2b6dbbdd3ac7fc3f5.css.xhtml?ln=combined.resource" />
    <script type="text/javascript" src="/playground/javax.faces.resource/2886165007ccd8fb65771b75d865f720.js.xhtml?ln=combined.resource"></script>
    

    Next, you have to intercept on combined resource requests made by the browser. That's the hardest part. First, in your custom ResourceHandler implementation, you need to implement the handleResourceRequest() method accordingly:

    @Override
    public void handleResourceRequest(FacesContext context) throws IOException {
        if (RESOURCE_LIBRARY.equals(context.getExternalContext().getRequestParameterMap().get("ln"))) {
            streamResource(context, new CombinedResource(getCombinedResourceName(context)));
        } else {
            super.handleResourceRequest(context);
        }
    }
    

    Then you have to do the whole lot of work of implementing the other methods of the custom Resource implementation accordingly such as getResponseHeaders() which should return proper caching headers, getInputStream() which should return the InputStreams of the combined resources in a single InputStream and userAgentNeedsUpdate() which should respond properly on caching related requests.

    @Override
    public Map<String, String> getResponseHeaders() {
        Map<String, String> responseHeaders = new HashMap<String, String>(3);
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_RFC1123_DATE, Locale.US);
        sdf.setTimeZone(TIMEZONE_GMT);
        responseHeaders.put(HEADER_LAST_MODIFIED, sdf.format(new Date(info.getLastModified())));
        responseHeaders.put(HEADER_EXPIRES, sdf.format(new Date(System.currentTimeMillis() + info.getMaxAge())));
        responseHeaders.put(HEADER_ETAG, String.format(FORMAT_ETAG, info.getContentLength(), info.getLastModified()));
        return responseHeaders;
    }
    
    @Override
    public InputStream getInputStream() throws IOException {
        return new CombinedResourceInputStream(info.getResources());
    }
    
    @Override
    public boolean userAgentNeedsUpdate(FacesContext context) {
        String ifModifiedSince = context.getExternalContext().getRequestHeaderMap().get(HEADER_IF_MODIFIED_SINCE);
    
        if (ifModifiedSince != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_RFC1123_DATE, Locale.US);
    
            try {
                info.reload();
                return info.getLastModified() > sdf.parse(ifModifiedSince).getTime();
            } catch (ParseException ignore) {
                return true;
            }
        }
    
        return true;
    }
    

    I've here a complete working proof of concept, but it's too much of code to post as a SO answer. The above was just a partial to help you in the right direction. I assume that the missing method/variable/constant declarations are self-explaining enough to write your own, otherwise let me know.


    Update: as per the comments, here's how you can collect resources in CombinedResourceInfo:

    private synchronized void loadResources(boolean forceReload) {
        if (!forceReload && resources != null) {
            return;
        }
    
        FacesContext context = FacesContext.getCurrentInstance();
        ResourceHandler handler = context.getApplication().getResourceHandler();
        resources = new LinkedHashSet<Resource>();
        contentLength = 0;
        lastModified = 0;
    
        for (Entry<String, Set<String>> entry : resourceNames.entrySet()) {
            String libraryName = entry.getKey();
    
            for (String resourceName : entry.getValue()) {
                Resource resource = handler.createResource(resourceName, libraryName);
                resources.add(resource);
    
                try {
                    URLConnection connection = resource.getURL().openConnection();
                    contentLength += connection.getContentLength();
                    long lastModified = connection.getLastModified();
    
                    if (lastModified > this.lastModified) {
                        this.lastModified = lastModified;
                    }
                } catch (IOException ignore) {
                    // Can't and shouldn't handle it here anyway.
                }
            }
        }
    }
    

    (the above method is called by reload() method and by getters depending on one of the properties which are to be set)

    And here's how the CombinedResourceInputStream look like:

    final class CombinedResourceInputStream extends InputStream {
    
        private List<InputStream> streams;
        private Iterator<InputStream> streamIterator;
        private InputStream currentStream;
    
        public CombinedResourceInputStream(Set<Resource> resources) throws IOException {
            streams = new ArrayList<InputStream>();
    
            for (Resource resource : resources) {
                streams.add(resource.getInputStream());
            }
    
            streamIterator = streams.iterator();
            streamIterator.hasNext(); // We assume it to be always true; CombinedResourceInfo won't be created anyway if it's empty.
            currentStream = streamIterator.next();
        }
    
        @Override
        public int read() throws IOException {
            int read = -1;
    
            while ((read = currentStream.read()) == -1) {
                if (streamIterator.hasNext()) {
                    currentStream = streamIterator.next();
                } else {
                    break;
                }
            }
    
            return read;
        }
    
        @Override
        public void close() throws IOException {
            IOException caught = null;
    
            for (InputStream stream : streams) {
                try {
                    stream.close();
                } catch (IOException e) {
                    if (caught == null) {
                        caught = e; // Don't throw it yet. We have to continue closing all other streams.
                    }
                }
            }
    
            if (caught != null) {
                throw caught;
            }
        }
    
    }
    

    Update 2: a concrete and reuseable solution is available in OmniFaces. See also CombinedResourceHandler showcase page and API documentation for more detail.

    0 讨论(0)
  • 2021-02-07 09:06

    I have an other solution for JSF 2. Might also rok with JSF 1, but i do not know JSF 1 so i can not say. The Idea works mainly with components from h:head and works also for stylesheets. The result is always one JavaScript (or Stylesheet) file for a page! It is hard for me to describe but i try.

    I overload the standard JSF ScriptRenderer (or StylesheetRenderer) and configure the renderer for the h:outputScript component in the faces-config.xml. The new Renderer will now not write anymore the script-Tag but it will collect all resources in a list. So first resource to be rendered will be first item in the list, the next follows and so on. After last h:outputScript component ist rendered, you have to render 1 script-Tag for the JavaScript file on this page. I make this by overloading the h:head renderer.

    Now comes the idea: I register an filter! The filter will look for this 1 script-Tag request. When this request comes, i will get the list of resources for this page. Now i can fill the response from the list of resources. The order will be correct, because the JSF rendering put the resources in correct order into the list. After response is filled, the list should be cleared. Also you can do more optimizations because you have the code in the filter....

    I have code that works superb. My code also can handle browser caching and dynamic script rendering. If anybody is interested i can share the code.

    0 讨论(0)
  • 2021-02-07 09:07

    Omnifaces provided CombinedResourceHandler is an excellent utility, but I also love to share about this excellent maven plugin:- resources-optimizer-maven-plugin that can be used to minify/compress js/css files &/or aggregate them into fewer resources during the build time & not dynamically during runtime which makes it a more performant solution, I believe.

    Also have a look at this excellent library as well:- webutilities

    0 讨论(0)
提交回复
热议问题