5 Replies Latest reply on Jun 18, 2013 3:43 AM by dako ak

    Add version number to RichFaces resources(JS, CSS)

    dako ak Newbie

      Hi all,

       

      I plan to upgrade my RichFaces application from 4.2.x to 4.3.1.

      Now, I have noticed that the old RichFaces resources like JavaScript and CSS files are cached by the browser.

      The problem is, I can't tell all my users that they have to refresh their browser resources or clear their caches.

       

      Is it possible to add a version attribute to the RichFaces css/JavaScript links?

      Something like that:

       

      <script type="text/javascript" src="/javax.faces.resource/jquery.js.jsf?version=4_3_1"></script>

      <script type="text/javascript" src="/javax.faces.resource/richfaces.js.jsf?version=4_3_1"></script>

       

      Best regards

       


        • 1. Re: Add version number to RichFaces resources(JS, CSS)
          Juraj Húska Master

          Hello,

           

          I do not know about the version number, however, can not be this resolved by playing with HTTP cache values in your web server ?

           

          Regards,

          Juro

          • 2. Re: Add version number to RichFaces resources(JS, CSS)
            dako ak Newbie

            Hi Juro,

             

            to add a version parameter to resource links like CSS is a common solution to avoid browser caching of old resources. We do this with our project resource files as well.

             

             

            Best regards

            • 3. Re: Add version number to RichFaces resources(JS, CSS)
              dako ak Newbie

              Hi all,

               

              this is my solution, heavily inspired by PrimeFaces-Extensions.

               

              You have to implement a resource handler and add it to your faces-config.xml:

               

              <resource-handler>de.mycompany.web.tools.RichFacesResourceHandler</resource-handler>

               

               

              Thats the ResourceHandler:

               

              import javax.faces.application.Resource;

              import javax.faces.application.ResourceHandler;

              import javax.faces.application.ResourceHandlerWrapper;

               

              public class RichFacesResourceHandler extends ResourceHandlerWrapper {

               

                  private final ResourceHandler wrapped;

               

                  public RichFacesResourceHandler(final ResourceHandler resourceHandler) {

                      super();

               

                      wrapped = resourceHandler;

                  }

               

                  @Override

                  public ResourceHandler getWrapped() {

                      return wrapped;

                  }

               

                  @Override

                  public Resource createResource(final String resourceName, final String libraryName) {

                      Resource resource = super.createResource(resourceName, libraryName);

               

                      if (resource != null && resource.getRequestPath() != null) {

               

                          String requestPath = resource.getRequestPath();

               

                          if ((requestPath.contains("richfaces") && (requestPath.contains(".js.jsf") || requestPath.contains(".css.jsf") || requestPath

                                  .contains(".ecss.jsf"))) || (requestPath.contains("jquery.js.jsf"))) {

                              resource = new RichFacesResource(resource);

                          }

                      }

               

                      return resource;

                  }

               

              }

               

              Thats the class RichFacesResource. You can set the version number by the maven replacer plugin:

               

              import javax.faces.application.Resource;

              import javax.faces.application.ResourceWrapper;

               

              public class RichFacesResource extends ResourceWrapper {

               

                  private Resource wrapped;

                  private String version;

               

                  public RichFacesResource(final Resource resource) {

                      super();

                      wrapped = resource;

               

                      version = "";

               

                      if (resource.getRequestPath().contains("?")) {

                          version += "&amp;";

                      } else {

                          version += "?";

                      }

               

                      version += "v=";

                      version += "4.3.2.Final";

                  }

               

                  @Override

                  public Resource getWrapped() {

                      return wrapped;

                  }

               

                  @Override

                  public String getRequestPath() {

                      return super.getRequestPath() + version;

                  }

               

                  @Override

                  public String getContentType() {

                      return getWrapped().getContentType();

                  }

               

                  @Override

                  public String getLibraryName() {

                      return getWrapped().getLibraryName();

                  }

               

                  @Override

                  public String getResourceName() {

                      return getWrapped().getResourceName();

                  }

               

                  @Override

                  public void setContentType(final String contentType) {

                      getWrapped().setContentType(contentType);

                  }

               

                  @Override

                  public void setLibraryName(final String libraryName) {

                      getWrapped().setLibraryName(libraryName);

                  }

               

                  @Override

                  public void setResourceName(final String resourceName) {

                      getWrapped().setResourceName(resourceName);

                  }

               

                  @Override

                  public String toString() {

                      return getWrapped().toString();

                  }

              }

              • 4. Re: Add version number to RichFaces resources(JS, CSS)
                Brian Leathem Master

                Nice solution.  How about filing that as an enhancement via a RichFaces pull request?

                 

                https://community.jboss.org/wiki/GuideToUsePullRequestsWithGitHubAndJIRA

                • 5. Re: Add version number to RichFaces resources(JS, CSS)
                  dako ak Newbie

                  Hi Brian,

                   

                  sorry but I'am afraid that I have too less time to create a personal fork. My knowledge about GIT is very low.