Skip to content

Commit ed7e061

Browse files
thisismydesignbyroot
authored andcommitted
[Fix rails#53683] Reduce cache time for non-asset files in public dir
1 parent c2c7e20 commit ed7e061

File tree

1 file changed

+11
-2
lines changed
  • railties/lib/rails/generators/rails/app/templates/config/environments

1 file changed

+11
-2
lines changed

railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@ Rails.application.configure do
1717
config.action_controller.perform_caching = true
1818
<%- end -%>
1919

20-
# Cache assets for far-future expiry since they are all digest stamped.
21-
config.public_file_server.headers = { "cache-control" => "public, max-age=#{1.year.to_i}" }
20+
# Cache digest stamped assets for far-future expiry.
21+
# Short cache for others: robots.txt, sitemap.xml, 404.html, etc.
22+
config.public_file_server.headers = {
23+
"cache-control" => lambda do |path, _|
24+
if path.start_with?("/assets/")
25+
"public, max-age=#{1.year.to_i}"
26+
else
27+
"public, max-age=#{1.minute.to_i}"
28+
end
29+
end
30+
}
2231

2332
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
2433
# config.asset_host = "http://assets.example.com"

0 commit comments

Comments
 (0)