Skip to content

Comments

Fix initialization order issue causing missing asset errors in Sprockets#2566

Merged
joelhawksley merged 4 commits intomainfrom
fix_sprockets_init_order
Feb 24, 2026
Merged

Fix initialization order issue causing missing asset errors in Sprockets#2566
joelhawksley merged 4 commits intomainfrom
fix_sprockets_init_order

Conversation

@camertron
Copy link
Contributor

What are you trying to accomplish?

This PR should fix #2433, which describes an initialization order problem that causes Sprockets to raise missing asset errors.

What approach did you choose and why?

The issue includes a single-file reproduction, which was very useful in determining the fix. The problem boils down to initialization order. Both Sprockets and ViewComponent define a number of after_initialize blocks, which Rails invokes in the order they were added. The VC engine assumes the Sprockets railtie adds its "after initializers" first, but if VC's after initializers were added first, VC tries to read certain Sprockets config options which aren't yet set.

The fix is to wait until all after initializers have executed, at which point Sprockets should be fully set up. Fortunately Rails exposes the after_routes_loaded callback, which appears to fire after all initializers and after_initialize blocks.

@joelhawksley
Copy link
Member

@jan-evermood @dark-panda @Paul-Bob would you mind testing #2566 in your apps before we land it?

@Paul-Bob
Copy link

gem "view_component", github: "ViewComponent/view_component", branch: "fix_sprockets_init_order"
gem "sprockets-rails"

Works for me ☝🏼

Thanks for looking into this, guys 🎉

@joelhawksley joelhawksley merged commit 86cbe87 into main Feb 24, 2026
18 checks passed
@joelhawksley joelhawksley deleted the fix_sprockets_init_order branch February 24, 2026 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assets resolution fails when initializers are run in different order

3 participants