gpt4 book ai didi

ruby-on-rails - 将 Expires header 添加到 rails Assets

转载 作者:行者123 更新时间:2023-12-04 06:21:48 25 4
gpt4 key购买 nike

我目前正在构建一个 rails 应用程序并试图提高其页面速度洞察力标记。唯一剩下的警告是我的 Assets 标题。

在查看了 Rails 文档和互联网上的一些文章后,这是我在 production.rb 文件中提出的内容:

config.public_file_server.headers = {
'Cache-Control' => 'public, s-maxage=31536000, max-age=86400',
'Expires' => "#{1.day.from_now.httpdate}"
}

现在,这是我的 js/css 文件的 chrome 网络选项卡中显示的内容:
cache-control:public, max-age=86400
content-encoding:gzip
content-length:90444
content-type:application/javascript
date:Tue, 22 Aug 2017 10:49:05 GMT
last-modified:Tue, 22 Aug 2017 08:49:06 GMT
server:...

缓存控制按其应有的方式出现,但没有 expires header 。

我还使用了 cloudfront,但我不确定我是否应该/可以从那里更改标题。

我做错了吗?

最佳答案

如果您使用的是 Rails 4 ,那么只有 Cache-Control可以为 Rails 服务的 Assets 设置响应头。这是一个限制。

您的解决方案适用于 rails 5
Rails 5 源代码中有一个测试,它确保响应中包含自定义 header :

  def test_serves_files_with_headers
headers = {
"Access-Control-Allow-Origin" => "http://rubyonrails.org",
"Cache-Control" => "public, max-age=60",
"X-Custom-Header" => "I'm a teapot"
}

app = ActionDispatch::Static.new(DummyApp, @root, headers: headers)
response = Rack::MockRequest.new(app).request("GET", "/foo/bar.html")

assert_equal "http://rubyonrails.org", response.headers["Access-Control-Allow-Origin"]
assert_equal "public, max-age=60", response.headers["Cache-Control"]
assert_equal "I'm a teapot", response.headers["X-Custom-Header"]
end

另外,即使您添加 Expires标题不知何故, max-age无论如何都会优先。

关于ruby-on-rails - 将 Expires header 添加到 rails Assets ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45855706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com