Anne van Kesteren

MIME types you should use

Robbert Broersma and I used to mail about this in the past. It isn't very interesting, but quite important.

text/html
Use this for all your HTML documents. Basically every document that has to be accessed by Internet Explorer should have this MIME type. If it weren't for service pack 2, you could have used text/plain instead, but that would have given some trouble in other, more compliant browsers. (Not that Internet Explorer is the only evil browser.)
text/css
The only MIME type available for style sheets. While you can store your content in many different forms, that is no option for your style rules.
application/xml
This is the one that is supposed to be used for your non-specific XML documents. If you are still using text/xml (you can check that quite easily with ctrl+i on Mozilla, Windows), switch today. text/xml is in process of being deprecated and already has serious encoding problems.
application/xhtml+xml
You are using XHTML? Probably not, although you might not know it or accept it. Nevertheless, this is the MIME type you should be using when you have a nice XHTML DOCTYPE or using the HTML element in the XHTML namespace as root.
application/x-javascript
Don't use it, use text/javascript instead, although it is unofficial, it is the best supported of the two (read: the only one Internet Explorer supports). application/x-javascript is the only one that is semi-official though (the x-prefix is used, since it hasn't been registered yet).

I consider these three MIME types as the most important, but probably not most used, on the internet. Other XML MIME types include application/rdf+xml for RDF documents (think of RSS 1.0), the not yet official application/atom+xml which is used for Atom (not an abbreviation) documents. For other RSS feeds I use application/rss+xml, which is also not official yet and probably never will be since RSS doesn't have the intention to become a standard.

There is also an XML DTD MIME type: application/xml-dtd. Note that this one uses a hyphen, since a DTD doesn't use XML syntax. Furthermore there is text/plain and if you mix MIME types (using content negotiation or similar) you will probably get into trouble because of RFC 3023 - XML Media Types, which Mark Pilgrim explains as well in XML on the Web Has Failed. I'm not going to explain it here, but I can tell you, you have to know about it the other day. Using UTF-8 can save you a lot of trouble though.

Mapping MIME types to files is easy. As is mapping the character encoding (for application/xml the character encoding is utf-8 by default.):

AddType {insert mime here};charset=utf-8 .{extension here}

Examples:

(Note that Apache might have enabled some of those by default.)

Comments

  1. If I'm not mistaken, Mozilla stops loading stylesheets (and scripts, for that matter) if the applied TYPE attribute doesn't match the Content-Type value of the HTTP response. Only when it is in X(HT)ML rendering mode, though.

    So, if you have a LINK to a CSS file with TYPE equal to text/css, but the CSS file is served with Content-Type: text/plain, Mozilla won't load it.

    Posted by Asbjørn Ulsberg at

  2. Asbjørn luckily filled the gap and at least mentioned text/css (mind, not that one would youse this type that often... ;-) )

    Posted by csant at

  3. Asbjørn, only in standard compliant mode. But I'm not sure if they are checking the TYPE attribute or just the file referred to, since after all, the TYPE attribute is just a hint and probably wrong. For example, I know Mozilla supports type="text/xslt" (and text/xsl) on the XML Style Sheet PI for compatibility with Internet Explorer. If you actually serve the XSLT file with that MIME type it won't be loaded.

    I added text/css by the way.

    Posted by Anne at

  4. Note that you can mix mime types on a page. Eg: when using ‹style type="text/css"› on a page of "text/html". Hmmm.

    Posted by Chris Hester at

  5. Via Weblog about Markup & Style:

    An excellent example of why a browser shouldn’t second-guess file types.

    Posted by K-Squared Ramblings » Trust the MIME at

  6. I have an XML file that calls an XSLT file in as a stylesheet. It works fine in IE, and also in Mozilla Firefox 0.9.2 on the local machine, but when I upload it to a server, IE still works fine, but Mozilla gives an error:

    Error loading stylesheet: An XSLT stylesheet does not have an XML mimetype:

    The XML file has this:

    <?xml-stylesheet type="text/xsl" href="cssformat.xsl"?>

    and the stylesheet has this:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    On Tools -> Page Info, the type says text/html in the local version that's okay in Mozilla, but type is unknown in the error version on the server.

    Does anyone know why?

    Posted by Linda at

  7. Oh, how weird, I uploaded it to another server and it worked although the type still says "text/html". Maybe something with the Apache or whatever server configuration is different.

    Is there a way to set the MIME type from the .xml or .xslt file? In a meta tag or something?

    Posted by Linda at

  8. O please, no META elements for XML.

    Use a .xml extension for your XSLT file and it will probably work.

    Posted by Anne at