Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Content-Type for File in Rails 'public' Folder

For assets stored in the 'public' folder of a ruby-on-rails application is it possible to change the 'Content-Type' when running 'script/server'? For example, I am attempting to create an HTML5 application supporting offline mode, and have an 'offline.manifest'. When I run:

curl -I localhost:3000/offline.mainfest

The following header information is returned:

HTTP/1.1 200 OK
...
Content-Type: text/plain
...

However, HTML5 specifications require:

HTTP/1.1 200 OK
...
Content-Type: text/cache-manifest
...
like image 573
Kevin Sylvestre Avatar asked Apr 15 '10 15:04

Kevin Sylvestre


1 Answers

As of Rails 5, putting this in an initializer works:

Rack::Mime::MIME_TYPES[".manifest"]="text/cache-manifest"

I'm not sure about other versions.

n.b. that it will not work to do Mime::Type.register "text/cache-manifest", :manifest — this is only for rails controllers.

I'm not sure if Rails::Rack::Static is used anywhere in Rails. Rails uses ActionDispatch::Static, which doesn't inherit from Rails::Rack::Static or anything like that. But it does use several things from Rack, including Rack::Mime, which is (i think?) completely separate from Mime which is used elsewhere in Rails.

source for ActionDispatch::Static: https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/static.rb

like image 183
John Bachir Avatar answered Sep 23 '22 14:09

John Bachir