Sinatra 的 Rackup 中的 Rack::ResponseHeaders
我认为这是一个非常简单的问题,但我似乎无法做到正确。基本上,我尝试使用 Rack 中间件将默认的 Cache-Control 标头设置到我的 Sinatra 应用程序提供的所有响应中。看起来 Rack::ResponseHeaders 应该能够完全满足我的需要,但是在尝试使用演示的语法时出现错误 here 在我的rackup 文件中:
use Rack::ResponseHeaders do |headers|
headers['X-Foo'] = 'bar'
headers.delete('X-Baz')
end
我能够让 Rack::Cache 成功工作,如下所示:
use Rack::Cache,
:default_ttl => 3600
但是,这并不能完全实现输出我想要,而 Rack::ResponseHeaders 提供了对标头的细粒度控制。
仅供参考,我的网站托管在 Heroku 上,所需的 Rack gem 在我的 .gems 清单中指定。
谢谢!
更新:经过一些研究,看起来第一个问题是在rack-contrib版本(0.9.2)中找不到Rack::ResponseHeaders )已安装。我将首先对此进行研究。
I think this is a very easy one, but I can't seem to get it right. Basically, I'm trying to use Rack middleware to set a default Cache-Control header into all responses served by my Sinatra app. It looks like Rack::ResponseHeaders should be able to do exactly what I need, but I get an error when attempting to use the syntax demonstrated here in my rackup file:
use Rack::ResponseHeaders do |headers|
headers['X-Foo'] = 'bar'
headers.delete('X-Baz')
end
I was able to get Rack::Cache to work successfully as follows:
use Rack::Cache,
:default_ttl => 3600
However, this doesn't achieve exactly the output I want, whereas Rack::ResponseHeaders gives fine-grained control of the headers.
FYI, my site is hosted on Heroku, and the required Rack gems are specified in my .gems manifest.
Thanks!
Update: After doing some research, it looks like the first issue is that Rack::ResponseHeaders is not found in the version of rack-contrib (0.9.2) which was installed. I'll start by looking into that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果有人感兴趣,我可以让它工作。看起来没有一种简单的方法可以在 Heroku 上安装
rack-contrib-0.9.3
,但我需要的唯一文件是response_headers.rb
,所以我只是将其复制到我的项目中目录并按如下方式编辑我的rackup:这对我没有在 Sinatra 中指定显式 Cache-Control 标头的对象(即静态资产)设置了 1 小时的默认 max-age。
In case anyone's interested, I was able to get this working. It didn't look like there would be an easy way to install
rack-contrib-0.9.3
on Heroku, but the only file I needed wasresponse_headers.rb
, so I simply copied this into my project directory and edited my rackup as follows:This sets a default max-age of 1 hr on objects for which I'm not specifying an explicit Cache-Control header in Sinatra – namely, static assets.