summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-07-13 23:29:54 +0200
committererdgeist <erdgeist@erdgeist.org>2026-07-13 23:29:54 +0200
commit802e81f8551e10a3f97846a49636e675c7e21cb3 (patch)
treee7781b88465cc35f51ea1839d655b6507383a5b7
parent0113d895e2fd8bf2cf88ae0196b9ab144ac9350b (diff)
Add cache-buster using mtime of css and js files outside the asset pipeline
-rw-r--r--app/helpers/admin_helper.rb6
-rw-r--r--app/views/layouts/admin.html.erb8
-rw-r--r--test/models/helpers/admin_helper_test.rb10
3 files changed, 20 insertions, 4 deletions
diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb
index 08336ef..1e84620 100644
--- a/app/helpers/admin_helper.rb
+++ b/app/helpers/admin_helper.rb
@@ -8,4 +8,10 @@ module AdminHelper
8 link_to raw('<span class="inactive">English</span>'), url_for(params.permit(:locale, :page_path).to_h.merge('locale' => 'de')) 8 link_to raw('<span class="inactive">English</span>'), url_for(params.permit(:locale, :page_path).to_h.merge('locale' => 'de'))
9 end 9 end
10 end 10 end
11
12 def mtime_busted_path(path)
13 file = Rails.public_path.join(path.sub(%r{\A/}, ""))
14 raise "Static asset not found for cache-busting: #{path} (looked for #{file})" unless File.exist?(file)
15 "#{path}?v=#{File.mtime(file).to_i}"
16 end
11end 17end
diff --git a/app/views/layouts/admin.html.erb b/app/views/layouts/admin.html.erb
index 8bbc0d5..0856a0f 100644
--- a/app/views/layouts/admin.html.erb
+++ b/app/views/layouts/admin.html.erb
@@ -10,10 +10,10 @@
10 <%= javascript_tag "var AUTH_TOKEN = #{form_authenticity_token.inspect};" if protect_against_forgery? %> 10 <%= javascript_tag "var AUTH_TOKEN = #{form_authenticity_token.inspect};" if protect_against_forgery? %>
11 <%= javascript_include_tag 'admin_bundle' %> 11 <%= javascript_include_tag 'admin_bundle' %>
12 <%= tinymce_assets %> 12 <%= tinymce_assets %>
13 <link rel="stylesheet" href="/stylesheets/admin.css"> 13 <link rel="stylesheet" href="<%= mtime_busted_path('/stylesheets/admin.css') %>">
14 <script src="/javascripts/admin_search.js"></script> 14 <script src="<%= mtime_busted_path('/javascripts/admin_search.js') %>"></script>
15 <script src="/javascripts/admin_interface.js"></script> 15 <script src="<%= mtime_busted_path('/javascripts/admin_interface.js') %>"></script>
16 <script src="/javascripts/related_assets.js"></script> 16 <script src="<%= mtime_busted_path('/javascripts/related_assets.js') %>"></script>
17 <script> 17 <script>
18 var ADMIN_SEARCH_URL = "<%= admin_search_path %>"; 18 var ADMIN_SEARCH_URL = "<%= admin_search_path %>";
19 var ADMIN_MENU_SEARCH_URL = "<%= admin_menu_search_path %>"; 19 var ADMIN_MENU_SEARCH_URL = "<%= admin_menu_search_path %>";
diff --git a/test/models/helpers/admin_helper_test.rb b/test/models/helpers/admin_helper_test.rb
index 23d9f40..94e9861 100644
--- a/test/models/helpers/admin_helper_test.rb
+++ b/test/models/helpers/admin_helper_test.rb
@@ -1,4 +1,14 @@
1require 'test_helper' 1require 'test_helper'
2 2
3class AdminHelperTest < ActionView::TestCase 3class AdminHelperTest < ActionView::TestCase
4 test "mtime_busted_path appends the file's real mtime as a query param" do
5 path = "/stylesheets/admin.css"
6 expected_mtime = File.mtime(Rails.public_path.join("stylesheets/admin.css")).to_i
7
8 assert_equal "#{path}?v=#{expected_mtime}", mtime_busted_path(path)
9 end
10
11 test "mtime_busted_path raises clearly for a missing file rather than silently omitting the version" do
12 assert_raises(RuntimeError) { mtime_busted_path("/stylesheets/does_not_exist.css") }
13 end
4end 14end