Rails asset path differs between asset_path and stylesheet_link_tag

222 Views Asked by At

Using Sprockets in a Rails 6 development environment, stylesheet_link_tag('mobile') generates:

<link rel=\"stylesheet\" media=\"screen\" href=\"/assets/mobile.debug-2c11e2473c793c2475ab3559e38e0b0331956e570b884642ecbe1f21fa2467f1.css\" />

But stylesheet_path('mobile') generates:

"/assets/mobile-ea85a9ecd4c09607a4f2d7dbfeb5e145cdb4ce2033726041472418be82c104ae.css"

On the development server both of these asset paths render the same content.

But I need to cache the correct asset path in a service worker. How can I generate an asset path that matches that produced by the stylesheet_link_tag helper?

(I could avoid the stylesheet_link_tag helper, but presumably I'm missing something here)

1

There are 1 best solutions below

0
On

The difference is because Sprockets separates out assets into individual files for inclusion when config.assets.debug is true - which it is by default in the development environment.

Sadly the option to toggle this flag off for an individual stylesheet_link_tag by setting the debug flag to false is broken - see this pull request: https://github.com/rails/sprockets-rails/pull/424

The workaround is to set config.assets.debug to false for the development environment, and to use stylesheet_link_tag( 'mobile', debug: true ) if we need to debug a particular asset.