prestashop XHTML/XML rendering error in browsers

I found a very strange problem with one of my ecommerce applications: one day all browsers started to report a XML rendering error of the content so that the website was only partially rendered. I have never found such problem before and it took me quite a while to find the cause.

prestashop XHTML/XML rendering error

The application that started to crash was prestashop 1.5.0.17 written in PHP, but in fact it could be any web application developed on any platform, as long as it returns content-type application/xhtml+xml:

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

apache mod_pagespeed

My hosting provider changed (in fact he was taken over by another one). The new server had mod_pagespeed installed and this was the root of the problem. Despite the name of the module (mainly it speeds up returning responses), it also affects HTTP response type. Until now, the server returned

Content-Type: test/html
HTTP response despite the fact that the mime type inside html response was xhtml, just like below:
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8"/>
After the mod_pagespeed was installed, the server returned:
Content-Type: application/xhtml+xml
When browsers got the xhtml+xml HTTP response type, they started to validate the XML response. After the first validation error, html rendering was stopped. The bad thing about prestashop is that it doesn't support proper xhtml/xml rendering and it uses this mime type as the default one. I hope one day I'll get to know author's intention why is it done like that.

solution

The easiest thing is to change the meta tag to return text/html for each page response. But if you don't want to touch the application code, you may just disable the mime type convertion in .htaccess:

ModpagespeedDisableFilters convert_meta_tags
With both solutions, the browers will display the content correctly.

icanhaz dynamic template name

You can make icanhaz engine render a template, passing a dynamic template name. This can be achieved since in javascript you may access object property using array notation (following statements are equivalent):

object.property
object['property']
Thanks to it you can built any string to be the template name and use icanhaz to render it:
ich['templateName'](params);
For example, instead of:
ich.incomeFormTemplate(params);
ich.outcomeFormTemplate(params);
you can call:
var type = 'income'; // or "outcome"
ich[type + "FormTemplate"](params);

java thrift project setup

Apache thrift is a magnificent interface communication platform that allows different platforms (C++, Java, PHP, Python and many more) to communicate with each other easily. Big projects, such as Cassandra, use thrift - but I'm gonna focus on basic setup for java thrift project. All you need to do is to fetch thrift lib along with its dependencies.

maven installation

If your project uses Maven, it's gonna be very easy - just add the following lines to your pom.xml file (latest stable thrift version is 0.9.0):

<dependency>
    <groupId>org.apache.thrift</groupId>
    <artifactId>libthrift</artifactId>
    <version>0.9.0</version>
</dependency>
Building the project will download all necessary dependencies, so you don't have to worry about that. You can see an example pom.xml file on my github thrift project.

non-maven installation

There'll be slightly more work here. Download the .tar.gz archive, unpack it and follow these steps to build appropriate jar:

  • browse to lib/java/
  • run ant in terminal
  • get the .jar from the build directory
Now you may use this .jar archive inside your project. Additionally, you have to manually download dependent libraries (or set your project to use them, if they're available locally). These dependencies include slf4j, commons-lang and few more (see full list here).