0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 02:19:55 +08:00
discourse/lib/freedom_patches/propshaft_patches.rb
discourse-patch-triage[bot] ef6ece39e3
FIX: Raise error after exhausting retries in development asset lookup (#39935)
## Summary

We have retry logic for `Propshaft::MissingAssetError` in development
mode, clearing the asset cache and retrying up to 3 times when ember-cli
might have replaced assets. However, the code had a bug where after
exhausting all retries, it silently returned `nil` instead of raising
the error.

---------

Co-authored-by: discourse-patch-triage[bot] <272280883+discourse-patch-triage[bot]@users.noreply.github.com>
2026-05-13 10:06:49 +01:00

52 lines
1.3 KiB
Ruby
Vendored

# frozen_string_literal: true
Propshaft::Asset.prepend(
Module.new do
def already_digested?
logical_path.to_s.start_with?("chunk.") || super
end
end,
)
Propshaft::Helper.prepend(
Module.new do
def compute_asset_path(path, options = {})
attempts = 0
begin
super
rescue Propshaft::MissingAssetError => e
if Rails.env.test?
# Assets might not be compiled in test mode. Just return a fake path
"/assets/#{path.sub(".", "-aaaaaaaa.")}"
elsif Rails.env.development?
# Ember-cli might've replaced the assets
Rails.application.assets.load_path.send(:clear_cache)
attempts += 1
retry if attempts < 3
raise e
else
raise e
end
end
end
end,
)
Propshaft::Compiler::SourceMappingUrls.prepend(
Module.new do
def compile(asset, input)
if asset.logical_path.to_s.include?(".digested.")
input
else
super
end
end
def source_mapping_url(*args)
# Propshaft insists on converting sourcemap URLs to absolute paths. We want to keep
# relative paths so that we can serve assets from different subdirectories without needing
# to recompile them
super.gsub(%r{sourceMappingURL=\S+/([^/]+\.map)}, 'sourceMappingURL=\1')
end
end,
)