Custom Content Type for Files in Rails Public Folder

For assets stored in the "public" folder of the ruby-on-rails application, can I change the "Content-Type" when running "script / server"? For example, I'm trying to create an HTML5 application that supports offline mode and have "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 ... 
+4
source share
2 answers

As in Rails 5, setting this to the initializer works:

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

I am not sure about other versions.

nb that it will not work Mime::Type.register "text/cache-manifest", :manifest is only for rails controllers.

I'm not sure if Rails::Rack::Static used everywhere in Rails. Rails uses ActionDispatch::Static , which does not inherit from Rails::Rack::Static or something like that. But it uses a few things from Rack , including Rack::Mime , which (I think?) Is 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

+1
source
Good question. I would suggest digging into Rails :: Rack :: Static, which is what these days is for storing files.

Alternatively, you can write action-action to serve only this type of file. Pass them using send_file and pass the type explicitly, for example:

 send_file params[:filename], :type => 'text/cache-manifest' 

http://apidock.com/rails/ActionController/Streaming/send_file

+3
source

Source: https://habr.com/ru/post/1307049/


All Articles