Fix follow up tasks for assets:precompile when no manifest existed

When no `.sprockets-manifest-xxx.json` existed, every instance of
`Sprockets::Manifest` generated their own path with their own random
filename, and since this happened before the assets were actually
precompiled, they were all empty. So the error pages didn't find the
manifest and the non-digest assets also didn't have any assets to copy.

So lets create our own instance of `Sprockets::Manifest` here, AFTER
`assets:precompile`, which then loads the manifest json that was used
during precompile, so all precompiled assets are available.

closes #8366
This commit is contained in:
Benjamin Neff 2022-07-20 04:59:26 +02:00
parent 3b02eb87bd
commit 3c4da76be5
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
2 changed files with 22 additions and 13 deletions

View file

@ -6,7 +6,11 @@
* Fix deprecation warnings for sidekiq 7.0 [#8359](https://github.com/diaspora/diaspora/pull/8359) * Fix deprecation warnings for sidekiq 7.0 [#8359](https://github.com/diaspora/diaspora/pull/8359)
* Remove entypo-rails dependency to prepare for rails 6 [#8361](https://github.com/diaspora/diaspora/pull/8361) * Remove entypo-rails dependency to prepare for rails 6 [#8361](https://github.com/diaspora/diaspora/pull/8361)
* Remove compass-rails dependency which is not supported anymore [#8362](https://github.com/diaspora/diaspora/pull/8362) * Remove compass-rails dependency which is not supported anymore [#8362](https://github.com/diaspora/diaspora/pull/8362)
* Switch to sassc-rails which speeds up `assets:precompile` a lot [#8362](https://github.com/diaspora/diaspora/pull/8362)
* Remove markerb dependency which doesn't exist anymore [#8365](https://github.com/diaspora/diaspora/pull/8365) * Remove markerb dependency which doesn't exist anymore [#8365](https://github.com/diaspora/diaspora/pull/8365)
* Upgrade to rails 6.1 [#8366](https://github.com/diaspora/diaspora/pull/8366)
* Update the suggested Ruby version to 2.7. If you run into trouble during the update and you followed our installation guides, run `rvm install 2.7`. [#8366](https://github.com/diaspora/diaspora/pull/8366)
* Upgrade to bundler 2 [#8366](https://github.com/diaspora/diaspora/pull/8366)
## Bug fixes ## Bug fixes
* Fix that no mails were sent after photo export [#8365](https://github.com/diaspora/diaspora/pull/8365) * Fix that no mails were sent after photo export [#8365](https://github.com/diaspora/diaspora/pull/8365)

View file

@ -1,35 +1,40 @@
# frozen_string_literal: true # frozen_string_literal: true
namespace :assets do namespace :assets do
# create new assets manifest for tasks which run after assets:precompile
def assets_manifest
return @assets_manifest if @assets_manifest
config = Diaspora::Application.config
path = File.join(config.paths["public"].first, config.assets.prefix)
@assets_manifest = Sprockets::Manifest.new(Diaspora::Application.assets, path, config.assets.manifest)
end
desc "Generate error pages" desc "Generate error pages"
task :generate_error_pages => :environment do task generate_error_pages: :environment do
ApplicationController.view_context_class.assets_manifest = assets_manifest
renderer = ErrorPageRenderer.new codes: [404, 422, 500] renderer = ErrorPageRenderer.new codes: [404, 422, 500]
renderer.render renderer.render
end end
desc "Create non digest assets" desc "Create non digest assets"
task non_digest_assets: :environment do task non_digest_assets: :environment do
logger = ::Logging::Logger["assets:non_digest_assets"] Diaspora::Application.config.assets.non_digest_assets.each do |asset|
digested_path = assets_manifest.assets[asset]
raise Sprockets::Rails::Helper::AssetNotFound, "Precompiled asset for '#{asset}' not found" unless digested_path
non_digest_assets = Diaspora::Application.config.assets.non_digest_assets full_digested_path = File.join(assets_manifest.directory, digested_path)
full_non_digested_path = File.join(assets_manifest.directory, asset)
Rails.application.assets_manifest.assets.each do |logical_path, digested_path|
logical_pathname = Pathname.new(logical_path)
next unless non_digest_assets.any? {|testpath| logical_pathname.fnmatch?(testpath, File::FNM_PATHNAME) }
full_digested_path = Rails.root.join("public", "assets", digested_path)
full_non_digested_path = Rails.root.join("public", "assets", logical_path)
next unless FileUtils.uptodate?(full_digested_path, [full_non_digested_path]) next unless FileUtils.uptodate?(full_digested_path, [full_non_digested_path])
logger.info "Copying #{full_digested_path} to #{full_non_digested_path}" puts "Copying #{full_digested_path} to #{full_non_digested_path}"
FileUtils.copy_file(full_digested_path, full_non_digested_path, true) FileUtils.copy_file(full_digested_path, full_non_digested_path, true)
end end
end end
# Augment precompile with error page generation # Augment precompile with error page generation
task :precompile do Rake::Task[:precompile].enhance do
Rake::Task["assets:generate_error_pages"].invoke Rake::Task["assets:generate_error_pages"].invoke
Rake::Task["assets:non_digest_assets"].invoke Rake::Task["assets:non_digest_assets"].invoke
end end