I’m creating a Sinatra web application which I would like to serve mime type application/xhtml+xml for all web pages served. I am using Builder as template engine. Note that the application will also be serving application/xml for various AJAX api interfaces, also using the Builder template engine, as well as text/css using the scss template engine.
I notice that the webpages that get generated receive a text/html content type while the api interfaces get an application/xml content type instead. However, I don’t know how the mime type is decided; presence of the html tag? usage of a layout or not? Whichever the case, I would like the application to issue a application/xhtml+xml mimetype instead of the text/html mime type.
I know I can specify the mime type with the content_type call, but I’d like to refrain from needing this in every routing function. Is there any way I can set a default mime type for a given template engine? Or can the mime type be controlled by the view’s filename? Does Tilt provide any means to control mime type in a nice way?
Since your application is factored into separate objects, it should be straightforward to pull all of the AJAX routes into a separate Sinatra application. This will allow you to use Sinatra’s after hook. The only requirement is for your AJAX calls to have something that identifies them as AJAX; for example, by setting a request header to a special value, or by using a specially formatted route. Here is an example rackup file of the unique route method:
In this example, all routes starting with /ajax are handled by instances of the AjaxRoutes class. The after hook ensures that the content type for those responses are ‘application/xml’. The non-ajax routes are not affected.
A few things to remember: