Rails“公共"文件夹中文件的自定义内容类型 [英] Custom Content-Type for File in Rails 'public' Folder

查看:43
本文介绍了Rails“公共"文件夹中文件的自定义内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于存储在 ruby​​-on-rails 应用程序的public"文件夹中的资产,是否可以在运行脚本/服务器"时更改内容类型"?例如,我试图创建一个支持离线模式的 HTML5 应用程序,并有一个offline.manifest".当我跑步时:

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

返回如下头信息:

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

但是,HTML5 规范要求:

However, HTML5 specifications require:

HTTP/1.1 200 OK
...
Content-Type: text/cache-manifest
...

推荐答案

从 Rails 5 开始,把它放在初始化器中是可行的:

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

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

我不确定其他版本.

n.b.它将工作 Mime::Type.register "text/cache-manifest", :manifest — 这仅适用于 rails控制器.

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

我不确定 Rails::Rack::Static 是否在 Rails 的任何地方使用.Rails 使用 ActionDispatch::Static,它不继承自 Rails::Rack::Static 或类似的东西.但它确实使用了 Rack 中的一些东西,包括 Rack::Mime,它(我认为?)与使用的 Mime 完全分开Rails 中的其他地方.

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.

ActionDispatch::Static 的源代码:https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/static.rb

这篇关于Rails“公共"文件夹中文件的自定义内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆