Home / Content-type for Javascript with Apache

Content-type for Javascript with Apache

I was making some changes to an Apache configuration this morning to ensure mod_deflate was compressing CSS and Javascript files and discovered I’d either documented the content-type for Javascript incorrectly, or the Apache version and Linux distro I used at the time served Javascript differently.

text/x-js vs application/javascript content-type

In the above linked post it’s documented as being text/x-js. This worked for me at the time on that particular host. When I tried to configure this on a Debian host this morning it had no effect.

To find out what content-type was being served, I used the command line “lynx” web browser with the -head flag to display the headers like so (yes, that .local address is correct – I was testing this on my local development box which had the domain configured like that):

lynx -head -dump http://www.electrictoolbox.local/js/common.min.js

The output from this was:

HTTP/1.1 200 OK
Date: Tue, 19 Jan 2010 18:16:31 GMT
Server: Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny3 with Suhosin-Patch
Last-Modified: Mon, 18 Jan 2010 18:08:20 GMT
ETag: "276030-1c8d3-47d885ad86d00"
Accept-Ranges: bytes
Content-Length: 116947
Vary: Accept-Encoding
Connection: close
Content-Type: application/javascript

Note the last line which shows the content type.

So using the application/javascript content-type I was then able to successfully have mod_deflate compress my Javascript files. I have also seen it in the past served as application/x-javascript

If you are having difficulty configuring mod_deflate and it doesn’t seem to be compressing the files, then check the content-type using lynx as shown above and use that content type.