RWS / dxa-web-application-java

SDL Digital Experience Accelerator Java Spring MVC web application
25 stars 37 forks source link

Use of ContextEngine is hardcoded into DXA #111

Open willprice76 opened 5 years ago

willprice76 commented 5 years ago

The ContextEngine is used to determine device characteristics such as screen width and device family. This is then used in the DefaultMediaHelper and ContextualDeviceUrlBasedViewResolver to implement serverside responsive (RESS) rendering.

Some rough tests I have done for a page (without any caching) give a processing time of 2.2 seconds with the ContextEngine and 1.2 seconds without. Many implementations implement responsive design client side, and would thus prefer not to have this unneccesary overhead on every request.

It would be thus good to have a feature toggle to set use of the ContextEngine on/off.

willprice76 commented 5 years ago

A quick and dirty workaround without rebuilding DXA is to do the following:

  1. Wire in (in a Spring initialization class) a standard ViewResolver before the ContextualDeviceUrlBasedViewResolvers kick in
@Bean
    public ViewResolver fallbackViewResolver() {
        UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix(this.viewResolverPrefix);
        viewResolver.setSuffix(this.viewResolverSuffix);
        viewResolver.setOrder(5);//before the 2 ContextualDeviceUrlBasedViewResolvers (order 10 and 20)
        return viewResolver;
    }
  1. Make your own MediaHelper and override the methods which use the context engine:
@Component
@Primary
public class NonResponsiveMediaHelper extends DefaultMediaHelper {

    //These are the 2 methods that call the context engine in the super class.
    //By overriding them we ensure the Context Engine is never called

    @Override
    public ScreenWidth getScreenWidth() {
        return ScreenWidth.MEDIUM;
    }

    @Override
    public int getResponsiveWidth(String widthFactor, int containerSize) {
        widthFactor = StringUtils.isEmpty(widthFactor) ? "100%" : widthFactor;
        double width = 0.0D;
        if (!widthFactor.endsWith("%")){
            try {
                width = Double.parseDouble(widthFactor);
            } catch (NumberFormatException var11) {
                widthFactor = "100%";
            }
        }
        if (widthFactor.endsWith("%")){
            return super.getResponsiveWidth(widthFactor, containerSize);
        }
        return (int)Math.ceil(width);
    }
}