Sinatra 的 Rackup 中的 Rack::ResponseHeaders

发布于 2024-09-03 12:44:46 字数 803 浏览 3 评论 0原文

我认为这是一个非常简单的问题,但我似乎无法做到正确。基本上,我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-09-10 12:44:46

如果有人感兴趣,我可以让它工作。看起来没有一种简单的方法可以在 Heroku 上安装 rack-contrib-0.9.3,但我需要的唯一文件是 response_headers.rb,所以我只是将其复制到我的项目中目录并按如下方式编辑我的rackup:

require 'rack/contrib/response_headers'

# set default cache-control header if not set by Sinatra
use Rack::ResponseHeaders do |headers|
    if not headers['Cache-Control']
        headers['Cache-Control'] = "public, max-age=3600"
    end
end

这对我没有在 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 was response_headers.rb, so I simply copied this into my project directory and edited my rackup as follows:

require 'rack/contrib/response_headers'

# set default cache-control header if not set by Sinatra
use Rack::ResponseHeaders do |headers|
    if not headers['Cache-Control']
        headers['Cache-Control'] = "public, max-age=3600"
    end
end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文