Skip to content

Commit f5796fd

Browse files
authored
Merge pull request rails#55249 from thisismydesign/patch-2
[Fix rails#53683] Reduce cache time for non-asset files in public dir
2 parents 9876751 + 6b5a125 commit f5796fd

File tree

1 file changed

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

1 file changed

+14
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,20 @@ 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+
# Files in /assets/ are expected to be fully immutable.
26+
# If the content change the URL too.
27+
"public, immutable, max-age=#{1.year.to_i}"
28+
else
29+
# For anything else we cache for 1 minute.
30+
"public, max-age=#{1.minute.to_i}, stale-while-revalidate=#{5.minutes.to_i}"
31+
end
32+
end
33+
}
2234

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

0 commit comments

Comments
 (0)