diff --git a/test/test_app.rb b/test/test_app.rb index a3a2292..9b92c23 100644 --- a/test/test_app.rb +++ b/test/test_app.rb @@ -34,6 +34,19 @@ class AppTest < Minitest::Test end end + # Same for every /vendor module our own JS imports: one that changes with + # the app (mdi.mjs gaining an icon) left cached under the old URL while + # its freshly versioned importer asks for a new export is a dead page. + def test_every_imported_vendor_module_has_a_versioned_importmap_entry + response = request.get("/") + sources = Dir.glob(File.join(__dir__, "..", "public", "js", "*.js")).map { |path| File.read(path) }.join("\n") + vendor = sources.scan(%r{from\s+"(/vendor/[^"?]+)"}).flatten.uniq + refute_empty vendor + vendor.each do |path| + assert_match(%r{"#{Regexp.escape(path)}":\s*"#{Regexp.escape(path)}\?v=\d+"}, response.body, "#{path} is missing from the importmap") + end + end + # Unversioned /vendor modules must be revalidated, or a deploy adding an # export to one (imported by a freshly versioned /js module) breaks a # browser still holding the old copy — see static_cache_control in app.rb. diff --git a/views/layout.erb b/views/layout.erb index 2593a58..cedc19d 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -10,7 +10,7 @@ - +
@@ -25,14 +25,18 @@ navigator.serviceWorker.register("/sw.js").catch(() => {}); } - - - <%# Vendored libs above don't change between deploys, so they're left - unversioned; app.js's own modules do, and map.js statically imports - them by bare path — an import map remaps those paths to a versioned - URL so editing e.g. gpx.js also busts its cache, without having to - touch every file's import statements. Must come before the module - script below: the importmap has to be parsed first. %> + + + <%# Every app module and every vendored module they import is versioned + (ASSET_VERSION, a fresh value per deploy): map.js and the rest import + each other — and /vendor modules — by plain path, and an import map + remaps those paths to a versioned URL, so a deploy busts all of them + without touching any import statement. A vendored module that changes + with the app (mdi.mjs gaining an icon) can't be left cached while the + module importing it is fresh. Static files are also revalidated + (static_cache_control in app.rb) as a backstop for anything loaded + some other way. Must come before the module script below: the + importmap has to be parsed first. %>