Skip to content

Commit ef54119

Browse files
committed
Set the mtime of pre-cached assets to the newest mtime of the files being packaged.
This should allow us to achieve less unneeded cache invalidations due to mtime changes of the prebuilt caches.
1 parent 037d5c7 commit ef54119

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

lib/jammit/packager.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ def initialize
3737
# changed since their last package build.
3838
def precache_all(output_dir=nil, base_url=nil)
3939
output_dir ||= File.join(PUBLIC_ROOT, Jammit.package_path)
40-
cacheable(:js, output_dir).each {|p| cache(p, 'js', pack_javascripts(p), output_dir) }
40+
cacheable(:js, output_dir).each {|p| cache(p, 'js', pack_javascripts(p), output_dir, nil, newest_mtime(p, :js)) }
4141
cacheable(:css, output_dir).each do |p|
42-
cache(p, 'css', pack_stylesheets(p), output_dir)
42+
mtime = newest_mtime(p, :css)
43+
cache(p, 'css', pack_stylesheets(p), output_dir, nil, mtime)
4344
if Jammit.embed_assets
44-
cache(p, 'css', pack_stylesheets(p, :datauri), output_dir, :datauri)
45+
cache(p, 'css', pack_stylesheets(p, :datauri), output_dir, :datauri, mtime)
4546
if Jammit.mhtml_enabled && base_url
46-
mtime = Time.now
4747
asset_url = "#{base_url}#{Jammit.asset_url(p, :css, :mhtml, mtime)}"
4848
cache(p, 'css', pack_stylesheets(p, :mhtml, asset_url), output_dir, :mhtml, mtime)
4949
end
@@ -72,6 +72,11 @@ def individual_urls(package, extension)
7272
package_for(package, extension)[:urls]
7373
end
7474

75+
# Return the modification time of the newest file in the package
76+
def newest_mtime(package, extension)
77+
package_for(package, extension)[:paths].map { |path| File.mtime(path) }.max
78+
end
79+
7580
# Return the compressed contents of a stylesheet package.
7681
def pack_stylesheets(package, variant=nil, asset_url=nil)
7782
@compressor.compress_css(package_for(package, :css)[:paths], variant, asset_url)

0 commit comments

Comments
 (0)