如何将HTTP标头设置在Heroku上的Ruby/Sinatra应用中?
-
11-10-2019 - |
题
我有一个位于Ruby和Sinatra的工作应用程序,该应用程序部署在Heroku上。
我想利用使用Varnish的Heroku上可用的HTTP缓存。
我不确定设置标头的最佳方法和正确的语法。
对最佳方法和语法有什么想法吗?
before do
headers "Content-Type" => "text/html; charset=utf8"
end
get '/' do
headers['Cache-Control'] = 'public, max-age=600'
# SOME STUFF HERE
haml :home, {:layout => :layout_minfooter}
end
解决方案
通常动态生成的页面没有缓存,所以
response.headers['Cache-Control'] = 'public, max-age=300'
标题是正确的起点。
尝试使用其中一种服务使用基于Web的服务“看看它们是否出现在您网站上发送回的HTTPD标头中。
其他提示
您还可以使用此语法访问响应对象的标题字段:
response['Cache-Control'] = 'public, max-age=600'
在Sinatra中,您可以使用 cache_control
方法:
get '/' do
# Cache for 24 hours
cache_control :public, max_age: 86400
# Your magic goes here
end
不隶属于 StackOverflow