Search code examples
tomcatgziptomcat8

Loading pre-compressed JS files (.js.gz) in tomcat 8


I want to refer pre-compressed JS files (ie sample.js.gz) in my html hosted on a tomcat 8 web server. I have changed the server.xml for gzip compression. Am able to refer the JS files directly and get the compressed gzip of the JS files served by the tomcat server.

But the browser throws sample.js.gz:1 Uncaught SyntaxError: Invalid or unexpected token error when referred the .gz file directly in the html

In the response header the content type is set as Content-Type:application/x-gzip and encoding is missing when .gz files are directly referred.

Can some one help me to address this issue ?


Solution

  • as you can see there is an open request still... Anyway setting for default servlet

    <init-param>
       <param-name>gzip</param-name>
       <param-value>true</param-value>
    </init-param>
    

    works fine for me. Be sure you reference your js files as file-name.JS and NOT file-name.ja.GZ! (received x-gzip content type speaks about that mistake).

    As alternative you can: or to create a custom Filter that will put a header "Content-Encoding: gzip" manually, or to play with GzipResourceResolver, or to configure Connector.