Jonathan Bennett

Automatically using environment specific images

It’s pretty common to want to use different assets, icons etc, in different environments – a development, staging, and production icon for example.

One way I’ve done this is by using different variants with the rails_icons gem for each environment, and then setting the default variant dynamically. File paths look like:

app/assets/svg/icons/marketing/development/logo.svg
app/assets/svg/icons/marketing/staging/logo.svg
app/assets/svg/icons/marketing/production/logo.svg

And for the custom library setup, I include:

# config/initializers/rails_icons.rb

RailsIcons.configure do |config|
	# heroicon config etc
	
	config.libraries.merge!({
		marketing: {
			default_variant: Rails.env,
		}
	})

By using the Rails environment as the default variant, this means your assets usage will automatically switch for you.

Note: The marketing library name/folder name is arbitrary, but I usually keep the environment based assets separate from normal assets to avoid needing to duplicate everything in every environment.